Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm
How to access related table id.
Landon
October 2013
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?
Landon
October 2013
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.
Harro
October 2013
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," ";
}
Harro
October 2013
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);
Landon
October 2013
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.
Harro
October 2013
Cool, good to hear that!
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
October 2013
Landon
October 2013