Docs for Model custom Getters & Setters suggest that these can have names that don’t match the properties defined for the Model. In the test system I am working on this appears to be true for custom getters, but not for custom setters. Attempts to use a custom setter that does not share a name with a defined property triggers an EE Exception. Would value thoughts / insights into what I’m doing wrong / how to fix.
Have a simple Model class defined. Saw the notes on custom Getters & Setters in docs and saw a space where one might be useful in a project I’m working on. So I’ve created the custom items in line with the examples in the docs, but when I try to use the setter I get the following error reported by EE.
InvalidArgumentException Caught
No such property: 'last_use' on JCOGSDesign\Jcogs_otp\Models\Otp
The custom items are coded thusly:
protected function get__last_use()
{
return end(json_decode($this->usage, true));
}
protected function set__last_use($value)
{
$usage = json_decode(urldecode($this->usage));
if (count($usage) > 2) {
$temp = array_shift($usage);
} // limit to last three events
array_push($usage,array($value));
$this->setRawProperty('usage',urlencode(json_encode($usage))); // write json encoded version to table
}
The idea is simply to provide a way of accessing the content of property called “usage” which contains a JSON version of an array of items. So the setter simply unpacks the JSON array, appends the incoming value, repacks the JSON and saves the value - along the way it limits the total number of events in the array to max of 3. The getter simply returns the last array from the ones stored.
If I manually populate the db I can use the custom getter and it works as it should. Examples of how I’m using them are:
$member = ee('Model')->get('jcogs_otp:Otp')->filter('member_id', $member_info['member_id'])->first();
$last_event = $member->last_use;
Output: $last_event is a two-value array as expected.
$member = ee('Model')->get('jcogs_otp:Otp')->filter('member_id', $member_info['member_id'])->first();
$member->last_use = array(time(),$validation_result);
$member->save();
Output: No such property: 'last_use' on JCOGSDesign\Jcogs_otp\Models\Otp
I’m doing something wrong … if you can see what it is I’d be very grateful for your thoughts / guidance.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.