Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to change query results from protected to public
  • Using the ORM (or query builder) results in something like this below.  I'm trying to step through the results within the code but the names are throwing me for a loop.  ie., "[_query:protected]" and "[_data:protected]"
    Any ideas?  Would like to just get this to output : print $query->_result:protected[0]['id'];

    Fuel\Core\Database_Result_Cached Object
    (
    [_query:protected] => SELECT * FROM `meta_data`
    [_result:protected] => Array
    (
    [0] => Array
    (
    [id] => 1
    [event_id] => 4
    [user_name] => frank
    )

    )

    [_total_rows:protected] => 1
    [_current_row:protected] => 0
    [_as_object:protected] =>
    )


  • "Any ideas? Would like to just get this to output : print $query->_result:protected[0]['id'];"

    $result = $query->execute()->as_array();
    print $result[0]['id'];

    ?
  • How did you right out the function you are calling to retrieve your results.

    If its a custom function start it off with

    public static function your_functionname

  • If $query is Fuel\Core\Database_Result_Cached Object, use $query->as_array() to get _result variable as array :-)
  • p.s. just to be clear, this has nothing to do with ORM, this is a resultset of a DB query.
  • I have the function set to public static the query set to as_assoc().  I guess my question is, how do I traverse the result?


    The query:
    $query = DB::query('SELECT * FROM `twitter_metas`')->as_assoc()->execute();


    The Result:
    Fuel\Core\Database_Result_Cached Object
    (
    [_query:protected] => SELECT * FROM `meta_data`
    [_result:protected] => Array
    (
    [0] => Array
    (
    [id] => 1
    [event_id] => 4
    [user_name] => frank
    )

    )

    [_total_rows:protected] => 1
    [_current_row:protected] => 0
    [_as_object:protected] =>
  • Oh yeah, here's how I'm getting the result from above ...right below the query:

    print "<pre>";
    print_r ($query);
    exit;
  • How would you traverse any other result?

    foreach ($query as $row)
    {
        // do something with $row
    }

    The object implements Traversable and ArrayAccess so you can treat it like it was an array.
  • How about accessing a value directly without a foreach?

    ie.,
    print $query->id;
    print $query['id'];

  • You can't, it's a row collection (even if there is only one row).

    If it contains only one record, you can use $query->current() to get it. On the result of that you can use ['id'] to access individual fields of the row.
  • Ahh, ok.  Thanks for your help!

Howdy, Stranger!

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

In this Discussion