Answer the question
In order to leave comments, you need to log in
What pitfalls can be in the implementation of working with array fields in the model?
I needed to work with some attributes as arrays.
For example, there is a list of checkboxes
<?= $form->field($model, 'parents')->checkboxList([1=>'значение 1', 2=>'значение 2', 3=>'значение 3']); ?>
...
public function typeArrayAttributes()
{
return ['parents'];
}
public function __get($name)
{
$value = parent::__get($name);
if(in_array($name, $this->typeArrayAttributes())) {
$value = empty($value) ? [] : explode(',', $value);
}
return $value;
}
public function __set($name, $value)
{
if(is_array($value) && in_array($name, $this->typeArrayAttributes())) {
$value = implode(',', $value);
}
parent::__set($name, $value);
}
...
Answer the question
In order to leave comments, you need to log in
Why not do it within the framework of the validator and static methods of the model? Like in the example of creating a blog in Yii - yiiframework.ru/doc/blog/ru/post.model
Why do __set and __get at all? Why touch the underlying ActiveRecord model?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question