/** * @param $tag is string * @param $object is model Posts * @return bool */ public function saveTagsInPosts($tag, $object) { $tagsId = []; $postId = $object->getId(); $getTagsId = $this->getTagsId($tag); if (!is_array($getTagsId)) { return false; } $postsTags = PostsTags::find(['postsId = ?0', 'bind' => [$postId]]); foreach ($postsTags as $value) { $tagsId[] = $value->tagsId; } //Deleted tags $rows2 = array_diff($tagsId, $getTagsId); $this->deletedTag($postId, $rows2); $rows = array_diff($getTagsId, $tagsId); foreach ($rows as $tagId) { $postsTags = new PostsTags(); $postsTags->setTagsId($tagId); $postsTags->setPostsId($postId); if (!$postsTags->save()) { return false; } //Update the total of posts related to a tags if ($object->getOperationMade() == \Phalcon\Mvc\Model::OP_CREATE) { $tags = Tags::findFirstById($tagId); $number = $tags->getNumberposts(); $tags->setNumberPosts($number + 1); $tags->save(); } } return true; }
/** * @todo Update the total of posts related to a tags * * @return bool */ public function saveTagsInPosts($_tag, $object) { $tags = $this->isTags($_tag); if (is_array($tags)) { // $postsTags = PostsTags::find(['postsId = ?0', 'bind' => [$object->getId()]]); if (isset($postsTags)) { $postsTags->delete(); } foreach ($tags as $tagsId) { $postsTags = new PostsTags(); $postsTags->setTagsId($tagsId); $postsTags->setPostsId($object->getId()); if (!$postsTags->save()) { return false; } //Update the total of posts related to a tags if ($object->getOperationMade() == \Phalcon\Mvc\Model::OP_CREATE) { $tag = Tags::findFirstById($tagsId); $number = $tag->getNumberposts(); $tag->setNumberPosts($number + 1); if (!$tag->save()) { return false; } } } return true; } return false; }