Joost
Joost

Reputation: 418

CakePhp 3.x select contain where row is NULL

It's not clear to me how I can fetch records that don't have a reference in an other row using contain() in Cakephp

public function initialize(array $config)
{        
    $this->hasmany('Prior', [
        'className' => 'Prior',
        'foreignKey' => 'photoID'
    ]);
}

public function search()
{
    $query = $this->find('all')->contain(['Prior']);
    return $query;
}

This returns something like:

-> results
  ->0
     ->ID = 1
     ->Prior = null

  ->1
     ->ID = 2
     ->Prior = array()

  ->2
     ->ID = 3
     ->Prior = array()

  ->3
     ->ID = 4
     ->Prior = null

How can I return only the NULL results?

Upvotes: 0

Views: 641

Answers (1)

arilia
arilia

Reputation: 9398

use notMatching (see the cookbook)

$this->find('all')
    ->contain(['Prior'])
    ->notMatching('Prior');

Upvotes: 1

Related Questions