public function afterSave($event) { $model_id = get_class($this->owner); if (!isset($_POST[$model_id]['tags'])) { return true; } $tags = explode(',', $_POST[$model_id]['tags']); $ids = []; foreach ($tags as $tag_name) { $tag = Tag::model()->findByAttributes(['name' => $tag_name]); if (!$tag) { continue; } $ids[] = $tag->id; $exists = TagRel::model()->existsByAttributes(['tag_id' => $tag->id, 'object_id' => $this->owner->id, 'model_id' => $model_id]); if ($exists) { continue; } $tag_rel = new TagRel(); $tag_rel->tag_id = $tag->id; $tag_rel->object_id = $this->owner->id; $tag_rel->model_id = $model_id; $tag_rel->save(); } $this->_deleteRels($ids); }
public function afterSave($event) { $this->_deleteRels(); $model_id = get_class($this->owner); if (isset($_POST[$model_id]['tags']) && is_array($_POST[$model_id]['tags'])) { foreach ($_POST[$model_id]['tags'] as $tag_id) { $tag = Tag::model()->findByPk($tag_id); if (!$tag) { continue; } $tag_rel = new TagRel(); $tag_rel->tag_id = $tag_id; $tag_rel->object_id = $this->owner->id; $tag_rel->model_id = $model_id; $tag_rel->save(); } } }