Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
What's the best way to return one row using a Model?
  • Using Models and the DB class, what's the best way to return an array. It's fetching one row from the database using an ID, so I only ever want to use one row.
    $return = $query->execute();
      
    $return = $return->as_array();
      
    return $return[0];
    

    That's what I have now, which obviously isn't great, but I don't want an array that looks like this:
    Array (
    
        [0] = Array (
            ...
        )
    
    )
    
  • If the database output will be 1 row (selection on primary key or LIMIT 1) you can use:
    return $query->execute()->current();
    

    which will return and array like:
    Array(
        ['username'] => 'Peter',
        ['country'] => 'The Netherlands'
    )
    
  • That's perfect Peter. Is that in the Docs anywhere, I couldn't see it?
  • Thomas Edwards wrote on Friday 25th of November 2011:
    That's perfect Peter. Is that in the Docs anywhere, I couldn't see it?

    It's under "Escaping": http://docs.fuelphp.com/classes/database/usage.html#/escaping

Howdy, Stranger!

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

In this Discussion