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]
$query->joinWith(['bRight1']);
$query->joinWith(['bRight2']);
$query->joinWith(['bRight3']);
What´s wrong?