/** * 为当前对象增加标签,如果存在,则直接返回;接受的参数是标签名称 * @param $tagName * @return \Phalcon\Mvc\Model|Tags */ public function addTag($tagName, Users $byUser) { $tag = (new Tags())->getTagByName($tagName); $taggables = Taggables::query()->where('tag_id = :tag_id:', ['tag_id' => $tag->id])->andWhere('taggable_id = :taggable_id:', ['taggable_id' => $this->id])->andWhere('taggable_type = :type:', ['type' => get_class($this)])->andWhere('user_id = :user:'******'user' => $byUser->id])->execute(); if (count($taggables) == 0) { $taggable = new Taggables(); $taggable->tag_id = $tag->id; $taggable->taggable_id = $this->id; $taggable->taggable_type = get_class($this); $taggable->user_id = $this->getDI()->getShared('session')->get('auth')['id']; $taggable->save(); } return $tag; }
public function addTag(Tags $tag) { $user = AuthFacade::getService(); $this->deleteCacheTags(); $taggables = $this->getTaggable($tag); foreach ($taggables as $t) { if ($t->user_id == $user->id) { return $this; } //如果当前用户已经加过该taggable,则直接返回 } $data = ['tag_id' => $tag->id, 'taggable_type' => get_class($this), 'taggable_id' => $this->id, 'user_id' => $user->id]; $taggable = new Taggables(); $taggable->save($data); $meta = $tag->getTagmetaOrNew(); $meta->save(['taggableCount' => $meta->taggableCount + 1]); if ($taggables->count() == 0) { $tag->increaseCount('taggableCount'); } return $this; }