I have two parent tables and 1 child table in this format -
A - a_id, a_name (parent) B - b_id, b_name (parent) C - c_id, a_id, b_id
[b]aid and bid are foreign keys in table C.
[/b]
I am trying to create a dataProvider starting from Model A, and want to join C to the picture such that the code looks like
$query->joinWith([B,A&B.C]
But I don’t know how to make C join to both A and B for the given query using Yii2 standards. Any help would be appreciated.
Thanks
alirz23
(Alirz23)
December 9, 2017, 5:32am
2
child table belongs to either a or b or maybe both think of this as parent has many childs and child have one parent in both cases
<?php
class C extends ActiveRecord
{
// ...
public function getA()
{
// has one
}
//...
// ...
public function getB()
{
// has one
}
//...
}
to join A and B with C it should pretty straight forward
C::find()->with(['A', 'B'])->all();
The problem is I have to do a join with either A or B as a parent; since it is a complex db structure. Which is why when i join C to A, i need to make sure the C value is the one corresponding to B as well.
I tried adding a condition
->andWhere([‘like’,‘b. b_id’,‘c. b_id’]);
But this takes c. b_id as a string to compare with directly instead of comparing it as a table attribute.
Any ideas on how to fix this. I am new to yii2.
child table belongs to either a or b or maybe both think of this as parent has many childs and child have one parent in both cases
<?php
class C extends ActiveRecord
{
// ...
public function getA()
{
// has one
}
//...
// ...
public function getB()
{
// has one
}
//...
}
to join A and B with C it should pretty straight forward
C::find()->with(['A', 'B'])->all();
alirz23
(Alirz23)
December 9, 2017, 6:59am
4
how about you join with both and do where condition like so
->andWhere(['c.b_id' => 'b.id', 'c.a_id' => 'a.id'])
how about you join with both and do where condition like so
->andWhere(['c.b_id' => 'b.id', 'c.a_id' => 'a.id'])
This takes b.id and a.id on the RHS on both sides to be strings and not the attribute
Also if i use condition if (‘c.b_iid=b.id’), it works but messes up the count in the dataProvider.
alirz23
(Alirz23)
December 10, 2017, 6:30am
6
I don’t understand what you mean by not the attribute
if you need to filter c by some id you can add another where condition
->andWhere(['c.id' => $someId])