Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to implement non_column or "virtual" property?
  • Have seen this references as "non_column" in a post or two, but can find any specific documentation or in the orm package code, using fuel 1.4.  One reference is here:

    http://fuelphp.com/forums/discussion/comment/11267#Comment_11267

    Trying to add "virtual" properties to my models.  A couple examples would be:

    1) have a calculated type field like "name" where its a combo of first_name and last_name set in a observer after retrieve

    2) joining in some columns from a has_one relationship where the parent model stores the id reference and the name property would be a "virtual" reference.


  • HarroHarro
    Accepted Answer
    You can't in 1.4, it is not supported. In 1.5/develop, $model->thisisnotacolumnname works fine.
  • Ok, thanks for the heads up, had seen comments from while back that it was in 1.4 develop so was hoping it made it into the release and I was just missing something

    Will have a look at the 1.5 develop codebase, and try to follow suit w/ some workable implementation in my codebase
  • I think the 1.5/develop ORM package works fine with a 1.4 release core.
  • Looked some through 1.5/develop branch for ORM, not seeing anything explicit with how to define properties that could be "virtual" in nature.

    In the create for intstance, there is the foreach that sets all the properties before the $query->create() call:

           $properties = array_keys(static::properties());
           foreach ($properties as $p)
           {
                  if ( ! (in_array($p, $primary_key) and is_null($this->{$p})))
                  {
                         $query->set($p, $this->{$p});
                  }
           }

    That seems to just grab all the properties, set them.  Then in looking at the model.php __construct() it seems to just loop over all the properties and set them to the appropriate cached/other variable locations.



  • You don't define them. You just use them.

    So if you do $model->someproperty, the model will first check if it's a property (a column). If not, it will check if it's a defined relation. If not, it will create a virtual property for you.

    And no, you can't create this type of property through the constructor, you'll have to use the getters/setters for this feature.
  • Went back through and diffed the whole ORM 1.4 vs 1.5/develop.  The only diff in model.php seems to be couple exception changes and the from_array() population where you can now pass in related objects and have them populated.

    Only other diffs appear to be in all the relation files, changes to the get() methods where change to using 'query' instead of 'find' call

    Any suggestions on where to find this?

    Side note.., Model_Soft is awesome, I have some deleted functionality I had to add since all my models are "soft" in nature, can wait to include this.
  • Then I made a mistake, and the change did make the 1.4 release. ;-)

    As I said, the functionality is in the getter/setter (__get/__set), where it stores the data in the _custom_data structure if it's not a property or a relation.

    It might be possible to alter the constructor to use this too, but we have to be very careful, since its also used by the DB class as_object() method, where PHP will call the constructor internally with a fixed format to create object instances. That feature may not break.

    If you want this please create a feature request on http://github.com/fuel/orm/issues
  • Ok, was thinking there was a built in mechanism in the way you could
    define properties.  Since this is the case I will mess around with
    extending the property def then and add some stuff such that it will do
    some set() calls on these "virtual" columns in my after_retrieve
    observer

    Once I play around with it some, will see how things work and file any appropriate issue/feature-requests

Howdy, Stranger!

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

In this Discussion