Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Creating New Propert from Observer
  • I would like to manipulate certain property in orm model.

    changing created_at to another date time. bit i need created_at in old fashion too. so i created a observer with event after_load. and. my created at field is created_at. i want to do some manipulation in it. and the model contain certain field which is accessible by $model->new_field

    how it is possible. how i create new property in observer and attach to model?
  • You can't without creating a setter method for it.

    The Model uses a data container in combination with magic setters and getters, so if you do $model->new_field, it will create a new property in the "custom data" container, not a new object property.
  • So what should i do?

    Any Help Appreciated
  • Create a setter method in your model:

    public function set_new_field($value)
    {
        $this->new_field = $value;
    }

  • It might be even enough just to define the property:

    public $new_field = null;

    in the class, as property existence will not trigger the magic setters and getters.
  • I do wonder however what your use-case is, and why you can't use the standard custom data values?

    Since

    $model->new_field = 'value';

    simply works?
  • My Application is in Angular js.

    eg:

    There are two table, Employee table, And attendance table.
    when ever a  json response of employee model (to_array). i want each employee's today_present property to true or false. but it is not a field in db, it is a property linked to another table, i have many checking in attendance table to findout whether he is present or not. i think to use after_load event as observer. and when should i put to_array. i want new property too with json.

    I want to assign

    $model->new_field = 'value'; in observer calss
  • The observer is passed the object instance, so you can do that without problems.

    For that you need to define properties in your model class definition, the ORM model has support for custom (= non database) properties.
  • exactly confused,  not succeeded, can you create me a bin for that...
  • You define an Observer method like so:

        public function before_save(\Orm\Model $obj)
        {
        }

    The ORM model object on which the Observer runs is passed on the method in $obj. So you can do stuff like:

        // change a column value
        if ($obj->column == 'A')
        {
            $obj->column = 'B';
        }

        // create some custom value based on object data
       if ( ! empty($obj->updated_at))
       {
            $obj->customvalue = true;
       }
       else
       {
            $obj->customvalue = false;
       }

    If you do this on_load, you can access "customvalue" like any other model property.

    If you want these values to be part of the result of to_array(), you need to tell that method to include custom values too. The prototype of the method is:

        public function to_array($custom = false, $recurse = false, $eav = false)

    so

        $values = $obj->to_array(true);

    will also include your custom values.
  • Dear,

    I have not tested With Custom Value, But Tested With EAV.. Not Working as Expected

    My Main Model is http://bin.fuelphp.com/snippet/view/MC

    My EAV Model is http://bin.fuelphp.com/snippet/view/MD

    response here http://bin.fuelphp.com/snippet/view/ME

    the issue is the eav value is showing in json, only value,, not Attribute
    the key of the value (eg: 25 & 36) is not the attribute, my attribute for 25 is 2 and 36 is 3.. but i getting the key 0 for 25 and 1 for 36..... how do i solve it

    Note:
    My Attribute Field is (int). Which is belong to Modal_Type.
  • Fixed EAV issue, edited packages/orm/classes/model.php and changed array_merge to \Arr::merge() ..

    Well work, fixed
  • Ah, you didn't mention that. EAV is very different from custom values, you can't even mix the two.

    array_merge() indeed renumbers numeric array indexes. Can you tell exactly what you changed, and where? I don't think the design of EAV takes into account attributes can be numeric.

Howdy, Stranger!

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

In this Discussion