[英]Yii2 get data with join table
I want to get all data from main table with left join relation with its own conditions 我想从具有左联接关系的主表中获取所有数据,并具有自己的条件
MainTableSearch.php MainTableSearch.php
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 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'])->andWhere(['table_two.something' => 2]);
}
public function getTableThree()
{
return $this->hasOne(TableThree::className(), ['main_id' => 'id'])->andWhere(['table_three.something' => 2]);
}
I want to see all data from main table in grid view and if table_two.something or table_three.something doesn't meet requirement to return null in that field. 我想在网格视图中查看主表中的所有数据,并且如果table_two.something或table_three.something不满足在该字段中返回null的要求。
I also tried 我也试过
return $this->hasOne(TableThree::className(), ['main_id' => 'id'])->andWhere(['table_three.something' => 2])->orWhere(['table_three.something' => NULL]);
[SOLVED] [解决了]
$query->leftJoin('table_two','main_table.id = table_two.main_id
AND (table_two.something=1 OR table_two.something IS NULL)');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.