/** * Creates a new Goods model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Goods(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($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(); } $result = AvRel::parsePost($model->category_id, $model->id); Yii::$app->db->createCommand()->batchInsert(AvRel::tableName(), ['attr_id', 'av_id', 'category_id', 'goods_id'], $result)->execute(); } return $this->redirect(['view', 'id' => $model->id]); } else { $category_id = Yii::$app->getRequest()->get('category_id'); $attr = null; //todo 这里待优化,更新方法中的这里应该和这里能有统一的方法 if ($category_id) { $attr = Attr::find()->where(['category_id' => $category_id])->indexBy('id')->all(); foreach ($attr as $k => $v) { $av[$v->id] = new AvRel(); } } return $this->render('create', ['model' => $model, 'attr' => isset($attr) ? $attr : null, 'av' => isset($av) ? $av : null]); } }