CakePHP使用“包含”查找 [英] CakePHP find with "contain"

查看:74
本文介绍了CakePHP使用“包含”查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型与其他表/模型有很多关系,如果我使用包含参数,例如按一个模型的结果中的字段进行排序,查询结果中的所有其他表都将丢失,并且必须手动将其写入包含参数,如何我可以在不手动编写的情况下获取所有相关模型的数据吗?

My model has many relationships to other tables/models, if i use contain parameter, for example to order by field in results of one model, all other tables in query results are missing, and must manually written in contain parameter, how can i get all related models data without manually writing it?

 $data = $this->Cv->find('first', 
    array(
        'contain' => array('Cv_experience' => array('order' => 'Cv_experience.start_year desc'))));


推荐答案

听起来您一直在依赖模型的递归属性。一旦使用了包含,那些曾经自动进入的模型就不再起作用了,对吧?

It sounds like you've been relying on the Model's recursive property. Once you use 'contain', those models that used to come in automatically no longer do, right?

答案是,您应该使用递归包含,但不能两者都包含。如果您使用的是包含,则应将递归设置为OFF( $ recursive = -1; )。

The answer is, you should be using recursive OR contain, not both. If you're using contain, you should set recursive to OFF ($recursive = -1;).

就使用哪个而言,强烈建议完全不使用 recursve 。在您的AppModel中将其设置为-1,并在需要任何其他数据时使用可包含行为。对于简单的博客应用程序而言,递归对任何人都是不好的做法,而且我相信它甚至在Cake 3中已被完全删除。

As far as which to use, it's HIGHLY recommended to not use recursve at all. Set it to -1 in your AppModel, and use Containable Behavior any time you want additional data. Recursive is bad practice for anything but a simple blog app, and I believe is even being completely removed in Cake 3.

关于订购的注意事项:您不能通过内置模型订购。 包含会创建多个查询,因此-如果您要尝试执行的操作,则需要使用JOIN。

Note on ordering: You cannot order by a contained model. 'Contain' creates multiple queries, so - if you want to do what you're trying, you'll need to use JOINs.

这篇关于CakePHP使用“包含”查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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