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 中,如何使用相关字段的条件进行查找


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01