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
I am kind of lost in relations
kesali
July 2013
I am trying to create an address ralation and things messed up.
Let me explain.
Every user can have multiple addresses and addresses has to have one address_detail.
My relations (shortened)
class Model_User extends \Orm\Model
{
protected static $_properties = array(
);
protected static $_has_many = array('address');
}
class Model_Address extends \Orm\Model
{
protected static $_properties = array(
);
protected static $_belongs_to = array('user');
protected static $_has_one = array('address_detail');
}
class Model_Address_Detail extends \Orm\Model
{
protected static $_properties = array(
);
protected static $_belongs_to = array('user', 'address');
}
my query
$result = Model_User::query()
->where('user_id', $this->user_id[1])
->related('address')
->related('address.address_detail')
->get();
And my foreach loop
foreach ($result as $address) {
print_r($address);
}
I can perfectly get addresses but somehow i could not able to pull address_details. Do i have to create another foreach loop inside of that loop. Tried but no luck.
I am missing something but i could not find out yet.
kesali
July 2013
3 loop and i able to pull my data. Why do i needed to created 3 loop.
foreach($result as $resultobj){
foreach($resultobj->address as $address){
foreach ($address->address_detail as $detail) {
print_r($detail);
}
}
}
Harro
July 2013
Accepted Answer
You can't (directly) see related data, for the same reason you can't see the model object property values directly. Both are constructed dynamically.
You access related objects using the relation name as property: $address->address_detail.
If it's a "one", you directly get the related object. If it's a "many", you get an array of objects.
Harro
July 2013
As to your second question: what did you expect? You are requesting a hierarchy after all...
kesali
July 2013
Thank you man. I should sleep before ask that question.
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
July 2013
kesali
July 2013