Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I am kind of lost in relations
  • 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.

  • 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);
    }
    }
    }
  • HarroHarro
    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.
  • As to your second question: what did you expect? You are requesting a hierarchy after all...
  • Thank you man. I should sleep before ask that question. 

Howdy, Stranger!

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

In this Discussion