U
U
ukoHka2017-09-26 15:56:52
MySQL
ukoHka, 2017-09-26 15:56:52

How to search by multiple tags in Yii2 ActiveQuery?

There are three tables t1, t2, t3. There is also a join table t123, which stores links between three tables.
We need to pull all records from t3 that match one specific record in t1 and one or more records from t2.
In t123, records are stored as:

id t1 t2 t3
1  1  1  1
2  1  2  2
3  1  2  3
4  1  3  1

It turns out only
t123::find()->joinWith('t3')
                 ->select('t3.id')
                 ->where([
                     't123.t1' => $t1,
               ])->andWhere('in','t2',$t2arr)->column();

In the above example, with t1 =1, t2arr = [2,3] this gives an array [2,3,1], but it is necessary that an empty array was issued, because there are no records in t3 corresponding to both 2 and 3.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2017-09-26
@proudmore

I could be wrong, but probably the problem is that joinWith does a LEFT JOIN by default, and you need InnerJoinWith()
www.yiiframework.com/doc-2.0/yii-db-activequery.ht...
PS. In general, it seems to me that your database structure is not quite right. Will you reveal the names of the tables?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question