# Accessors & Mutators

**URL:** <https://discourse.cakephp.org/t/accessors-mutators/3423>\
**Category:** Need Help\
**Created:** [October 31, 2017, 8:50am UTC](https://discourse.cakephp.org/t/accessors-mutators/3423 "2017-10-31T08:50:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barbierp](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@barbierp](https://discourse.cakephp.org/u/barbierp)\
**Post date:** [October 31, 2017, 8:50am UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/1 "2017-10-31T08:50:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 1, 2017, 2:52pm UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/2 "2017-11-01T14:52:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barbierp](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@barbierp](https://discourse.cakephp.org/u/barbierp)\
**Post date:** [November 1, 2017, 5:50pm UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/3 "2017-11-01T17:50:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 4, 2017, 9:51am UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/4 "2017-11-04T09:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barbierp](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@barbierp](https://discourse.cakephp.org/u/barbierp)\
**Post date:** [November 13, 2017, 10:22am UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/5 "2017-11-13T10:22:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skumar](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@skumar](https://discourse.cakephp.org/u/skumar)\
**Post date:** [May 29, 2018, 12:44pm UTC](https://discourse.cakephp.org/t/accessors-mutators/3423/6 "2018-05-29T12:44:36Z")

</div>

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’);
