2016-08-03 66 views
0

我想打印我执行的存储过程的结果。打印存储过程中的值yii2

这是我试过的代码:

public function actionGetdata($tranId) { 
    $model = new ReceivedGoodsDetail(); 
    // $model = $this->findModel($tranId); 
    $connection = \Yii::$app->db; 
    $transaction = $connection->beginTransaction(); 

    //set nilai default 
    $model->ID_Received_Goods = $tranId; 

    if ($model->validate()) { 
     $connection = Yii::$app->db; 
     $command = $connection->createCommand('{call usp_T_Sales_Inventory_Detail#SelectData(:ID_Received_Goods)}'); 

     $ID_Received_Goods = $model->ID_Received_Goods; 
     $command->bindParam(":ID_Received_Goods", $ID_Received_Goods, PDO::PARAM_STR); 

     $isSuccess = 0; 
     if ($command->execute()) { 
      $transaction->commit(); 
      $isSuccess = 1; 
     } else { 
      $transaction->rollBack(); 
      $isSuccess = 0; 
     } 
     return [ 
      'data' => $isSuccess 
     ]; 
    } else { 
     $isSuccess = 0; 

     return [ 
      'data' => $isSuccess, 
      'error' => $model->getErrors(), 
     ]; 
    } 
} 

当我尝试这RESTClient实现的结果是

<response> 
<data>1</data> 
</response> 

,而我想要的结果将显示

结果
select * 
from T_Received_Goods_Detail 
where ID_Received_Goods = 'RG/1608/11' 

请帮忙。

+0

为什么它会显示'select * from T_Received_Goods_Detail的结果,其中ID_Received_Goods ='RG/1608/11''当您设置'$ isSuccess = 1;'&'$ isSuccess = 0;'并返回数据'=> $ isSuccess,'。所以,它会打印'1'或'0'。它打印'1'很好。意思是,它正在执行你的查询。 –

+0

我想看到的结果从执行该查询, 例如: ' RG /一分之一千六百○七 158 ITEM-0002 管理员 2016年7月28日10:36:50.910 管理员 2016年7月28日10:36:50.910 ' – Allegra

+0

你想我知道。但是,你在哪里编写了获取选定数据的代码? –

回答

0

因此,您可以运行您的查询,而不是返回$isSuccess的值,并在操作成功时返回结果。

在伪代码:

if ($isSucces) { 
    $data = [result of query "select * from T_Received_Goods_Detail where ..."]; 
    return [ 
      'data' => $data 
     ]; 
} 

确保$data是一个普通数组和REST接口将输出为您有效的响应。

+1

谢谢@jlapoutre它的作品 – Allegra