Thursday, October 5, 2017

Manual update field command yii2 advanced

public function actionCreate()
    {
        $model = new Book();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
   
  $countnumbook = Book::find()->count(); //Count records to update field
   
  Yii::$app->db->createCommand() //Here update command
         ->update('statistics', ['numbook' => $countnumbook], ['id_statisticd' => 1]) 
         //Here update command
         ->execute(); //Here update command
   
   
            return $this->redirect(['view', 'id' => $model->id_book]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }