Accessors & Mutators

Hi,

I don’t understand accessors and mutators in CakePHP.

  • For me, accessor with _get is for get a properties with a changement of this value. For exemple ucfirst on the name. And for me, the accessor isn’t use when i create an entity. It’s just use when I get this value of this entity.
  • And for me, mutators with _set is when I set the value on the entity. But for me, the mutator isn’t call when i use $user->name.

But I think I haven’t understand this point. Because when I create a accessor _getName and I create a newEntity with the name value, the name value change with this accessor.

So can you explain me accessors and mutators please ?

Thanks, and sorry for my bad english.

https://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators

Hi,

Thanks for your answer but I read this doc ans I don’t understand totaly.
_setName($name) : It’s for change value after newEntity or parseEntity ?
_getName($name) : It’s for change value after newEntity or parseEntity, and when I do a $user->name
_getNameMaj() : It’s for change value only when i do a $user->name_maj ?

Please can you say to me if It’s that or if I don’t understand ?

Thanks

set methods for setting or changing (writing) values. get methods are for getting (reading) values.

Thank’s for your answer.

But why when I do “newEntity($values) and $this->Model->save($object)”, It’s call function _setName($name) AND _getName($name) ?
I don’t understand why _get is call, because for me, when i do a newEntity() and save(), I just call _set method.

I have also faced this issue and I think there is no need to use _get but when cake build entity before saving the record it uses this.To avoid I have uses in accessor _get like below:

if($this->isNew()) {
return $stardDate;
}
return $sd->setTimezone($timezone)->format(‘m-d-Y H:i:s’);