/**
  * Updates an existing Category model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     if (!$this->is_access('category/update')) {
         Yii::$app->session->setFlash('error', $this->errorInfo);
         return $this->redirect($this->redirectUrl);
     }
     $model = $this->findModel($id);
     $CatRecommend = new CatRecommend();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         // 加入分类推荐
         $CatRecommend->insertRecommend($model->cat_id, $_POST['Category']['recommend_type']);
         // 是否需要显示在导航栏
         $Nav = new Nav();
         if ($model->show_in_nav == 1) {
             $Nav->addData('c', $model->cat_id);
         } else {
             $Nav->delData('c', $model->cat_id);
         }
         Yii::$app->session->setFlash('success', '编辑成功');
         return $this->redirect(['index']);
     } else {
         $model->recommend_type = $CatRecommend->catRecommend($id);
         Yii::$app->view->params['meta_title'] = '编辑分类';
         $catList = $model->parent(0);
         return $this->render('update', ['model' => $model, 'catList' => $catList]);
     }
 }
Beispiel #2
0
 /**
  * 新增分类推荐
  * @param $cat_id
  * @param array $recommend_type 必须是一个数组
  */
 public function insertRecommend($cat_id, $recommend_type)
 {
     if ($recommend_type && is_array($recommend_type)) {
         // 删除以前的数据
         CatRecommend::deleteAll('cat_id = :cat_id', [':cat_id' => $cat_id]);
         $sql = "INSERT INTO {{%cat_recommend}} (`cat_id`, `recommend_type`) VALUES ";
         // 新增现在的
         foreach ($recommend_type as $val) {
             $sql .= "({$cat_id}, {$val}),";
         }
         Yii::$app->db->createCommand(trim($sql, ','))->execute();
     }
 }