public function testSetTags() { $tagGroup = new TagGroup(); $tagGroup->setTags(array('tag1', 'tag2', 'tag3')); $tagGroup->save(); $this->assertEquals(array('tag1', 'tag2', 'tag3'), Tag::statement()->where('? IN (?)', Tag::columns()->id, new Raw(TagGroup::findByPrimaryKey($tagGroup->id)->tagIds))->query()->fetchAll(null, Tag::columns()->text)); }
public function setTags(array $tagTexts) { $tagCols = Tag::columns(); $tagIdsToAdd = array(); $tagIdsByText = Tag::statement()->where('? IN (?)', $tagCols->text, $tagTexts)->query()->fetchAll($tagCols->text, $tagCols->id); foreach ($tagTexts as $tagText) { if (!isset($tagIdsByText[$tagText])) { $tagIdsToAdd[$tagText] = null; } } foreach ($tagIdsToAdd as $tagText => $tagId) { $newTag = new Tag(); $newTag->text = $tagText; $newTag->save(); $tagIdsByText[$tagText] = $newTag->id; } asort($tagIdsByText); /** * @todo consider helper crc32 index here */ $this->tagIds = implode(',', $tagIdsByText); return $this; }