In cakephp how can I do a find with conditions on a related field?(在 cakephp 中,如何使用相关字段的条件进行查找?)
问题描述
我有一个模型(列表),它拥有并属于几个不同的模型,我想找到所有这个模型,它的相关模型(Openhouses)有一个条件.拥有和属于"在模型文件中设置.Listings has many Openhouses 和 Openhouses 属于 Listings.(并且列表中有很多并且属于我想要数据的其他一些模型.)
I've got a model (Listings) that has and belongs to a few different models, I'd like to find all of this model where it's related model (Openhouses) have a condition. The 'has and belongs to' are setup in the model files. Listings hasmany Openhouses and Openhouses belong to Listings. (And listings has many and blongs to a few other models where I want the data.)
我一直在努力.
$this->Listing->find('all',
array('conditions' =>
array('Openhouse.date >' => $openhouse_start->format('Y-m-d H:i:s'),
'Openhouse.date <' => $openhouse_end->format('Y-m-d H:i:s'))
));
但无济于事.
Error: 1054: Unknown column 'Openhouse.date' in 'where clause
我知道我可以搜索 Openhouse 模型并获取相关列表,但随后数据以不同的格式返回,我需要使用递归方式从我的其他模型中获取数据.(我最终得到了重复的开放日数据!).如果需要,我可以发布更多代码示例.
I know I can search on the Openhouse model and get related Listings but then the data is returned in a different format and I need to turn recursion way up to get data from my other models. (And I end up with duplicate openhouse data!). I can post some more code examples if needed.
我的问题基本上是我只需要查询 openhouse 模型并接受它,还是我在相关模型上放置条件的语法不正确?
My question is basically do I need to just query the openhouse model and live with it or is my syntax for putting conditions on related models incorrect?
推荐答案
试试这个:
$this->List->find('all', array(
'contain' => array(
'Openhouse.conditions' => array(
'Openhouse.date >' => $openhouse_start->format('Y-m-d H:i:s'),
'Openhouse.date <' => $openhouse_end->format('Y-m-d H:i:s'))
)
)
)
这篇关于在 cakephp 中,如何使用相关字段的条件进行查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 cakephp 中,如何使用相关字段的条件进行查找


基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01