2016-08-23 94 views
0

上午通過yii2創建命令傳遞原始的sql命令,但我總是得到一個錯誤的屬性必須是實現QueryInterface的類的一個實例,例如警予\ DB \查詢或它的子類將原始sql傳遞給yii2活動數據提供者

這是代碼

 $sql =" 
     SELECT 
    tblpritems.PRlineID 
    , tblpritems.Tracking_Code 
    , tblpritems.Description 
    , tblpritems.Quantity 
    , tblpritems.Unit_Price 
    , tblpritems.Extended_price 
    , tblpritems.PRID 
    , tblpritems.pr_solicitation_id 
    , tblpritems.date_item_received 
    , tblpritems.Quantity_received 
    , tblpritems.Remarks_on_receipt 
    , tblpritems.Received_by 

FROM 
    prts.tblpritems 
    INNER JOIN prts.tblpr 
     ON (tblpritems.PRID = tblpr.PRID) 
    INNER JOIN prts.tblprsolicitations 
     ON (tblprsolicitations.PRID = tblpr.PRID) AND (tblpritems.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblprsuppliers 
     ON (tblprsuppliers.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblpo 
     ON (tblpo.pr_supplier_id = tblprsuppliers.pr_supplier_id) 
    where tblpr.PRID=".$val." and tblpo.PO_Status_ID=7 and item_received_status=0 
    "; 

    $connection = Yii::$app->getDb(); 
    $command = $connection->createCommand($sql); 
    $result = $command->query(); 


    $dataProvider = new ActiveDataProvider([ 
     'query' => $result, 
    ]); 

當我嘗試的var_dump($數據提供程序),這就是當它返回錯誤

+0

'$命令 - >查詢()'返回DataReader的,不Query'的'實例。使用'SqlDataProvider'而不是活動的。 – ineersa

+0

問題是,我想要顯示相關記錄,但sql的dataProvider返回一個數組 –

回答

3

如果你想使用原始的SQL你應該使用sqlDatProvider http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html#sql-data-provider

$sql =" 
     SELECT 
    tblpritems.PRlineID 
    , tblpritems.Tracking_Code 
    , tblpritems.Description 
    , tblpritems.Quantity 
    , tblpritems.Unit_Price 
    , tblpritems.Extended_price 
    , tblpritems.PRID 
    , tblpritems.pr_solicitation_id 
    , tblpritems.date_item_received 
    , tblpritems.Quantity_received 
    , tblpritems.Remarks_on_receipt 
    , tblpritems.Received_by 

FROM 
    prts.tblpritems 
    INNER JOIN prts.tblpr 
     ON (tblpritems.PRID = tblpr.PRID) 
    INNER JOIN prts.tblprsolicitations 
     ON (tblprsolicitations.PRID = tblpr.PRID) AND (tblpritems.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblprsuppliers 
     ON (tblprsuppliers.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblpo 
     ON (tblpo.pr_supplier_id = tblprsuppliers.pr_supplier_id) 
    where tblpr.PRID=".$val." and tblpo.PO_Status_ID=7 and item_received_status=0 
    "; 


$sqlCount = " 
     SELECT 
    count(*) 

FROM 
    prts.tblpritems 
    INNER JOIN prts.tblpr 
     ON (tblpritems.PRID = tblpr.PRID) 
    INNER JOIN prts.tblprsolicitations 
     ON (tblprsolicitations.PRID = tblpr.PRID) AND (tblpritems.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblprsuppliers 
     ON (tblprsuppliers.pr_solicitation_id = tblprsolicitations.pr_solicitation_id) 
    INNER JOIN prts.tblpo 
     ON (tblpo.pr_supplier_id = tblprsuppliers.pr_supplier_id) 
    where tblpr.PRID=".$val." and tblpo.PO_Status_ID=7 and item_received_status=0 
    "; 


    $count = Yii::$app->db->createCommand($sqlCount)->queryScalar(); 

    $provider = new SqlDataProvider([ 
    'sql' => $sql, 
    'params' => [':status' => 1], 
    'totalCount' => $count, 
    'pagination' => [ 
     'pageSize' => 10, 
    ], 
    'sort' => [ 
     'attributes' => [ 
       ...... 
     ], 
    ], 
]); 

否則,如果你想activeDataProvider你可以使用

Assiming的ActiveRecord的是關係到Pritems模型

$query = Pritems::findBySql($sql) 


    $provider = new ActiveDataProvider([ 
    'query' => $query, 
    'pagination' => [ 
     'pageSize' => 10, 
    ], 
    'sort' => [ 
     'defaultOrder' => [ 
      .... 
     ] 
    ], 
]); 
相關問題