Tuesday, September 29, 2009

Re: Associating model aliases

Hi John,

thank you for your reply.

As stated, is done exactly as in the Cookbook:

<?php
class Message extends AppModel {
var $name = 'Message';
var $belongsTo = array(
'Sender' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Recipient' => array(
'className' => 'User',
'foreignKey' => 'recipient_id'
)
);
}
?>

<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'MessageSent' => array(
'className' => 'Message',
'foreignKey' => 'user_id'
),
'MessageReceived' => array(
'className' => 'Message',
'foreignKey' => 'recipient_id'
)
);

var $hasOne = 'Profile'; // this for the Profile association
}
?>

Best,

Mario


On Sep 29, 12:35 pm, John Andersen <j.andersen...@gmail.com> wrote:
> Please provide information on how you relates the message model with
> the other models?
> Enjoy,
>    John
>
> On Sep 29, 12:52 pm, Mario <mario.calli...@gmail.com> wrote:
>
>
>
> > Hi all,
>
> > probably a not so difficult question, but I am a newbie to CakePHP and
> > would really appreciate some advice.
>
> > I have coded something exactly following the situation described inhttp://book.cakephp.org/view/851/Multiple-relations-to-the-same-model
> > that is: a User model, a Message model, Sender and Recipient aliases.
> > Everything works smoothly.
>
> > Then I have a Profile model one-to-one associated with User, here as
> > well following the Cookbookhttp://book.cakephp.org/view/80/hasOne
> > <?php
> > class User extends AppModel {
> >     var $name = 'User';
> >     var $hasOne = 'Profile';}
>
> > ?>
> > andhttp://book.cakephp.org/view/81/belongsTo
> > <?php
> > class Profile extends AppModel {
> >     var $name = 'Profile';
> >     var $belongsTo = 'User';}
>
> > ?>
> > Relationship working fine except for the following...
>
> > The problem I have is that $this->Message->find('all') in
> > MessagesController doesn't return the Profile data for the Sender and
> > the Recipient. They are aliases of User, so I can expect that maybe
> > some extra code is needed somewhere.
>
> > I tried to modify the Profile model into something like:
> >   var $belongsTo = array(
> >     'User',
> >     'Sender' => array(
> >       'className' => 'User',
> >       'foreignKey' => 'user_id'
> >     ),
> >     'Recipient'...
> >   );
>
> > Doesn't work, half expected it, because it seems more important in
> > this case to declare that the Sender (Recipient) 'hasOne' Profile. But
> > where and how, not having a Sender (Recipient) model?
>
> > Thank you in advance,
>
> > Mario
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: