F
F
Flaker2014-07-15 15:36:42
Yii
Flaker, 2014-07-15 15:36:42

(Yii2) Why is the result of $UserModelInstance->friends different from $UserModelInstance->getFriends()?

The user model has a getFriends() method

public function getFriends()
    {
        return $this->hasMany(static::className(), ['id' => 'id'])
            ->viaTable('{{%user_friends}}', ['friend_id' => 'id']);
    }

Since the Model class inherits from the Component class, in which get is overridden, then, in theory, $UserModelInstance->friends should return the same as $UserModelInstance->getFriends() .
But $UserModelInstance->friends returns an array of User models, and ->getFriends() returns object(yii\db\ActiveQuery)
PS property friends in the User model does not.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question