public function create() { $question = new Question(); $question->setAttributes(['subject' => $this->subject, 'content' => $this->content, 'author_id' => $this->author_id]); if (($result = $question->save()) && ($result = $question->setActive())) { $tags = Tag::findAll(['name' => is_array($this->tags) ? $this->tags : explode(',', $this->tags)]); foreach ($tags as $tag) { $tagItem = new TagItem(); $tagItem->setAttributes(['target_id' => $question->id, 'target_type' => $question::TYPE]); $tag->addItem($tagItem); } } return $result ? $question : false; }
public function down() { $this->dropTable(Tag::tableName()); $this->dropTable(TagItem::tableName()); }
/** * 获取帖子标签记录 * @return mixed */ public function getTagItems() { return $this->hasMany(TagItem::className(), ['target_id' => 'id'])->andWhere(['target_type' => self::TYPE]); }
public function addItem(TagItem $item) { $item->setAttributes(['tid' => $this->id]); return $item->save(); }