/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TypeGoods::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * @return mixed */ public function actionIndex($type_id = null, $kind_id = null) { $types = TypeGoods::find()->all(); $orders = ArrayHelper::getColumn(Order::find()->where(['user_id' => \Yii::$app->user->getId(), 'active' => 1])->all(), 'goods_id'); $query = Goods::find()->innerJoinWith('type'); if ($type_id) { $query->andWhere(['type_id' => $type_id]); } if ($kind_id) { switch ($kind_id) { case 'buy': $query->andWhere(['kind' => 1]); break; case 'order': $query->andWhere(['kind' => 0]); break; } } $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $pages->pageSize = 12; $models = $query->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('index', ['types' => $types, 'goods' => $models, 'pages' => $pages, 'orders' => $orders]); }
/** * @return \yii\db\ActiveQuery */ public function getType() { return $this->hasOne(TypeGoods::className(), ['id' => 'type_id']); }
/* @var $searchModel common\models\GoodsSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Товары'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="goods-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a('Создать товар', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', 'name', ['label' => 'Тип', 'attribute' => 'type_id', 'value' => function ($model) { return $model->type->name; }, 'filter' => Html::activeDropDownList($searchModel, 'type_id', ArrayHelper::map(\common\models\TypeGoods::find()->asArray()->all(), 'id', 'name'), ['class' => 'form-control', 'prompt' => 'Выберите тип'])], 'price', ['label' => 'Вид', 'attribute' => 'kind', 'value' => function ($model) { return $model->kind ? "В продаже" : "Под заказ"; }, 'filter' => Html::activeDropDownList($searchModel, 'kind', [0 => 'Под заказ', 1 => "В продаже"], ['class' => 'form-control', 'prompt' => 'Выберите вид'])], ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
/** * Finds the TypeGoods model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return TypeGoods the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TypeGoods::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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->imageFile = UploadedFile::getInstance($model, 'imageFile'); $oldname = $model->image; $filename = $model->imageFile ? $model->imageFile->baseName . '.' . $model->imageFile->extension : $model->image; if ($model->imageFile && $oldname != $filename) { $model->image = $filename; } if ($model->save()) { if ($oldname != $filename) { $model->uploadImage(); } return $this->redirect(['view', 'id' => $model->id]); } } $types = ArrayHelper::map(TypeGoods::find()->asArray()->indexBy('id')->all(), 'id', 'name'); return $this->render('update', ['model' => $model, 'types' => $types]); }