0

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'))));
  • `Cv_experience` => You should stik to the convention: `CvExperience` or stuff will break. PS: You should always mention the exact cakephp version you are using. – mark Jul 20 '14 at 20:26

1 Answers1

0

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.

Dave
  • 28,833
  • 23
  • 113
  • 183
  • but it orders...my problem is that when i use contain i don't get other models data in results. For example CV model is related to CV_education, CV_experience, CV_seminars and so on...i get all data when i don't use contain. Everything is fine. But when i use CV_experience in contain, i get only this model,not Cv_education ..etc., and i don't want to define all other model names in contain, is there a way to get all related data, without defining every related model in contain? – Arturas Grizas Jul 21 '14 at 08:44