Wednesday, February 23, 2011

Re: Relations between models

Don't worry too much about the hows and whys. If your models are associated in any way you can reach a distantly related model to run any model method as described earlier. It's a very useful and powerful facility.

Jeremy Burns
Class Outfit

jeremyburns@classoutfit.com
http://www.classoutfit.com

On 23 Feb 2011, at 08:25, Axel wrote:

> Very interesting!
>
> When you speak of $this->Car->Race->Spectator, the "->" arrow can
> represent any association, belongsTo, hasMany, hasOne,..?
>
> I find quite hard to understand how a model can be a property or
> attribute of another object model? The constructing of those model
> classes is still full of opacity for me.
>
> Thank you for your both answers.
>
> On Feb 22, 9:24 pm, dtemes <dte...@gmail.com> wrote:
>> The classregistry way is the one I have seen as response to similar
>> questions.
>>
>> In your example you are talking about races, so probably your Race
>> model has many cars and also has many spectators, in that case models
>> A and B are linked throug model C, so you could code something like:
>>
>> $this->Car->Race->Spectator->find(....
>>
>> Please cakephp gurus out there, correct me if I'm wrong.
>>
>> Regards
>>
>> On 21 feb, 09:48, Axel <the...@gmail.com> wrote:
>>
>>> Hello,
>>
>>> I'm still a bit stuck in the MVC architecture. I've two models A and B
>>> which are not logically linked (let's imagine model A is a racing car
>>> and model B is a spectator of a race), so I haven't built any
>>> association between them. How can I use both models in a function in
>>> model A, I mean what is the best way to load B in A ? I4ve in mind two
>>> solution : first one is loading models in controller, retrieving data
>>> and passing them to function, but it's not controllers thin models
>>> fat. Second one is classregistry loading inside of the model, but
>>> might it be time consuming?
>>
>>> Example :
>>
>>> -------------FIRST example -------------------
>>> AController
>>> {
>>
>>> function AAA($a_id,$b_id)
>>> {
>>> $a=$this->a->find(..conditions => ... a_id....)
>>> $this->loadModel('b');
>>> $b=$this->b->find((..conditions => ... b_id...);
>>> $result=$this->a->funA($a,$b)
>>> }
>>
>>> }
>>
>>> AModel
>>> {
>>> function funA($a,$b)
>>> {Logic, whole stuff, data treatment}
>>
>>> }
>>
>>> ------------SECOND example -----------------------
>>
>>> AController
>>> {
>>> function AAA($a_id,$b_id)
>>> {
>>> $result=$this->a->funA($a_id,$b_id)
>>> }
>>
>>> }
>>
>>> AModel
>>> {
>>> function funA($a_id,$b_id)
>>> {
>>> $a=$this->find(..conditions => ... a_id....)
>>> $b=ClassRegistry::init(a)->find(..conditions => ... b_id....)
>>> }
>>
>>> }
>>
>>> Thank you a lot!
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: