/** * @param \Mirasvit\Blog\Model\Post $post * @return $this */ protected function saveCategories($post) { $table = $this->getTable('mst_blog_category_post'); /** * If category ids data is not declared we haven't do manipulations */ if (!$post->hasCategoryIds()) { return $this; } $categoryIds = $post->getCategoryIds(); $oldCategoryIds = $this->getCategoryIds($post); $insert = array_diff($categoryIds, $oldCategoryIds); $delete = array_diff($oldCategoryIds, $categoryIds); $connection = $this->getConnection(); if (!empty($insert)) { $data = []; foreach ($insert as $categoryId) { if (empty($categoryId)) { continue; } $data[] = ['category_id' => (int) $categoryId, 'post_id' => (int) $post->getId()]; } if ($data) { $connection->insertMultiple($table, $data); } } if (!empty($delete)) { foreach ($delete as $categoryId) { $where = ['post_id = ?' => (int) $post->getId(), 'category_id = ?' => (int) $categoryId]; $connection->delete($table, $where); } } return $this; }