CakePHP find with “contain”

十年热恋 提交于 2020-01-07 05:28:19

问题


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

回答1:


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?

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

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.

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.



来源:https://stackoverflow.com/questions/24848551/cakephp-find-with-contain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!