問(wèn)題描述
我有一個(gè)模型(列表),它擁有并屬于幾個(gè)不同的模型,我想找到所有這個(gè)模型,它的相關(guān)模型(Openhouses)有一個(gè)條件.擁有和屬于"在模型文件中設(shè)置.Listings has many Openhouses 和 Openhouses 屬于 Listings.(并且列表中有很多并且屬于我想要數(shù)據(jù)的其他一些模型.)
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'))
));
但無(wú)濟(jì)于事.
Error: 1054: Unknown column 'Openhouse.date' in 'where clause
我知道我可以搜索 Openhouse 模型并獲取相關(guān)列表,但隨后數(shù)據(jù)以不同的格式返回,我需要使用遞歸方式從我的其他模型中獲取數(shù)據(jù).(我最終得到了重復(fù)的開(kāi)放日數(shù)據(jù)!).如果需要,我可以發(fā)布更多代碼示例.
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.
我的問(wèn)題基本上是我只需要查詢 openhouse 模型并接受它,還是我在相關(guān)模型上放置條件的語(yǔ)法不正確?
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?
推薦答案
試試這個(gè):
$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'))
)
)
)
這篇關(guān)于在 cakephp 中,如何使用相關(guān)字段的條件進(jìn)行查找?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!