在cakephp如何可以做一个相关领域的条件找到? [英] In cakephp how can I do a find with conditions on a related field?

查看:99
本文介绍了在cakephp如何可以做一个相关领域的条件找到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型(Listings),它属于几个不同的模型,我想找到所有这个模型,它的相关模型(Openhouses)有一个条件。 has和belongs to在模型文件中设置。 Listings hasmany 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如何可以做一个相关领域的条件找到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆