Saving an array to database as string


I want to save an array to database field with a conditional relation to another model.

$this->Form->select(‘eav’, $options, [‘label’ => ‘name’, ‘required’ => ‘required’,‘multiple’ => ‘multiple’ ]);

Then I receive the data eav => [‘0’ => ‘1’, ‘1’ => ‘2’]

Now I want to store the array into field as ‘1,2’, then when looking for the field, automaticly bind the related records, like when find you don’t get the string but you receive the array back. I know I can loop through the record in controller and do it manually like that, but I saw somewhere you can do it in model or entity, just not sure where to look for it…

What is the best way to do that, is it entity fields?

You can create an ArrayType database type class which would implode the array when saving to database and explode the string when reading from database. Check the JsonType in core for example.

Then use Table::_initializeSchema() method to set the database type for the eav field. Saving Data - 4.x