Save array to json with getter


#1

Hi all,

With CakePHP 3, i’m trying to save Array to json with setter, but i always get empty value.

My form inputs :

$this->Form->input('tag[1]');
$this->Form->input('tag[2]');

My Setter :

function _setTag($tag) {
    // debug($tag); <- Return always empty value
    return json_encode($tag);
}

Someone can help me ?

Thanks !


#2

You can’t use setters/getters on you entity for this. You need complex types:

You’ll add something like this to your table:

Type::map('json', 'Cake\Database\Type\JsonType');

https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-complex-types