Xpressengine\Tag\TagHandler::attach PHP Method

attach() protected method

Attach tag to taggable
protected attach ( string $taggableId, ArrayAccess | array $tags ) : void
$taggableId string taggable id
$tags ArrayAccess | array tag instances
return void
    protected function attach($taggableId, $tags)
    {
        $position = 0;
        /** @var Tag $tag */
        foreach ($tags as $tag) {
            $conn = $tag->getConnection();
            try {
                // 대상아이디와 태그 아이템 아이디가 unique 키로 설정되어
                // 존재 유무와 상관없이 insert 시도 함
                // duplicate error 무시
                $conn->table($tag->getTaggableTable())->insert(['tagId' => $tag->getKey(), 'taggableId' => $taggableId, 'position' => $position, 'createdAt' => Carbon::now()]);
                $tag->increment('count');
            } catch (QueryException $e) {
                if ($e->getCode() != "23000") {
                    throw $e;
                }
                $conn->table($tag->getTaggableTable())->where('tagId', $tag->getKey())->where('taggableId', $taggableId)->update(['position' => $position]);
            }
            $position++;
        }
    }