在 cakephp 中,如何使用相关字段的条件进行查找?

2021-12-21 00:00:00 php 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'))
        )
    )
)

相关文章