Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem fetching result with relations
  • Hi all, I've tried to find similar posts to get my problem solved but unfortunately I was not able to find anything that explains my "problem". Controller:
    class Controller_Plan extends Controller_Common {
     
     public function action_show() {
      $plan = Model_Plan::find('all', array(
       'related' => array('planrecords'),
       'where' => array(
        array('id', '=', Uri::segment(3)),
       ),
      ));
     }
     
    }
    

    Model:
    class Model_Plan extends Orm\Model {
     
     protected static $_has_many = array('planrecords'=> array(
       'key_from' => 'id',
       'key_to' => 'plan_id',
     ));
     
     protected static $_observers = array(
      'Orm\\Observer_CreatedAt' => array('before_insert'),
      'Orm\\Observer_UpdatedAt' => array('before_save'),
     );
     
    }
    

    Model:
    class Model_Planrecord extends Orm\Model {
     
     protected static $_belongs_to = array('plan');
     
    }
    

    Extract of the result:
    array(1) {
      [1]=>
      object(Model_Plan)#14 (7) {
        ["_is_new":"Orm\Model":private]=>
        bool(false)
        ["_frozen":"Orm\Model":private]=>
        bool(false)
        ["_data":"Orm\Model":private]=>
        array(6) {
          ["id"]=>
          string(1) "1"
          ["created_at"]=>
          string(19) "2011-04-25 09:35:13"
          ["updated_at"]=>
          string(19) "2011-04-25 09:35:13"
    [...]
    
        ["_data_relations":"Orm\Model":private]=>
        array(1) {
          ["planrecords"]=>
          array(2) {
            [1]=>
            object(Model_Planrecord)#15 (7) {
              ["_is_new":"Orm\Model":private]=>
              bool(false)
              ["_frozen":"Orm\Model":private]=>
              bool(false)
              ["_data":"Orm\Model":private]=>
              array(13) {
                ["id"]=>
                string(1) "1"
                ["created_at"]=>
                string(19) "2011-04-25 09:17:58"
                ["updated_at"]=>
                string(19) "2011-04-25 09:17:58"
                ["plan_id"]=>
                string(1) "1"
                ["user_id"]=>
                string(1) "1"
                ["pos"]=>
                string(1) "1"
                ["task_name"]=>
                string(46) "Initiate Code-Freeze for main repository"
    [...]
    

    Well, the "problem" is: how can I access those objects? Any try with
    $plan->planrecords;
    
    failed. Additionally I am a bit surprised that the result is sort of an array. Could anyone help me? Maybe my queries are wrong? Thanks in advance,
    Bastian
  • You are requesting "all" objects, which means that 0 to many objects are returned and thus an array with 0 to many objects is returned. In this case it's just one, but it might just as well be 0 or a 100. $plan would be an object (and always just 1 object) if you'd do the following:
    public function action_show() {
      $plan = Model_Plan::find('first', array(
       'related' => array('planrecords'),
       'where' => array(
        array('id', '=', Uri::segment(3)),
       ),
      ));
     }
    

    Which is also a lot more logical as you're requesting it by ID and as such there should never be more than 1 result.
    Now $plan->planrecords will work.
  • Hi Jelmer, you are absolutely right...! I didn't see that it was still 'all' - obviously. Now it works as expected and I am happy! Thank you for your quick help... ;-) Regards,
    Bastian

Howdy, Stranger!

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

In this Discussion