在cakephp中,如何在相关字段中查找条件?

时间:2021-12-29 09:02:35

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.)

我有一个模型(列表),它拥有并属于一些不同的模型,我想找到所有这个模型,它的相关模型(Openhouse)有一个条件。 “拥有并属于”是在模型文件中设置的。清单中的开放式房屋和开放式房屋属于清单。 (对于我想要数据的其他一些模型,列表有很多和很多。)

I've been trying.

我一直在努力。

$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'))
));

but to no avail.

但无济于事。

Error: 1054: Unknown column 'Openhouse.date' in 'where clause

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模型上搜索并获得相关的列表但是然后以不同的格式返回数据,我需要将递归方式向上移动以从我的其他模型获取数据。 (最后我得到了重复的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?

我的问题基本上是我需要查询openhouse模型并使用它,还是我的相关模型的条件不正确?

4 个解决方案

#1


Try this:

$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'))
        )
    )
)

#2


If you have a lot of linked models, settings recursive to 2 might bring more data than you might want.

如果您有许多链接模型,递归到2的设置可能会带来比您想要的更多的数据。

If that is true, there is an alternative to what mavarro said, you can also try using the Containable behaviour:

如果这是真的,有一种替代mavarro所说的,你也可以尝试使用Containable行为:

$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')
        ),
        'contain' => array('Openhouse')
    )
);

#3


Try adding a $this->Listing->recursive = 2; before the call to find all. That should link up the tables before the call giving you access to the Openhouse model from the Listing model.

尝试添加$ this-> Listing-> recursive = 2;在通话之前找到所有。这应该在调用之前链接表,使您可以从Listing模型访问Openhouse模型。

#4


$this->List->find('all', array(
    'contain' => 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')
                             ),
        'order' => array('Openhouse.date DESC')
                      )
                             )
)

#1


Try this:

$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'))
        )
    )
)

#2


If you have a lot of linked models, settings recursive to 2 might bring more data than you might want.

如果您有许多链接模型,递归到2的设置可能会带来比您想要的更多的数据。

If that is true, there is an alternative to what mavarro said, you can also try using the Containable behaviour:

如果这是真的,有一种替代mavarro所说的,你也可以尝试使用Containable行为:

$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')
        ),
        'contain' => array('Openhouse')
    )
);

#3


Try adding a $this->Listing->recursive = 2; before the call to find all. That should link up the tables before the call giving you access to the Openhouse model from the Listing model.

尝试添加$ this-> Listing-> recursive = 2;在通话之前找到所有。这应该在调用之前链接表,使您可以从Listing模型访问Openhouse模型。

#4


$this->List->find('all', array(
    'contain' => 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')
                             ),
        'order' => array('Openhouse.date DESC')
                      )
                             )
)