Search with multiple related tables

I have this schema:

My A Model




class A extends \yii\db\ActiveRecord

{	

	public function getB()

	{

		return $this->hasOne(B::className(), ['id' => 'b_id']);

	}

	public function getBLeft()

	{

		return $this->hasOne(BLeft::className(), ['id' => 'b_left_id']);

			->via('B');

	}

	public function getBRight()

	{

		return $this->hasOne(BRight::className(), ['id' => 'b_right_id']);

			->via('B');

	}

	public function getBRight1()

	{

		return $this->hasOne(BRight1::className(), ['id' => 'b_right_1_id']);

			->via('BRight');

	}

	public function getBRight2()

	{

		return $this->hasOne(BRight2::className(), ['id' => 'b_right_2_id']);

			->via('BRight');

	}

	public function getBRight3()

	{

		return $this->hasOne(BRight3::className(), ['id' => 'b_right_3_id']);

			->via('BRight');

	}

}



My A Search Model:




class ASearch extends A

{

	.......

	public function search($params)

	{

		$query = A::find();


		$dataProvider = new ActiveDataProvider([

			'query' => $query,

		]);


		if (!($this->load($params) && $this->validate())) {

			return $dataProvider;

		}

		

		$query->andFilterWhere([

			'id' => $this->id,

		]);


		return $dataProvider;

	}

}



The View:




<?=  GridView::widget([

		'dataProvider' => $dataProvider,

		'columns' => [

			[

				'attribute' => 'bLeft',

				'value' => 'bLeft.id'

			],

			[

				'attribute' => 'bRight',

				'value' => 'bRight.id'

			],

			[

				'attribute' => 'bRight1',

				'value' => 'bRight1.id'

			],

			[

				'attribute' => 'bRight2',

				'value' => 'bRight2.id'

			],

			[

				'attribute' => 'bRight3',

				'value' => 'bRight3.id'

			],

			'id',

		],

	]);

?>



It´s Ok!




+---------+---------+---------+---------+---------+---------+---------+

|   id    |   b.id  |  bL.id  |  bR.id  |  bR1.id |  bR2.id |  bR3.id |

+=====================================================================+

|   A1    |   B1    |   BL1   |  BR1    |  BR11   |  BR21   |  BR31   |

+---------+---------+---------+---------+---------+---------+---------+

| 																	|

|  ...   	...   	...   	...        ...   	...      ...    |

| 																	|

+---------+---------+---------+---------+---------+---------+---------+

|   An    |   Bn    |  BLn    | BRn 	|  BR1n   |  BR2n   |  BR3n   |

+---------+---------+---------+---------+---------+---------+---------+



I need a search form with related field, so, i tried using "$query->joinWith":




class ASearch extends A

{

	public $b;

	public $bLeft;

	public $bRight;

	public $bRight1;

	public $bRight2;

	public $bRight3;

    

	public function rules()

	{

		return [

			[['id'], 'integer'],

			[['b', 'bLeft', 'bRight', 'bRight1', 'bRight2', 'bRight3'], 'safe'],

		];

	}

	.......

	public function search($params)

	{

		$query = A::find();


		$dataProvider = new ActiveDataProvider([

			'query' => $query,

		]);


		if (!($this->load($params) && $this->validate())) {

			return $dataProvider;

		}

		

		$query->joinWith(['bLeft']);

		$query->joinWith(['bRight1']);

		$query->joinWith(['bRight2']);

		$query->joinWith(['bRight3']);

		

		$query->andFilterWhere([

			'id' => $this->id,

			'tbl_b_left.id' => $this->bLeft,

			'tbl_b_right.id' => $this->bRight,

			'tbl_b_right_1.id' => $this->bRight1,

			'tbl_b_right_2.id' => $this->bRight2,

			'tbl_b_right_3.id' => $this->bRight3,

		]);


		return $dataProvider;

	}

}



But i get:




Getting unknown property: common\models\A::b_id



I also tried with "->viaTable"




class A extends \yii\db\ActiveRecord

{	

	......

	public function getBLeft()

	{

		return $this->hasOne(BLeft::className(), ['id' => 'left_id']);

			->viaTable('tbl_b', ['id' => 'b_id']);

	}

	public function getBRight()

	{

		return $this->hasOne(BRight::className(), ['id' => 'right_id']);

			->viaTable('tbl_b', ['id' => 'b_id']);

	}

	.....

}



I thing the problem is [size="2"]bRight1 or [/size][size="2"]bRight2 or [/size][size="2"]bRight3 because i also[/size][size="2"] get the same error when i use only one of:[/size]

    &#036;query-&gt;joinWith(['bRight1']);


	&#036;query-&gt;joinWith(['bRight2']);


	&#036;query-&gt;joinWith(['bRight3']);

What´s wrong?

if i use:




$query->joinWith(['bRight1'], true, 'INNER JOIN');



instead of:




$query->joinWith(['bRight1']);



I got the error:




SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'tbl_b'

The SQL being executed was: SELECT `tbl_a`.* FROM `tbl_a` LEFT JOIN `tbl_b` ON `tbl_a`.`id_b` = `tbl_b`.`id` LEFT JOIN `tbl_b_right` ON `tbl_b`.`b_right_id` = `tbl_b_right`.`id` INNER JOIN `tbl_b` ON `tbl_a`.`id_b` = `tbl_b`.`id` INNER JOIN `tbl_b_left` ON `tbl_b`.`b_left_id` = `tbl_b_left`.`id` LEFT JOIN `tbl_b_right_1` ON `tbl_b_right`.`b_right_1_id` = `tbl_b_right_1`.`id`



I understand this error, but i don`t know how to fix it.

I read some posts about it:

But i can’t find how to use it.