Alex Malev
Alex Malev

Reputation: 177

How to refine the query in Yii?

I have 2 tables:

USER id, name, surname

UserUser u_id1, u_id2, date, status

I have here is 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 'name', 'surname' of the table User, if I add a WHERE or Like, writes an error that the table UserUser such columns (name, surname) not found? How do I add these two values ​​to search or return value? I use Yii2.

Upvotes: 4

Views: 126

Answers (1)

Alex Malev
Alex Malev

Reputation: 177

    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]);

Upvotes: 1

Related Questions