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

In cakephp how can I do a find with conditions on a related field?(在 cakephp 中,如何使用相关字段的条件进行查找?)
本文介绍了在 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 中,如何使用相关字段的条件进行查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)