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.
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.
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)
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.
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.