Esempio n. 1
0
 /**
  * @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;
 }
Esempio n. 2
0
 public function run()
 {
     $faker = Faker::create();
     $log = new Stream('php://stdout');
     $log->info('Start ' . __CLASS__);
     /** @var Phalcon\Db\AdapterInterface $database */
     $database = $this->getDI()->get('db');
     $database->begin();
     $postsId = Posts::find(['columns' => 'id'])->toArray();
     $tagsId = Tags::find(['columns' => 'id'])->toArray();
     for ($i = 0; $i <= self::POSTS_TAGS_TOTAL; $i++) {
         $postRandId = array_rand($postsId);
         $tagsRandId = array_rand($tagsId);
         $postTag = new PostsTags();
         $postTag->postsId = $postsId[$postRandId]['id'];
         $postTag->tagsId = $tagsId[$tagsRandId]['id'];
         if (!$postTag->save()) {
             var_dump($tags->getMessages());
             $database->rollback();
             die;
         }
         $log->info('Posts<->Tags ' . $postTag->getPostsId() . '<->' . $postTag->getTagsId());
     }
 }
Esempio n. 3
0
 /**
  * @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;
 }