示例#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;
 }
示例#2
0
文件: _form.php 项目: cboy868/nana
    $av_model = new AvRel();
    ?>
        <?php 
    foreach ($attr as $k => $v) {
        ?>
            <?php 
        if ($v->is_multi) {
            ?>
                <?php 
            echo $form->field(isset($av[$v->id]) ? $av[$v->id] : new AvRel(), "[{$k}]av_id")->checkBoxList(Attr::getValMap($v->id))->label($v->name);
            ?>
            <?php 
        } else {
            ?>
                <?php 
            echo $form->field(isset($av[$v->id]) ? $av[$v->id] : new AvRel(), "[{$k}]av_id")->radioList(Attr::getValMap($v->id))->label($v->name);
            ?>
            <?php 
        }
        ?>
        <?php 
    }
    ?>
    <?php 
}
?>

	<div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <?php 
echo Html::submitButton('保 存', ['class' => 'btn btn-primary btn-block']);
示例#3
0
 /**
  * Finds the Attr model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Attr the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Attr::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
示例#4
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]);
     }
 }