Example #1
0
 protected function saveCategoryRelation(ArticleModel $article)
 {
     $article->setIsChangedCategoryList(false);
     $id = $article->getId();
     $categories = $article->getCategoriesIds();
     if ($categories === null) {
         return $this;
     }
     $oldCategoryIds = $article->getCategoryIds();
     $insert = array_diff_key($categories, $oldCategoryIds);
     $delete = array_diff_key($oldCategoryIds, $categories);
     $adapter = $this->getConnection();
     if (!empty($delete)) {
         $condition = array('category_id IN(?)' => $delete, 'article_id=?' => $id);
         $adapter->delete($this->articleCategoryTable, $condition);
     }
     if (!empty($insert)) {
         $data = array();
         foreach ($insert as $categoryId) {
             $data[] = array('article_id' => (int) $id, 'category_id' => (int) $categoryId, 'position' => 1);
         }
         $adapter->insertMultiple($this->articleCategoryTable, $data);
     }
     if (!empty($insert) || !empty($delete)) {
         $categoryIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
         $this->eventManager->dispatch('gemtoo_blog_article_change_categories', array('article' => $article, 'category_ids' => $categoryIds));
     }
     if (!empty($insert) || !empty($delete)) {
         $article->setIsChangedCategoryList(true);
         $categoryIds = array_keys($insert + $delete);
         $article->setAffectedCategoryIds($categoryIds);
     }
     return $this;
 }