yii2,跳过查询

时间:2018-05-08 13:14:11

标签: yii2

无法找到有关如何执行此操作的任何示例,但我正在构建一个查询,其中可能发生列为空,如果为空则跳过此行。

当翻译没有任何值(为空)时应该跳过

  $query = (new \yii\db\Query())
  ->select(['a.id',
  'b.filter_item_id',
  'c.id as fid',
  'c.filter_id',
  'd.translation'//skip when this has no value
  ])
  ->from(['a' => 'product'])
  ->leftJoin( ['b' => 'product_filter_item'] ,
               'b.product_id = a.id ')
  ->leftJoin( ['c' => 'filter_item'] ,
               'c.filter_id = b.filter_item_id ')
  ->leftJoin( ['d' => 'filter_item_translation'] ,
               'd.filter_item_id = c.id AND d.attribute = "name" AND 
                d.language = "en"')
  ->all();

1 个答案:

答案 0 :(得分:0)

您可以在条件中添加join filter_item_translation

$query = (new \yii\db\Query())
  ->select(['a.id',
  'b.filter_item_id',
  'c.id as fid',
  'c.filter_id',
  'd.translation'//skip when this has no value
  ])
  ->from(['a' => 'product'])
  ->leftJoin( ['b' => 'product_filter_item'] ,
               'b.product_id = a.id ')
  ->leftJoin( ['c' => 'filter_item'] ,
               'c.filter_id = b.filter_item_id ')
  ->leftJoin( ['d' => 'filter_item_translation'] ,
               'd.filter_item_id = c.id AND d.attribute = "name" AND 
                d.language = "en" and d.translation != ""')
  ->all();