1

can anyone tell me how to override actioncreate and actionupdate method yii2 rest api..

class CabController extends ActiveController
  {
     public $modelClass = 'api\modules\v1\models\Cab';

      public function actions(){

        $actions = parent::actions();           

        unset($actions['create']);
        unset($actions['update']);

        return $actions;
    }

     public function actionCreate(){

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $model = $this->modelClass;
       $model->load(Yii::$app->request->post());
       $cur_time = date('Y-m-d H:i:s');
       $model->date_created  =  $cur_time;
       $$model->save(false);
  }  

If i do like this mean i got error like 500 internal server error and error message like Call to a member function load() on a non-object ..how to solve this issue.. Thanks...

1 Answer 1

0

You are not creating object, instead you are assigning string api\modules\v1\models\Cab to $model variable. Change assigning part to:

$model = new $this->modelClass;

In case of "Class not found" error add leading backslash to class name: \api\modules\v1\models\Cab.

Related answers about creating object from string:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.