Cake PHP返回空值的记录

时间:2015-10-28 15:05:42

标签: php mysql cakephp cakephp-3.0

我有一个索引方法:

public function index()
{
    $this->set('contracts',
        $this->paginate(
            $this->Contracts
                ->find()
                ->contain('Currencies'])
                ->where(['Contracts.created_by_id' => $this->Auth->user('id')])
                ->order(['Contracts.created' => "desc"])
        )
    );
    $this->set('_serialize', ['contracts']);
}

问题是它只返回具有Currency的记录(在我的数据库表currency_id中)。具有currency_id = null的记录。 所以我只想获得所有记录,无论他们是否有currency_id为null或不为null,但同时保持在我的结果中返回Currency实体

1 个答案:

答案 0 :(得分:0)

如果你已经烘焙了模型,那么你会遇到这个问题。因为通过烘焙模型,默认模型关联将设置为“INNER JOIN”。

如果你在src / model / ContactsTable中查看'ContactsTable',请确保你'左'加入'User'表或者你可以完全删除'joinType',因为在默认情况下cakephp设置Contain或join to 'LEFT JOIN'

class ContactsTable extends Table
{

   public function initialize(array $config)
    {

      $this->belongsTo('Currencies', [
            'foreignKey' => 'created_by_id',
            'joinType' => 'LEFT'
      ]);
   }
}