在yii2中打印存储过程的值

时间:2016-08-03 07:18:04

标签: stored-procedures yii2 resultset

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

这是我尝试过的代码:

    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'

请帮忙。

1 个答案:

答案 0 :(得分:0)

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

在伪代码中:

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

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