CakePhp 3.x select包含row为NULL的位置

时间:2017-02-15 11:46:32

标签: php cakephp cakephp-3.0 query-builder

我不清楚如何使用Cakephp中的contain()获取另一行中没有引用的记录

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

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

返回类似于:

的内容
-> results
  ->0
     ->ID = 1
     ->Prior = null

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

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

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

如何仅返回NULL结果?

1 个答案:

答案 0 :(得分:1)

使用notMatching(请参阅cookbook

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