@izheme
Познаю мир

Почему Yii2 не разрешает использовать в атрибутах ActiveQueryInterface?

Если есть код в AR
/**
* @return ActiveQuery
*/
public function getLabel()
{
    // какой-то код
}


То получить ActiveQuery можно только так
$AR->getLabel() если обратиться через $AR->label, то ActiveQuery уже не будет, т.к. метод __get BaseActiveRecord.php его не пропустит

Типа так делать нельзя?

UPD. Нашел упоминание в документации.

Note: While this concept looks similar to the object property feature, there is an important difference. For normal object properties the property value is of the same type as the defining getter method. A relation method however returns an yii\db\ActiveQuery instance, while accessing a relation property will either return a yii\db\ActiveRecord instance or an array of these.


Как следствие, такое поведение имеет отношение к любым методам, даже если внутри используются не связи.
  • Вопрос задан
  • 128 просмотров
Решения вопроса 1
@izheme Автор вопроса
Познаю мир
Note: While this concept looks similar to the object property feature, there is an important difference. For normal object properties the property value is of the same type as the defining getter method. A relation method however returns an yii\db\ActiveQuery instance, while accessing a relation property will either return a yii\db\ActiveRecord instance or an array of these.

Yii2 docs

У меня хоть и не связи, но логика та же применяется.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы