Answer the question
In order to leave comments, you need to log in
How to properly use multi-model related method in Yii2?
The question arose - how to correctly use the method associated with several models. There are several options, but I don't know which one is better. I would like to learn how to use OOP correctly in the context of Yii2. If someone is not too lazy to delve into all this, then thank you very much =). So we have:
AR models One, Two, Three
class One extends ActiveRecord
{
// Есть атрибуты - поля в БД: id, category_id
…
public function getTwo()
{
return $this->hasMany(Two::className(), ['one_id' => 'id']);
}
...
}
class Two extends ActiveRecord
{
// Есть атрибуты - поля в БД: id, one_id
…
public function getOne()
{
return $this->hasOne(One::className(), ['id' => 'one_id']);
}
public function getThree()
{
return $this->hasMany(Three::className(), ['id' => 'three_id'])
->viaTable('two_three', ['two_id' => 'id']);
}
...
}
class Three extends ActiveRecord
{
// Есть атрибуты - поля в БД: id, category_id, name, group
…
public function getThree()
{
return $this->hasMany(Three::className(), ['id' => 'two_id'])
->viaTable('two_three', ['three_id' => 'id']);
}
...
}
private $threeByGroup = [];
…
public function threeArrayForGroup($group)
{
if (empty($this->threeByGroup)) {
$this->threeByGroup = ArrayHelper::map(
Three::find()->where(['category_id' => $this->category_id])->all(),
'id', 'name', 'group'
);
}
return isset($this->threeByGroup[$group]) ? $this->threeByGroup[$group] : [];
}
private $threeByGroup = [];
…
public function threeArrayForGroup($group)
{
if (empty($this->threeByGroup)) {
$this->threeByGroup = ArrayHelper::map(
$this->getThree()->all(),
'id', 'name', 'group'
);
}
return isset($this->threeByGroup[$group]) ? $this->threeByGroup[$group] : [];
}
public static $threeByGroup = [];
...
public static function threeArrayForGroup($group, $category_id = false, $two_id = false)
{
if (! $category_id && ! $two_id) {
throw new InvalidParamException('Хотя бы один из двух аргументов должен быть не пустым: $category_id $two_id ');
}
if ($category_id) {
$data = Three::find()->where(['category_id' => $category_id])->all();
} elseif ($two_id) {
$data = Three::find()->joinWith('two')->where(['two.id' => $two_id])->all();
}
$key = $category_id .’_’.$two_id;
if (empty($this->threeByGroup[$key])) {
$this->threeByGroup[$key] = ArrayHelper::map(
$data,
'id', 'name', 'group'
);
}
return isset($this->threeByGroup[$key][$group]) ? $this->threeByGroup[$key][$group] : [];
}
public function threeArrayForGroup($group)
{
$data = Three::find()->where(['category_id' => $this->category_id])->all();
$key = ‘category’;
return Three:: threeArrayForGroup($group, $data, $key);
}
public function threeArrayForGroup($group)
{
$data = $this->getThree()->all();
$key = ‘two_’ . $this->id;
return Three:: threeArrayForGroup($group, $data, $key);
}
public static $threeByGroup = [];
...
public static function threeArrayForGroup($group, $data, $key)
{
if (empty($this->threeByGroup[$key])) {
$this->threeByGroup[$key] = ArrayHelper::map(
$data,
'id', 'name', 'group'
);
}
return isset($this->threeByGroup[$key][$group]) ? $this->threeByGroup[$key][$group] : [];
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question