Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Add extra data to model for view specific data.
  • Is there a way to include additional fields from a query into a model object temporarily. For example calculating a total or something as it relates to the model but having it come from a query you already made vs calculating it each time?

    If this isn't clear I mean like a Select * on a model plus one or two additional fields and then be able to say Model->additional_field in the view.
  • The ORM is not designed as a query builder (it's not the purpose of an ORM), and as a result you're limited to what you can do with it.

    Selecting a subset of columns is technically supported but not advised, since once you query them you can not get the missing columns back anymore, due to the ORM's caching mechanism (subsequent queries for the same iD just return the cached object).

    Expressions where not support until 10 minutes ago, when I added it to the 1.8/develop codebase. If you don't want to run develop code, wait for 1.7.2, which will be released soon.

    This change allows you to do

    Model->select(array(DB::expr('count(*)'), 'countcolumnalias'))->get();

    which will add the custom property 'countcolumnalias' to the object containing the result of the expression.

    Like this, it will only include the PK and the expression, you can also use

    Model->select('*', array(DB::expr('count(*)'), 'countcolumnalias'))->get();

    to add all columns and the expression, or use any list of column names you want. Note that if you do that, ALL PK fields MUST be present in the list.

Howdy, Stranger!

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

In this Discussion