Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
As_Object & count(*) - extra fields not in the model
  • Hello I'm trying to return a DB query as a particular object, but I'd like to have extra fields returned in the query that aren't contained by the model. Here's an example:
    $reviews = DB::select('reviews.*', DB::expr('count(*) as count'))
    ->from('reviews')
    ->group_by('thing_id')
    ->as_object('Model_Review')->execute();
    
    Which results in the error:
    PDOException [ HY000 ]: SQLSTATE[HY000]: General error: could not call class constructor
    I want to return all reviews unique to a particular 'thing' but I'd also like to have a count of the number of times that thing has been reviewed. I get an error because count is not in the Review model. One quick hack I did to stop the error was just to add count to the model - obviously this is not right and now I'm paying the price for it. I'm thinking maybe I could create an extra model that contained just count and review_id and then lazy loading the actual review in from the ORM - this doesn't seem right either. Is this something anyone's come across? Thanks Mike
  • An ORM model doesn't support additional properties, it will throw an exeception when you try to set one. Custom property support has been implemented for ORM in 1.4/develop, but not sure if it will fix this case, I haven't checked the constructor code to see if changes are needed there too.

Howdy, Stranger!

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

In this Discussion