/** * Lists all Info models. * @return mixed */ public function actionIndex() { $searchModel = new InfoSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->sort = ['defaultOrder' => ['published_at' => SORT_DESC]]; $dataProvider->query->andFilterWhere(['{{info}}.locale' => Yii::$app->language]); if (Yii::$app->request->get('mid')) { $parentModel = Model::findOne(['id' => Yii::$app->request->get('mid')]); $models = Info::find()->andFilterWhere(['{{info}}.domain_id' => Yii::getAlias('@defaultDomainId'), '{{info}}.locale' => 'uk-UA'])->andWhere(['like', 'slug', $parentModel->slug])->all(); } else { $models = Info::find()->andFilterWhere(['{{info}}.domain_id' => Yii::getAlias('@defaultDomainId'), '{{info}}.locale' => 'uk-UA'])->all(); } $list = \yii\helpers\ArrayHelper::map($models, 'locale_group_id', 'title'); $cars = Model::find()->andFilterWhere(['{{model}}.domain_id' => Yii::getAlias('@defaultDomainId'), '{{model}}.locale' => 'uk-UA'])->all(); $carList = \yii\helpers\ArrayHelper::map($cars, 'id', 'title'); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'list' => $list, 'carList' => $carList]); }
/** * Lists all Info models. * @return mixed */ public function actionIndex() { $searchModel = new InfoSearch(); $searchModel->detachBehaviors(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); // $dataProvider->sort = [ // 'defaultOrder' => ['published_at' => SORT_DESC] // ]; $dataProvider->sort->attributes['search_date_published'] = ['asc' => ['published_at' => SORT_ASC], 'desc' => ['published_at' => SORT_DESC]]; $dataProvider->query->andFilterWhere(['info.locale' => Yii::$app->language]); // $dataProvider->query->andFilterWhere(['!=', 'info.locale', 'en-US']); if (Yii::$app->request->get('mid')) { $parentModel = Model::findOne(['id' => Yii::$app->request->get('mid')]); $models = Info::find()->andFilterWhere(['domain_id' => Yii::getAlias('@defaultDomainId'), 'locale' => 'uk-UA'])->andWhere(['like', 'slug', $parentModel->slug])->all(); } else { $models = Info::find()->andFilterWhere(['domain_id' => Yii::getAlias('@defaultDomainId'), 'locale' => 'uk-UA'])->all(); } $list = \yii\helpers\ArrayHelper::map($models, 'locale_group_id', 'title'); $cars = Model::find()->andFilterWhere(['domain_id' => Yii::$app->user->identity->domain_id, 'locale' => 'uk-UA'])->all(); $carList = \yii\helpers\ArrayHelper::map($cars, 'id', 'title'); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'list' => $list, 'carList' => $carList]); }
/** * Finds the Model model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Model the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Model::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function multiSave($model) { //\yii\helpers\VarDumper::dump(Yii::$app->request->post(),11,1); die(); $defaultAttributes = []; foreach ($model->getModels() as $key => $v) { foreach ($model->getModel($key)->attributes() as $attrKey) { if (empty($defaultAttributes[$attrKey])) { if (!empty($model->getModel($key)->{$attrKey})) { if ('[]' != $model->getModel($key)->{$attrKey}) { $defaultAttributes[$attrKey] = $model->getModel($key)->{$attrKey}; } } } } } unset($defaultAttributes['id']); unset($defaultAttributes['locale']); unset($defaultAttributes['locale_group_id']); //unset($defaultAttributes['slug']); $groupId = self::getLastLocaleGroupId() + 1; foreach ($model->getModels() as $key => $v) { if (!$model->getModel($key)->locale_group_id) { $model->getModel($key)->locale_group_id = $groupId; } foreach ($defaultAttributes as $key2 => $value2) { if (empty($model->getModel($key)->{$key2}) or "[]" == $model->getModel($key)->{$key2}) { $model->getModel($key)->{$key2} = $value2; } } //model_id fix $modelGroupId = Model::findOne(['id' => $model->getModel($key)->model_id])->locale_group_id; $currentModelId = Model::findOne(['locale_group_id' => $modelGroupId, 'locale' => $model->getModel($key)->locale])->id; $model->getModel($key)->model_id = $currentModelId; //\yii\helpers\VarDumper::dump($model->getModel($key),11,1); } return $model->save(); }