A
A
Alexey2014-07-11 11:02:29
MySQL
Alexey, 2014-07-11 11:02:29

How to refine a query in Yii?

I have 2 tables:
USER
id, name, surname
UserUser
u_id1, u_id2, date, status
I have this action in the USER model:

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

But I also need to add the ability to search for name, surname from the User table, if I add WHERE or Like, it
writes an error that such columns (name, surname) were not found in the UserUser table?
How do I add these 2 values ​​to the search or return value?
I am using Yii2.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2014-07-11
@azovl

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', 'surname' , $input]);

F
Flaker, 2014-07-11
@Flaker

Try like this:

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

Official Guide Info:
https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question