Hello
I’m using Yii2 for a while now, but the following issue confuses me:
I Have three models Computer
, ComputerSearch
and Mainboard
The Mainboard
has a 1:1 relation with Computer
.
In Computer
I have the following Code:
public function getMainboard(){
return $this->hasMany(Mainboard::className(), ['computer_id', 'id']);
}
In the view I have a table, which should show columns of Computer
(id, name and serial_number) and the type of the mainboard (mainboard.type), which is stored in the mainboard table, so I join it as following:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => null,
'columns' => [
'id',
'name',
'mainboard.type',
'serial_number',
],
]);
This works so far and there is nothing wrong.
The problem comes now:
The user also needs to filter/search for the type
-field from the Model Computer
.
Because of that, I have to join with the mainboard
relation in the search()
method of ComputerSearch
, which looks as following:
public function search($params) {
$query = Computer::find();
$query->innerJoinWith('mainboard');
//...
var_dump($dataProvider[0]->mainboard); //first dump
var_dump($query->createCommand()->rawSql); //second dump
exit;
return $dataProvider;
}
the result of the “first dump” is null, no matter which index of the $dataProvider I take. According to the database every Computer
has a Mainboard
. If I copy the result of the “second dump” into the sql and execute it manually I get a correct result, and - apparently - every computer record has a mainboard record.
When the related mainboard
Model is not in the $dataProvider
, it will show “(not set)” in the Gridview for the mainboard.type
column.
I found out, that the related mainboard
Model is set if I do the join manually by $query->join()
instead of $query->innerJoinWith()
, but I don’t like this workaround.
I think this is an issue from the yii framework.
Somebody else knows this issue or can somebody explain me, why it doesn’t work with $query->innerJoinWith()
?