Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM: find('all') returning error
  • I've tried to fetch all posts from the database. If I do
    Model_Post::find(1)
    

    it works out perfectly and returns the information from that post. If I do
    Model_Post::find('all')
    

    I get
    Notice!
    
    ErrorException [ Notice ]: Trying to get property of non-object
    
    APPPATH/views/post/index.php @ line 2:
    
    1: <div id="id_box">
    2: <?=$thought->id;?>
    3: </div>
    Notice!
    
    ErrorException [ Notice ]: Trying to get property of non-object
    
    APPPATH/views/post/index.php @ line 5:
    
    4: <div id="title">
    5: <?=$thought->title;?>
    6: </div>
    Notice!
    
  • The PHP error tells you what is wrong: $thought is not an object. I'm guessing it contains the array of all objects returned and thus is an array and not an object.
  • So I would do Model_Post::find('all')->get()->as_array(); ?
  • On a phone thus keeping it short and can't answer fully. But find(all) returns an array what do you think will happen if you call a method on the output of that? Answer: almost the exact same error but about a method instead of a property. Read the error, understand it and realize PHP doesn't tell you nonsense. And use what you got in the correct manner. Thus find(1) returns an object which you use as such. But find(all) returns an array of objects which you should use as an array before trying to access the objects's properties.
  • So I've tried the following to set the data into an array and pass it onto the view and still no luck.
    $data['post'] = array( Model_Post::find('all') );
    $this->body->response = View::factory('post/all', $data['post']);
    
  • You dont need to set the data into an array, you should be getting an array of objects back. Try
    $posts = Model_Post::find('all');
    
    var_dump($posts); //should be an array of post objects
    
    //traverse these objects like so
    
    foreach($posts as $post){
     echo "post object id: $post->id"; //or something
    }
    
  • I get a DUMP of all the info. I can't select regular things.
    array(1) { [1]=> object(Model_Post)#11 (7) { ["_is_new":"Orm\Model":private]=> bool(false) ["_frozen":"Orm\Model":private]=> bool(false) ["_data":"Orm\Model":private]=> array(5) { ["id"]=> string(1) "1" ["created"]=> string(10) "1313618725" ["title"]=> string(40) "This is the first thought ever made here" ["author"]=> string(4) "Zero" ["content"]=> string(509) "their was a popular post earlier about suicide i just read .. its crazy there was soo much feedback and help from everyone ... it goes to show there was still GOOD people in this fucked up world ... and i thought the world as gone to hell. i am glad to find people that think the same way as me. people i too have fought with depression and suicide. and i wish i had THIS much backing or help. i had to deal with it by my self... you guys def made my night 10 times better .... ONE LOVE ,Brother/SisterHood" } ["_original":"Orm\Model":private]=> array(5) { ["id"]=> string(1) "1" ["created"]=> string(10) "1313618725" ["title"]=> string(40) "This is the first thought ever made here" ["author"]=> string(4) "Zero" ["content"]=> string(509) "their was a popular post earlier about suicide i just read .. its crazy there was soo much feedback and help from everyone ... it goes to show there was still GOOD people in this fucked up world ... and i thought the world as gone to hell. i am glad to find people that think the same way as me. people i too have fought with depression and suicide. and i wish i had THIS much backing or help. i had to deal with it by my self... you guys def made my night 10 times better .... ONE LOVE ,Brother/SisterHood" } ["_data_relations":"Orm\Model":private]=> array(0) { } ["_original_relations":"Orm\Model":private]=> array(0) { } ["_iterable":protected]=> array(0) { } } } 1
    
  • Jay Arias wrote on Wednesday 17th of August 2011:
    I get a DUMP of all the info. I can't select regular things.
    that looks fine to me you have an array with 1 entry. The only accessible part of the ORM model is _data You traverse each one like this
    foreach($posts as $post){
    
     echo "The title of this post is " . $post->title;
    }
    

    of course you wouldn't want to do this in the controller but just to show how its done.

Howdy, Stranger!

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

In this Discussion