Answer the question
In order to leave comments, you need to log in
Why is the query returning an empty model?
I make a request
ArticleAuthor::find()->where(['article_id' => $id])->all();
(new Query())->select('*')->from(ArticleAuthor::tableName())->where(['article_id' => $id])->all();
/**
* Автор статьи
*
* @property integer $id
* @property integer $article_id
* @property string $lastname
* @property string $firstname
* @property string $middlename
* @property string $email
* @property-read string $fullname
*/
class ArticleAuthor extends ActiveRecord {
public static function tableName() {
return '{{%article_author}}';
}
public function getArticle() {
return $this->hasOne(Article::tableName(), ['id' => 'article_id']);
}
public function __get($name) {
switch ($name) {
case "fullname": return "$this->lastname $this->firstname $this->middlename";
}
}
public function rules() {
return [
[['lastname', 'firstname', 'middlename', 'email'], 'required', 'message' => 'Это поле не может быть пустым']
];
}
public function attributeLabels() {
return [
'lastname' => 'Фамилия',
'firstname' => 'Имя',
'middlename' => 'Отчество',
'email' => 'Электронная почта',
];
}
}
Answer the question
In order to leave comments, you need to log in
Apparently the problem is in the magic method __get() .
If you need to get the full name as a property of the class, this is done like this.
public function getFullname() {
return $this->lastname.' '.$this->firstname.' '.$this->middlename;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question