M
M
Max Voronov2014-12-05 08:30:42
Yii
Max Voronov, 2014-12-05 08:30:42

How to create a model with dynamic attributes (EAV) in Yii2?

Hello!
I ran into such a problem, I can’t understand how to correctly implement models in Yii 2 for such a database schema:
1499f123be1846d3b9c5f299198af8ed.png
In fact, this is a familiar EAV to everyone - there is an object, the object has properties that are set dynamically.
To implement this task through pure SQL or QueryBuilder will not be any particular problem. But I want to be able to conveniently get and change these dynamic attributes. For example:

...
echo $model->description;       // Получаем свойство "Описание"
$model->name = 'Object #1';    // Задаем новое Имя для объекта
$model->save();
...

Well, ideally, I would also like support for validation.
If someone has already encountered a similar task on Yii2, then tell me how to implement it as competently as possible.
Thanks in advance for any good advice

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mirocow, 2015-10-27
@RavenRage

https://github.com/Mirocow/yii2-eav
This is an improved and working implementation of the russianlagman/yii2-eav module

  • Fixed a number of bugs
  • Added new types of fields
  • Added administration interface

M
Mylistryx, 2014-12-05
@Mylistryx

In the model, implement the magic methods __get(), __set(), __isset() which wrap into functions that work with related objects. I did not check it in Yii2, but in Kohana I did just that in my time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question