How to refine the request in Yii?

I have 2 tables:

USER id, first name, last name

UserUser u_id1, u_id2, date, status

I have a model in action USER:

public function getUsers($status)
{
    return $this->hasMany(UserUser::className(), ['u_id1' => 'id'])
        ->where(['status' => $status]);
}

But I need to add the ability to search for "name", "surname" of the User table, if I add WHERE or Like, it writes an error that the UserUser table of such columns (name, surname) was not found? How to add these two values ​​to search or return a value? I am using Yii2.

+4
source share
1 answer
    return $this->hasMany(User::className(), ['id' => 'u_id2'])->viaTable(UserUser::tableName(), ['u_id1' => 'id'],
        function($query) {
            $query->where(['status' => 1]);
        })->andWhere(['like', 'name', $input])
          ->orWhere(['like', 'last_name', $input]);
+1
source

Source: https://habr.com/ru/post/1548037/


All Articles