2017-08-24 191 views
1

我想通過在Yii2框架中的PUT http請求更新我的模型。 當我的模型中有單個主鍵時,一切正常。Yii2複合鍵更新(放)當複合鍵

問題是當我在表中有複合主鍵時。

如何更新?

我提交JSON:

{"date_execution":"2017-08-26","order_id":"59", "company_id":13,"your_price":100,"car_id":"8","note":"lorem ipsum"} 

我複合主鍵包括: - ORDER_ID - COMPANY_ID

我嘗試以下請求:

  • PUT SERVER /報價/ 100 - 其中100是company_id

  • PUT SERVER /報價/ 2000 - 在2000 ORDER_ID

那些2個請求都返回的問題:

{"name":"Not Found","message":"Object not found: 13","code":0,"status":404,"type":"yii\\web\\NotFoundHttpException"} 

我也試過

  • PUT SERVER /報價/ 2000/100 - 其中2000是order_id,100是company_id

  • PUT SERVER/offer/100/200 0

的2回控制器/動作未發現異常

我也加入ORDER_ID和COMPANY_ID到JSON, 但沒有任何工程。

控制器類:

use yii\rest\ActiveController; 
class OfferController extends ActiveController 
{ 
    // adjust the model class to match your model 
    public $modelClass = 'app\models\Offer'; 
    public function behaviors(){ 
     $behaviors = parent::behaviors(); 

     // remove authentication filter 
     $auth = $behaviors['authenticator']; 
     unset($behaviors['authenticator']); 

     // add CORS filter 
     $behaviors['corsFilter'] = [ 
       'class' => CustomCors::className() 
     ]; 

     // re-add authentication filter 
     $behaviors['authenticator'] = [ 
       'class' => CompositeAuth::className(), 
       'authMethods' => [ 
         HttpBearerAuth::className(), 
       ], 
     ]; 
     // avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method) 
     $behaviors['authenticator']['except'] = ['options']; 

     return $behaviors; 
    } 
} 
+1

我認爲你必須創建一個自定義操作,因爲UpdateAction只需$ id參數:https://github.com/yiisoft/yii2/blob/master /framework/rest/UpdateAction.php –

回答

0

您首先需要添加的PrimaryKey()模型,覆蓋ActiveRecord類的默認的PrimaryKey()。這個函數需要返回你的複合主鍵。 你需要做模型什麼從而將

primaryKey() 
{ 
    return array('company_id', 'order_id'); 
}