Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How can I return a calculated field in my model?
  • Hello,

    I have 5 fields in my model and want to return only one based on who is logged in.
    I want to return fld1,fld2,fld3,fld4,fld5 as fld

    Can my model handle this or must I use DB:Select?

    Thanks
  • If you're talking about ORM, An ORM model object is an exact representation of a single record.

    You are not supposed to SELECT columns (although there is limited support), and there is therefore also limited support for using an alias in a select.

    You can run a DB::select(), and use as_object('Model_Mine') to have the result returned as instances of Model_Mine. It is strongly suggested to do a "SELECT *, fld5 as fld" to make sure the object is fully populated. This avoids ORM issues when the object is re-used elsewhere.

    If you're talking about just a model class, you're in control of it's implementation.
  • I tried just adding a public $price to the orm model.

    but when I reference it by $row->price, I get nothing.
    I did this before in yii, but am unsure if the syntax is the same.
  • It doesn't work that way, ORM models don't use traditional class properties.

    You have to use assignment ($model->var = $value) or the set() method to set a custom property.

    This also means you can not pre-define them, unless you create it in the class constructor.
  • Sorry I am being so think on this.

    Can I assign the price in my model using pose_find event?
    I tried adding it in the results array, but it is not working.

    basically for each record found, I need to have a price field that contains one of the 5 price fields in the model based on the user logged in.
  • pose_find event?

    If it is a custom field based on data in the original record, I would use an "after_load" observer that sets it.
  • It's an observer you will have to write, as it is custom logic. It's only a few lines of code, and you can look at the pre-defined observers to see how they work and how you should create one.

    Observers are great for performing actions to each ORM object of a specific class, before or after load, before or after save.

Howdy, Stranger!

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

In this Discussion