public function afterSave($insert, $changedAttributes) { //$oldCategorias = array("0"=>1); $actualItens = []; if (($actualItens = AuthItemChild::find()->andWhere("parent = '{$this->name}'")->asArray()->all()) !== null) { $actualItens = ArrayHelper::getColumn($actualItens, 'child'); } //verifica se é um array $this->itemIds = is_array($this->itemIds) ? $this->itemIds : [$this->itemIds]; $inserirItens = array_diff($this->itemIds, $actualItens); if (!empty($inserirItens)) { //save the relations foreach ($inserirItens as $id) { $r = new AuthItemChild(); $r->parent = $this->name; $r->child = $id; $r->save(); } } $delItens = array_diff($actualItens, $this->itemIds); if (!empty($delItens)) { foreach ($delItens as $remove) { $r = AuthItemChild::findOne(['child' => $remove, 'parent' => $this->name]); $r->delete(); } } parent::afterSave($insert, $changedAttributes); //don't forget this }
/** * Finds the AuthItem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AuthItemChild the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AuthItemChild::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Страница не найдена.'); } }
/** * Finds the AuthItemChild model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $parent * @param string $child * @return AuthItemChild the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($parent, $child) { if (($model = AuthItemChild::findOne(['parent' => $parent, 'child' => $child])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }