I added a leader (int) field to the group table to act as the foreign key
and modified that model class as follows.
    class Group extends AppModel{
        var $name = 'Group';
        var $hasAndBelongsToMany = 'Student';
        var $belongsTo = array ('Leader' => array('className' => 'Student',
'foreignKey' => 'leader'));
    }

Using hasOne cakephp complains but with the code above it's happy and having
the group belong to the leader makes logical sense to me so I am happy too.
Thanks for pointing me in the right direction.

On Thu, Sep 18, 2008 at 10:16 AM, [EMAIL PROTECTED] <
[EMAIL PROTECTED]> wrote:

>
>
> You need another associaton where a Group hasOne (or belongsTo) a
> Student "aliased" as the leader.
>
> For example:
> var $hasOne = array(
>    'Leader' => array(
>        'className'    => 'Student'
>    )
> );
>
> Sometimes I prefer to think in terms of a Group having a leader but
> the database design may be more smooth using belongsTo relationship
> because of where the foreign key goes. In this case hasOne results in
> a Student only ever being able to be the leader of one group. Some
> very driven Student may want to be the leader of two or three groups,
> right? Putting the fk in Group and using belongsTo you would avoid
> this problem but possible create some new ones.
>
>
>
>
>
> On Sep 18, 3:34 pm, "silk.odyssey" <[EMAIL PROTECTED]> wrote:
> > I am trying to get the table associations right for an application but
> > I am not sure how to make it happen with cakephp. I have a "students"
> > table that has a many to many relationship with the table groups. This
> > is straightforward to do but the problem is each group also has to
> > have a leader. The leader of each group is a student also. How do I
> > define the relevant models to make this setup work? This is what I
> > have so far.
> >
> > <?php
> >     class Group extends AppModel{
> >         var $name = 'Group';
> >         var $hasAndBelongsToMany = 'Student';}
> >
> > ?>
> >
> > <?php
> >     class Student extends AppModel{
> >         var $name = 'Student';
> >         var $hasAndBelongsToMany = 'Group';
> >     }
> > ?>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to