Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
how to loop the result..
  • I feel like spamming the forum but here it goes.
    uhh..how do I loop the result? $messages = Model_Message::find()->where('request_id', 1)->get(); foreach($messages as $message)
    {
    echo $message->message.'<br />';
    } that doesnt work? I was under the impression that model_message will return model_messages and I can just loop through, I found a lot of info on how to get the data but not to loop/manipulate it. However I can only find 1 where in the database there are 3 of them.
  • Just a stab in the dark here but couldn't you do something like:
    $messages = Model_Message::find()->where('request_id', 1)->get()->as_array();
    

    Not used the Orm package that much.
  • nope, it returns an array so it threw a PHP ERROR saying : ErrorException [ Error ]: Call to a member function as_array() on a non-object
  • Ah yeah it's an array of objects that gets returned when using Orm isn't it. The Orm maps a table row to an object I think. Sorry did say I hadn't used it much and it was a stab in the dark. Last go just trying to help. Wouldn't it be something like:
    foreach($messages->message as $message)
    &#123;
    echo $message->message.'<br />';
    }
    
  • no worries Phil, hopefully both of us can learn something out of it :D
    that wouldn't work either $messages is an array contains 1 message, if I use DB::select() I can get it all, but not through ORM, I only get 1 message
  • lol yeah. So if you do a echo '<pre>';
    print_r( $messages);
    echo '</pre>'; you only see results for 1 hmm strange. post your print_r($messages). What are we missing??? Will have a look tomorrow as it's 5:45am here in the UK and I haven't been to bed yet as I been trying to make some progress on our CMS. It's all getting blurred now lol.
  • The Orm maps a table row to an object I think.
    That's basicly what most Orms do, especially those like ours that implement (parts of) the active record pattern.
    $messages = Model_Message::find()->where('request_id', 1)->get()->as_array();
    This could never ever work. The get() method returns an array of instances of Model_Message and you can't call the as_array() method on an array. The as_array() method is meant to turn a single object into an array. But that wouldn't help shohyuken even if it did work. The Orm returns an array as I stated thus should loop just fine, and it does as shohyuken demonstrated. Even if you were to loop an object it would work fine btw, it would just loop over the properties and relations.
    The problem seems to be that there's only 1 instance returned when you believe it should be three, which has nothing to do with looping. @shohyuken
    Could you try to do this:
    echo Model_Message::find()->where('request_id', 1)->get_query();
    
    That should return the query the Orm generates, post both that and the query you're doing yourself and the output both generate when you run them manually. Use scrapyrd.com to post the output.
  • here is the output, I give an extra info just to give you the idea the data i am handling:
    http://scrp.at/9d
  • Jelmer Schreuder wrote on Monday 18th of July 2011:
    That's your problem, your primary key isn't unique. It has to be, otherwise the ORM can't work with it. The column your defining as your primary key, must also be the real primary key in the database.

    my primary key is unique, as a matter of fact, I didn't define any primary key, it's an empty array, because the table doesn't have any? at least in the Message model, or are you referring to another model?
  • my primary key is unique, as a matter of fact, I didn't define any primary key, it's an empty array, because the table doesn't have any?
    Which is the problem, the Orm needs a primary key to distinguish one row from the next, without a primary key the Orm won't work. And no the lack of one can't be supported, ever.
  • That's your problem, your primary key isn't unique. It has to be, otherwise the ORM can't work with it. The column your defining as your primary key, must also be the real primary key in the database.

Howdy, Stranger!

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

In this Discussion