Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Accessing different parts of ORM object
  • Hi, I'm new to ORM (as well as MVC and PHP frameworks too). I love the ORM concept but i am still not very good with it. I have a query like so:
    $projectdata = Model_Websites::find('all', array(
       'where' => array('status' => 1),
       'related' => ('kr_website_data'),
       'group_by' => ('kr_website_data.parent'),
       'order_by' => ('hostname'),
    ));
    

    I want to associate the parent of "kr_website_data" to the primary key of "kr_websites". That seems to work fine! Now I have a foreach in a view going and I need to find out the parent. If I print the return I get this object:
    Model_Websites Object
    (
        [_is_new:Orm\Model:private] => 
        [_frozen:Orm\Model:private] => 
        [_data:Orm\Model:private] => Array
            (
                [id] => 4
                [status] => 1
                [created] => 1317250160
                [hostname] => test.com
            )
    
        [_original:Orm\Model:private] => Array
            (
                [id] => 4
                [status] => 1
                [created] => 1317250160
                [hostname] => test.com
            )
    
        [_data_relations:Orm\Model:private] => Array
            (
                [kr_website_data] => Array
                    (
                        [4] => Model_Websitedata Object
                            (
                                [_is_new:Orm\Model:private] => 
                                [_frozen:Orm\Model:private] => 
                                [_data:Orm\Model:private] => Array
                                    (
                                        [id] => 4
                                        [parent] => 6
                                        [created] => 1317250164
                                    )
    
                                [_original:Orm\Model:private] => Array
                                    (
                                        [id] => 4
                                        [parent] => 6
                                        [created] => 1317250164
                                    )
    
                                [_data_relations:Orm\Model:private] => Array
                                    (
                                    )
    
                                [_original_relations:Orm\Model:private] => Array
                                    (
                                    )
    
                                [_iterable:protected] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [_original_relations:Orm\Model:private] => Array
            (
                [kr_website_data] => Array
                    (
                        [0] => 4
                    )
    
            )
    
        [_iterable:protected] => Array
            (
                [id] => 4
                [status] => 1
                [created] => 1317250160
                [hostname] => test.com
            )
    
    )
    

    I don't understand how to access certain areas, such as "_data_relations:Orm\Model:private". Im trying to get the parent basically! Any help appreciated, loving FuelPHP!
  • foreach ($projectdata->kr_website_data as $website_data){
    $key = $website_data->id;
    $parent_key = $projectdata->id;
    }
  • Relations are available as object properties, in this case it'd be $model->kr_website_data which will give you the array. Or $model->kr_website_data[4] which will give you the 1 instance in there, but you'd have to know the instance's ID for that of course.
  • Thanks Jelmer. I think the problem I am having is finding the initial ID. Having one of those moments were nothing is coming to me. I can get the initial ID by doing this:
    $key = key($model->kr_website_data);
    $parent = $model->kr_website_data[$key]['id'];
    

    This seems a bit 'hacky' though. Does anyone know if there's a better approach or if that is acceptable? Thanks

Howdy, Stranger!

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

In this Discussion