Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to access related table id.
  • I have this query to the ORM with related table "team_members".
    [code]$requests = Model_User::find('all', array(
    'related' => array(
    'team_members' => array(
    'where' => array(
    array('subteam_id', $subteam_id),
    array('approved', 0),
    ),
    ),
    ),
    ));
    [/code]
    On the page I am trying to obtain the id from team_members like this:
    $request->team_members->id but it returns a non-object error.

    I was wondering how I can get this id?


  • Calling <?=print_r($request->team_members)?> returns an array with the data that I am looking for, but I do not know how to access it.

    This is the array that the call produces:

    Array ( [6] => Model_Team_Member Object ( [_is_new:protected] => [_frozen:protected] => [_data:protected] => Array ( [id] => 6 [user_id] => 1 [subteam_id] => 2 [approved] => 0 ) [_custom_data:protected] => Array ( ) [_original:protected] => Array ( [id] => 6 [user_id] => 1 [subteam_id] => 2 [approved] => 0 ) [_data_relations:protected] => Array ( ) [_original_relations:protected] => Array ( ) [_reset_relations:protected] => Array ( ) [_view:protected] => [_iterable:protected] => Array ( [id] => 6 [user_id] => 1 [subteam_id] => 2 [approved] => 0 ) ) ) 1

    Sorry I don't know how to use code blocks properly on this forum as I am new. If anyone would like to let me know that would be great. Thanks.
  • HarroHarro
    Accepted Answer
    user->team_members is a has_many relation, you'll have more then one id value (one for every related team member).

    So $request->team_members is an array of objects:

    echo "team member id's: ";
    foreach ($request->team_members as $member)
    {
         echo $member->id," ";
    }
  • Since the array index is the same as the primary key of the object in the array, a quick trick to get all PK's out is to use:

    array_keys($request->team_members);
  • Thanks for taking the time to help me out! I am really loving this framework. Just switched from Codeigniter and ORM/Oil is saving me so much time.
  • Cool, good to hear that!

Howdy, Stranger!

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

In this Discussion