CakePHP 在 hasMany 关系中搜索

发布于 2024-09-13 20:14:51 字数 751 浏览 2 评论 0原文

我有一个模型内容,它属于类别,有许多发布商,并且发布商属于城市。 还有一个搜索表单,用户可以从下拉框中选择要查看的类别和城市。

但如何将这两者结合在一个分页条件中呢?我的意思是我不能做类似的事情:

$this->paginate('Content',array('conditions' =>array('Category.id'=>$category,
'City.id'=>$city)));

因为要获取城市蛋糕会执行不同的查询。

我也不能做类似的事情:

$this->paginate('Content',array('conditions' =>array('Category.id'=>$category),
 'contain'=>array('Publisher.City'=>array('conditions'=>array(City.id'=>$city)))));

因为这将根据类别搜索并根据 $city 过滤城市结果。

我知道我可以做类似的事情:

$this->Content->Publisher->City->find(...)

但这会改变分页数据的输出。

我通常做的是编写自定义查询,其中我左连接所有模型并在 WHERE 中过滤结果。但我想问是否有更多的蛋糕(原文如此)的方式!

谢谢

I have a model Content which belongsTo Categories, hasMany Publishers, and Publisher belongsTo city.
There is also a search form where someone selects from a drop-down box which category to view and which city.

But how can I combine these two in a single paginate condition? I mean I cannot do something like:

$this->paginate('Content',array('conditions' =>array('Category.id'=>$category,
'City.id'=>$city)));

because to get cities cake performs a different query.

Nor can I do something like:

$this->paginate('Content',array('conditions' =>array('Category.id'=>$category),
 'contain'=>array('Publisher.City'=>array('conditions'=>array(City.id'=>$city)))));

because this will search according to category and filter the city results according to $city.

I know I can do something like:

$this->Content->Publisher->City->find(...)

but this will change the output of my paginated data.

What I usually do is write my custom query where I LEFT join all models and filter the results in WHERE. But I wanted to aks if there is a more cake (sic) way!

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2024-09-20 20:14:51

当我第一次尝试创建一个具有多种关系的简单搜索引擎时,我也经历过同样的事情。

我使用了多个 $this->find() 并将其分配在一个变量中,然后在我使用的分页代码上 $this->paginate(array_merge($ 中使用的变量的名称)这->find()));

希望这会帮助你......

〜吉奥

i've experienced the same thing when i first try to create a simple search engine with hasmany relationship.

i used multiple $this->find() and assigned it in a variable then on the paginate code i used$this->paginate(array_merge(name of the variables used in $this->find()));.

hope this will help you...

~gio

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文