class MainTableSearch extends MainTable
{
public $table_two;
public $table_three
public function search($params)
{
$query = MainTable::find();
$query->joinWith(['table_two']);
$query->joinWith(['table_three']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
....
MainTable.php
class MainTable extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'main_table';
}
public function getTableTwo()
{
return $this->hasOne(TableTwo::className(), ['main_id' => 'id']);
}
public function getTableThree()
{
return $this->hasOne(TableThree::className(), ['main_id' => 'id']);
}
.....
I want to see all data from main table in grid view and if table_two.something or table_three.something doesn’t meet condition to return null in that field.
I tried:
public function getTableTwo()
{
return $this->hasOne(TableTwo::className(), ['main_id' => 'id'])
->andWhere(['table_two.something' => 1])
->orWhere(['table_two.something' => NULL);
}
but that will just remove rows that doesn’t have correlations between main_table and table_two, also tried:
$query->leftJoin('table_two',
'main_table.id = table_two.home_id AND
(table_two.something=1 OR table_two.something = NULL)');
with no luck.
To help you understand this sql works fine:
SELECT * FROM `main_table`
LEFT JOIN table_two ON main_table.id = table_two.home_id
AND (table_two.something=1 OR table_two.something=NULL)
LEFT JOIN table_three ON main_table.id = table_three.home_id
AND (table_three.something=1 OR table_three.something=NULL)
WHERE main_table.user_id = 1
Probably you have to use "with" instead of "joinWith".
[edit]
When you use ‘joinWith’ in the ‘search’ method, it will produce the query that uses ‘JOIN’. And if you set some conditions based on the joined tables, something like ‘table_two.other = xxx’, it may give a result set of records that matches the criteria.
Would you please show us your ‘search’ method as a whole?