Esempio n. 1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Attr::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'body', $this->body]);
     return $dataProvider;
 }
Esempio n. 2
0
 /**
  * Updates an existing Goods model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if (isset($_POST['mid'])) {
             Yii::$app->db->createCommand()->update('{{%attachment_rel}}', ['res_id' => $model->id], ['res_name' => 'shop_goods', 'attach_id' => $_POST['mid']])->execute();
             $model->cover = $_POST['mid'][0];
             $model->save();
         }
         $av = AvRel::find()->where(['goods_id' => $model->id])->indexBy('av_id')->asArray()->all();
         $postAv = Yii::$app->getRequest()->post('AvRel');
         $postResult = AvRel::parsePost($model->category_id, $model->id);
         $new = array_diff_key($postResult, $av);
         $del = array_diff_key($av, $postResult);
         if ($del) {
             AvRel::deleteAll(['and', 'goods_id = :goods_id', ['in', 'av_id', array_keys($del)]], [':goods_id' => $id]);
         }
         if ($new) {
             Yii::$app->db->createCommand()->batchInsert(AvRel::tableName(), ['attr_id', 'av_id', 'category_id', 'goods_id'], $new)->execute();
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $attr = Attr::find()->where(['category_id' => $model->category_id])->all();
         $attr = ArrayHelper::index($attr, 'id');
         $av = AvRel::find()->where(['goods_id' => $id])->all();
         $result = [];
         foreach ($av as $k => &$v) {
             if ($attr[$v->attr_id]->is_multi) {
                 if (isset($result[$v->attr_id])) {
                     $result[$v->attr_id]->av_id = array_merge([$v->av_id], $result[$v->attr_id]->av_id);
                 } else {
                     $v->av_id = [$v->av_id];
                     $result[$v->attr_id] = $v;
                 }
             } else {
                 $result[$v->attr_id] = $v;
             }
         }
         return $this->render('update', ['model' => $model, 'attr' => $attr, 'av' => $result]);
     }
 }