Monday, February 10, 2014

Using ORM Entity mutators to change value doesn't work

Hi everyone,

I'm very excited wit new CakePHP 3 ORM! 
Now I'm playing with accessors and mutators but I have issues trying to change the value to save on db.

Here is my Entity

<?php

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Author extends Entity {

    protected $_accessible = ['*' => true];

    public function getName($name) {
        return str_replace(',', ' ', $name);
    }

    public function setName($name) {
        $name = str_replace(' ', ',', $name);
        return $name;
    }

    public function getNameUpper() {
        return strtoupper($this->_properties['name']);
    }

}

Everything works except for Author::setName().  When I save from controller

public function saveAuthor() {
    //debug($this->request->data());
    $data = ['name' => 'John Smith'];
    $authors = TableRegistry::get('Authors');
    $author = $authors->newEntity($data);

    if (!$authors->save($author)) {
        die("error");
    }
}

setName method is called and return the $name to save but in database the name doesn't reflect the changes done in setName.

setName returns 'John,Smith' but in db I have 'John Smith'.

Have I misunderstood how mutators work?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: