Example #1
0
 /**
  * @return \Mirasvit\Blog\Model\Tag
  */
 protected function initModel()
 {
     if ($id = $this->getRequest()->getParam('id')) {
         $tag = $this->tagFactory->create()->load($id);
         if ($tag->getId() > 0) {
             $this->registry->register('current_blog_tag', $tag);
             return $tag;
         }
     }
     return false;
 }
Example #2
0
 /**
  * @param \Mirasvit\Blog\Model\Post $post
  * @return $this
  */
 protected function saveTags($post)
 {
     $table = $this->getTable('mst_blog_tag_post');
     if (!$post->hasTagNames()) {
         return $this;
     }
     $oldTagIds = $post->getTagIds();
     $tagIds = [];
     foreach ($post->getTagNames() as $tag) {
         $tagIds[] = $this->tagFactory->create()->getOrCreate($tag)->getId();
     }
     $tagIds = array_unique($tagIds);
     $insert = array_diff($tagIds, $oldTagIds);
     $delete = array_diff($oldTagIds, $tagIds);
     $connection = $this->getConnection();
     if (!empty($insert)) {
         $data = [];
         foreach ($insert as $tagId) {
             if (empty($tagId)) {
                 continue;
             }
             $data[] = ['tag_id' => (int) $tagId, 'post_id' => (int) $post->getId()];
         }
         if ($data) {
             $connection->insertMultiple($table, $data);
         }
     }
     if (!empty($delete)) {
         foreach ($delete as $tagId) {
             $where = ['post_id = ?' => (int) $post->getId(), 'tag_id = ?' => (int) $tagId];
             $connection->delete($table, $where);
         }
     }
     return $this;
 }