Friday, May 22, 2009

Behavior in belongsTo request...

As usual I was not looking for easy ways, and as usual faces challenges not addressed in book.cakephp.org ;-)

Always, the most troublesome part of the project for me is paginate and all that associated with it (sampling, sorting).

And now, I once again stepped on a rake, and the answer to my question - no.

So, what was the challenge:

Model A - hasMany Model B
Model B - belongsTo Model A

Model B - actAs ( 'Image')

Image - a wonderful Yevgenia Tomenko behavior, which is used to link entries in the model with the stored image.

In the case of a request Model B -> ... of any form - behavior of hours worked, and I got me the correct information - in this case, pictures.

However, when the problem began to build a query type

Model B -> Model -> A (in terms of where the sample is on the fields of Model A)

turned out - that the search and pedzhinatsiya work perfectly, but the results of Model B - no trace of the behavior ... I just got a field model, without the right to me «curvature».

No dancing, dancing with Contain, the order request, etc. - No effect. Well the answer is no, and guglegruppe.

And today I found another rake, but with behavior Translate. If you use the instructions and do as they advised - and it works fine, except my favorite pedzhinatsii ...

And it is - sort of fields located in the i18n table - you can not. Not possible. Only those fields that are present in an explicit form. Whew.

Of course, I do not want to say that sorting through the i18n table built to work quickly, or, in principle, be attributable to work. Maybe not. But in this case, if you need some sort of results except for id, created - no chance to use the behavior you do not. It does not work. : (

And if the rake can bypass the second request (first received the list of id Model B, and then to obtain directly from the Model B, all records (this is work behavior). In the second rake, the only way to sort - to dispense with boxed behavior.

But I have the trick ;-)

$results = this->Immovable->Best->find('all',array('conditions' => array('Best.country_id' => $country), 'limit' => 5, 'contain' => array('Immovable' => array('order' => array('Immovable.created' => 'ASC')))));

$result = $this->Immovable->Behaviors->Image->afterFind($this->Immovable, $results, false);

In this case, the results will be applied behavior

Cheers!

Labels: , , , ,