2017-08-24 7 views
1

Yii2フレームワークのPUT httpリクエストを使用してモデルを更新しようとしています。 モデルに単一の主キーがある場合、すべて正常に動作します。Yii2コン​​ポジットキーの場合のREST APIの更新(put)

問題は、テーブルに複合主キーがあるときです。

更新の仕方?

私は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サーバー/オファー/ 100を - 100はcompany_idです

  • PUTサーバー/オファー/ 2000年から2000年には

これら2つの要求

が問題戻ってきているORDER_IDされています

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

は、私も試してみました

  • PUTサーバー/オファー/ 2000/100 - 2000はorder_id、100はcompany_id

  • PUT SERVER /オファー/ 100/200 0

これら2リターンコントローラ/アクション見つからない例外

はまた、私は 、JSONにORDER_IDとのcompany_idを追加しましたが、何も働きません。

コントローラクラス:

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

あなたは、まず、ActiveRecordのクラスのデフォルトのPrimaryKey()をオーバーライドするために、モデルに()主キーを追加する必要があります。この関数は複合主キーを返す必要があります。 あなたがモデルを実行する必要がどのようなので、

primaryKey() 
{ 
    return array('company_id', 'order_id'); 
} 
関連する問題