Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
_belongs_to and _has_many
  • Hello! I'm having a question about the _belongs_to/_has_many relationship. I have two simple models User and Team with the relationship that a User belongs to a Team and a Team has many Users. Simple enough right? In the team view I can loop through the related users with
    foreach ($team->users as $user) ...
    

    however I can not get access to the Team name from the User view with
    $teamname = $user->team->name;
    

    I feel like I have tried everything at this point, is the relationship not supposed to work this way?
  • Relations are always array's, even it there is only one object in them. So try
    // get the first array element
    $team = reset($user->team);
    
    echo $team->name;
    
  • Got an idea from this, how does fuel support intra table relations, like An employee may have many employee's under him ??
  • Harro Verton wrote on Monday 26th of September 2011:
    Relations are always array's, even it there is only one object in them. So try
    // get the first array element
    $team = reset($user->team);
    
    echo $team->name;
    

    Unfortunately I've already tried that, the problem is that the return result is a string (the id of the team) and not an array which leads me to believe that something is wrong with my relation. Care to take a look? I didn't follow the default setting to map the team to team_id but this has been overridden correctly I think. User:
    protected static $_belongs_to = array(
        'team' => array(
            'key_from' => 'team',
            'model_to' => 'Model_Team',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => false,
        )
    );
    

    and Team:
    protected static $_has_many = array(
        'users' => array(
            'key_from' => 'id',
            'model_to' => 'Model_User',
            'key_to' => 'team',
            'cascade_save' => true,
            'cascade_delete' => false,
        )
    );
    

    Edit: I've also created an index and foreign key constraint on the team in the users table.
  • If the foreign key is 'team_id', you shouldn't use 'team' in your relationship definition. If the key is 'team', you have the problem that the name of the relation is the same as a column name, so $object->team will return the column value, and will not fetch the related object.
  • Harro Verton wrote on Monday 26th of September 2011:
    If the key is 'team', you have the problem that the name of the relation is the same as a column name, so $object->team will return the column value, and will not fetch the related object.

    This one did the trick, thanks! Makes sense also. Just defaulted everything to User
    _belongs_to = array('team');
    

    and Team:
    _has_many = array('users')
    

    along with renaming the team attribute in the users table to 'team_id'. Is there any way of marking a thread as solved?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion