D
D
DarkByte20152017-10-19 17:09:20
Yii
DarkByte2015, 2017-10-19 17:09:20

Why is the query returning an empty model?

I make a request

ArticleAuthor::find()->where(['article_id' => $id])->all();

It returns me an array with one element, but all fields are NULL. This record exists in the database and its fields are filled. id is correct. I checked everything and debugged it 100500 times already. At the same time, for example, such a query is executed normally and returns non-empty data (in an array):
(new Query())->select('*')->from(ArticleAuthor::tableName())->where(['article_id' => $id])->all();

Help me please! I can't figure out what's going on here!
PS PHP 5.6, Yii2.
upd. Here's another if you need the model itself

/**
 * Автор статьи
 *
 * @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

[[+comments_count]] answer(s)
S
Stanislav, 2017-10-19
@DarkByte2015

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;
    }

To get the property, call $object->fullname, $object is an instance of the ArticleAuthor class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question