如何在ArrayHelper Yii2中传递两个不同的查询数据?

时间:2015-07-30 06:03:34

标签: php mysql yii2

我不知道该怎么做但无论如何我想在同一个数组中为我的过滤器传递这两个查询。

public function getName(){

查询:

  

1

$s_data = (new \yii\db\Query())
->select(["CONCAT(stu_first_name,' ',stu_last_name) as value","us.user_id as id"])
->from('stu_info si')
->join('join','stu_master sm','si.stu_info_id = stu_master_stu_info_id')
->join('join','users us','sm.stu_master_user_id = user_id')
->join('join','lib_borrow_transaction lbt','us.user_id = lbt_holder_id')
->where('us.is_block = 0')
->andWhere('lbt.lbt_holder_type = "S"') 
->andWhere('lbt.is_status = 0')
->andWhere('lbt.lbt_holder_id = us.user_id')
->all();
  

2

e_data = (new \yii\db\Query())
->select(["CONCAT(emp_first_name,' ',emp_last_name) as value","us.user_id as id"])
->from('emp_info ei')
->join('join','emp_master em','ei.emp_info_id = emp_master_emp_info_id')
->join('join','users us','em.emp_master_user_id = user_id')
->join('join','lib_borrow_transaction lbt','us.user_id = lbt_holder_id')
->where('us.is_block = 0')
->andWhere('lbt.is_status = 0')
->andWhere('lbt.lbt_holder_type = "E"')
->andWhere('lbt.lbt_holder_id = us.user_id')
->all();

帮助者:

$data = ArrayHelper::map($e_data, 'id', 'value');
return $data;
}

如何在ArrayHelper中同时传递两个查询数据或某种方式组合两个查询?

2 个答案:

答案 0 :(得分:0)

尝试使用union

public function getName(){

    $s_data = (new \yii\db\Query())
    ->select(["CONCAT(stu_first_name,' ',stu_last_name) as value","us.user_id as id"])
    ->from('stu_info si')
    ->join('join','stu_master sm','si.stu_info_id = stu_master_stu_info_id')
    ->join('join','users us','sm.stu_master_user_id = user_id')
    ->join('join','lib_borrow_transaction lbt','us.user_id = lbt_holder_id')
    ->where('us.is_block = 0')
    ->andWhere('lbt.lbt_holder_type = "S"') 
    ->andWhere('lbt.is_status = 0')
    ->andWhere('lbt.lbt_holder_id = us.user_id')
    ->all();

    $e_data = (new \yii\db\Query())
    ->select(["CONCAT(emp_first_name,' ',emp_last_name) as value","us.user_id as id"])
    ->from('emp_info ei')
    ->join('join','emp_master em','ei.emp_info_id = emp_master_emp_info_id')
    ->join('join','users us','em.emp_master_user_id = user_id')
    ->join('join','lib_borrow_transaction lbt','us.user_id = lbt_holder_id')
    ->where('us.is_block = 0')
    ->andWhere('lbt.is_status = 0')
    ->andWhere('lbt.lbt_holder_type = "E"')
    ->andWhere('lbt.lbt_holder_id = us.user_id')
    ->all();


     $e_data->union($s_data);    // Union of the query

     $data = ArrayHelper::map($e_data, 'id', 'value');
     return $data;

}

答案 1 :(得分:0)

我找到了有效的傻解决方案, 刚刚使用了 array_merge()

$all = array_merge($s_data, $e_data);
$data = ArrayHelper::map($all, 'id', 'value');
return $data;