Пример #1
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());
     }
 }
Пример #2
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;
 }
Пример #3
0
 /**
  * @param $postId
  * @param $tagIds
  */
 public function deletedTag($postId, $tagIds)
 {
     foreach ($tagIds as $id) {
         $object = PostsTags::find(['postsId = ?0 AND tagsId =?1', 'bind' => [$postId, $id]]);
         if ($object) {
             $object->delete();
         }
     }
 }