Cakephp 3 NOT IN query

这一生的挚爱 提交于 2019-11-30 13:54:20

If you want to use a subquery, simply pass a query object as the condition value, like

$subquery = $Courses->CoursesMemberships
    ->find()
    ->select(['CoursesMemberships.course_id'])
    ->where(['CoursesMemberships.student_id' => $student_id]);

$query = $Courses
    ->find()
    ->where([
        'Courses.id NOT IN' => $subquery
    ]);

As an alternative there's also Query::notMatching() (as of CakePHP 3.1), which can be used to select records whichs associated records do not match specific conditions:

$query = $Courses
    ->find()
    ->notMatching(['CoursesMemberships' => function (\Cake\ORM\Query $query) use ($student_id) {
        return $query
            ->where(['CoursesMemberships.student_id' => $student_id]);
    }]);

See also

In CakePHP 3 you can create NOT IN query like this.

$query = $Courses->find()
    ->where(['id NOT IN' => $ids]);

And in CakePHP 3 you can create IN query like this.

$query = $Courses->find()
    ->where(['id IN' => $ids]);

You can read about it in CakePHP 3 Cookbook - Creating IN Clauses.

Found IMO the most elegant answer... use the notMatching() option:

$data = $this->Courses->find("list")
                      ->notMatching("Students", 
                         function($q) use ($student_id) {
                            return $q->where(["Students.id"=>$student_id]);
                         }
                      );

This assumes that Students HasMany Courses and Courses HasMany Students of course.

I think this is the most elegant answer since it doesn't depend on knowing any SQL, and represents the logic of what I'm trying to achieve using Cake's semantics only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!