我有一个索引方法:
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_id中)。具有currency_id = null
的记录。
所以我只想获取所有记录,无论它们的currency_id是否为空,但同时保持在我的结果中返回货币实体
最佳答案
如果你已经烘焙了模型,那么你就会遇到这个问题。因为通过烘焙模型,默认模型关联将设置为“INNER JOIN”。
如果您查看 src/model/ContactsTable 中的“ContactsTable”,请确保您“LEFT JOIN”“User”表,或者您可以完全删除“joinType”,因为在默认情况下 cakephp 将 Contain 或 join 设置为'左连接'
class ContactsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Currencies', [
'foreignKey' => 'created_by_id',
'joinType' => 'LEFT'
]);
}
}
关于php - Cake PHP 返回带有空值的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394622/