protected function getChildOptions($model_name, $cat_id) { return ModelsOptionsList::find()->where(['model_name' => $model_name, 'model_id' => $cat_id])->all(); }
/** * @return OptionsList */ public function getChildOptionsList($cat_id) { $model_name = MyHelper::modelFromNamespace($this->model_name); $parent_model_name = MyHelper::modelFromNamespace($this->parent_model_name); $model = new $this->parent_model_name(); $parent_ids = $this->getParentIds($cat_id, [$cat_id], $model); $option_ids = ModelsOptionsList::find()->select('option_id')->where(['IN', 'model_id', $parent_ids])->asArray()->all(); $options = OptionsList::find()->where(["IN", 'id', $this->flatArray($option_ids, 'option_id')])->andWhere(['model' => $parent_model_name . '-' . $model_name])->all(); return $options; }
public function actionUpdate() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $postBody = Yii::$app->getRequest()->getBodyParams(); $cats = $postBody['cats']; $options = $postBody['options']; foreach ($cats as $cat_id) { $current_cat = ModelsOptionsList::findOne(['model_id' => $cat_id]); foreach ($options as $option_id) { $coincidence = ModelsOptionsList::find()->where(['model_id' => $cat_id, 'option_id' => $option_id])->count() > 0 ? true : false; if (!$coincidence) { $newModelOptions = new ModelsOptionsList(); $newModelOptions->model_id = $cat_id; $newModelOptions->model_name = 'Cats'; $newModelOptions->option_id = $option_id; $newModelOptions->save(); } } } }