public function save() { //Yii::getLogger()->log('start save photobook:'.$this->id, YII_DEBUG); $cart = Cart::findOne(['id' => $this->id]); if (empty($cart)) { $cart = new Cart(); $cart->user_id = $this->user_id; $cart->title = $this->title; $cart->price = $this->price; $cart->quantity = $this->quantity; $cart->sub_total = $this->sub_total; $cart->product_type = $this->product_type; if (empty($this->product_info)) { $this->product_info = []; } $cart->product_info = json_encode($this->product_info); Yii::getLogger()->log('save:', YII_DEBUG); if ($cart->save()) { $this->id = $cart->id; return $cart; } else { Yii::getLogger()->log('save error', YII_DEBUG); } } else { $cart->user_id = $this->user_id; $cart->title = $this->title; $cart->price = $this->price; $cart->quantity = $this->quantity; $cart->sub_total = $this->sub_total; $cart->product_type = $this->product_type; if (empty($this->product_info)) { $this->product_info = []; } $cart->product_info = json_encode($this->product_info); Yii::getLogger()->log('update:', YII_DEBUG); if ($cart->update()) { return $cart; } else { Yii::getLogger()->log('update error:' . print_r($cart, true), YII_DEBUG); } } return null; }
/** * Finds the Cart model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Cart the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Cart::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }