Cakephp 3 NOT IN query

10,486

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
10,486
Roger Kaplan
Author by

Roger Kaplan

Updated on June 05, 2022

Comments

  • Roger Kaplan
    Roger Kaplan almost 2 years

    I think this is a common pattern, but I can't find the elegant CakePHP way of doing it. The idea is to remove the values from a list which have already been chosen. To use an example from the Cake book:

    table Students (id int, name varchar)
    table Courses (id int, name varchar)
    table CoursesMemberships (id int, student_id int, course_id int, days_attended int, grade varchar)
    

    All I want to do is create a query which returns the courses that a given student has not yet signed up for, most probably to populate a select dropdown.

    If I weren't using Cake, I'd do something like

    select * from Courses where id not in 
        (select course_id from CoursesMemberships where student_id = $student_id)
    

    Or maybe an equivalent NOT EXISTS clause, or outer join trickery, but you get the idea.

    I'm stumped how to do this elegantly in Cake. It seems to me that this would be a common need, but I've researched for awhile, as well as tried some query ideas, to no avail.

  • Roger Kaplan
    Roger Kaplan over 8 years
    I managed to make that work using a more complex mechanism (see comments above). But what I was looking for is a way of doing this query in one shot, rather than querying for the existing records, coercing them into an array, then using the above.
  • user3082321
    user3082321 over 8 years