I am trying to create a custom datatype in cakephp 3 for multi select dropdown. I have a multiple => true
control in my form:
- PHP
- HTML
- CSS
When I submit this form, I get the value for that control as an array (0 => PHP, 1 => HTML
), that is fine, Now I want to save these value in string format like this PHP,HTML
and retrieve back as an array again.
Saving of this input is going well but data is retrieved in the form of string
only. For this I have followed this answer and created a custom datatype:
class MultiSelectType extends Type
{
public function toPHP($value, Driver $driver)
{
if (!$value) {
return null;
}
return explode(',', $value);
}
public function marshal($value)
{
return explode(',', $value);
}
public function toDatabase($value, Driver $driver)
{
return implode(',', $value);
}
public function toStatement($value, Driver $driver)
{
if ($value === null) {
return PDO::PARAM_NULL;
}
return PDO::PARAM_STR;
}
}
How do I modify this code
to get
array
back so that my form will automatically select values from mutiple select box.to get
string
back whenever I want to show that value as a string.
1:n
, or probably bettern:m
related associations? – ndm