/** * 商品カテゴリの削除、登録 */ protected function createProductCategory($row, Product $Product, $app, $data) { // カテゴリの削除 $ProductCategories = $Product->getProductCategories(); foreach ($ProductCategories as $ProductCategory) { $Product->removeProductCategory($ProductCategory); $this->em->remove($ProductCategory); $this->em->flush($ProductCategory); } if ($row['商品カテゴリ(ID)'] == '') { // 入力されていなければ削除のみ return; } // カテゴリの登録 $categories = explode(',', $row['商品カテゴリ(ID)']); $rank = 1; foreach ($categories as $category) { if (preg_match('/^\\d+$/', $category)) { $Category = $app['eccube.repository.category']->find($category); if (!$Category) { $this->addErrors($data->key() + 1 . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。'); } else { $ProductCategory = new ProductCategory(); $ProductCategory->setProductId($Product->getId()); $ProductCategory->setCategoryId($Category->getId()); $ProductCategory->setProduct($Product); $ProductCategory->setCategory($Category); $ProductCategory->setRank($rank); $Product->addProductCategory($ProductCategory); $rank++; $this->em->persist($ProductCategory); } } else { $this->addErrors($data->key() + 1 . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。'); } } }
/** * @param ProductCategory $TargetProductCategory * @param $position * @return bool * @throws \Doctrine\DBAL\ConnectionException */ public function moveRank(ProductCategory $TargetProductCategory, $position) { $repos = $this->_em->getRepository('\\Eccube\\Entity\\ProductCategory'); $this->_em->getConnection()->beginTransaction(); try { $oldRank = $TargetProductCategory->getRank(); // 最大値取得 $qb = $repos->createQueryBuilder('pc'); $max = $qb->select($qb->expr()->max('pc.rank'))->where($qb->expr()->eq('pc.category_id', $TargetProductCategory->getCategoryId()))->getQuery()->getSingleScalarResult(); $position = $max - ($position - 1); $position = max(1, $position); $TargetProductCategory->setRank($position); $status = true; if ($position != $oldRank) { // 他のItemのランクを調整する if ($position < $oldRank) { // down $this->_em->createQueryBuilder()->update('\\Eccube\\Entity\\ProductCategory', 'pc')->set('pc.rank', 'pc.rank + 1')->where('pc.rank <= :oldRank AND pc.rank >= :rank AND pc.category_id = :category_id AND pc.product_id != :product_id')->setParameter('oldRank', $oldRank)->setParameter('rank', $position)->setParameter('category_id', $TargetProductCategory->getCategoryId())->setParameter('product_id', $TargetProductCategory->getProductId())->getQuery()->execute(); } else { // up $this->_em->createQueryBuilder()->update('\\Eccube\\Entity\\ProductCategory', 'pc')->set('pc.rank', 'pc.rank - 1')->where('pc.rank >= :oldRank AND pc.rank <= :rank AND pc.category_id = :category_id AND pc.product_id != :product_id')->setParameter('oldRank', $oldRank)->setParameter('rank', $position)->setParameter('category_id', $TargetProductCategory->getCategoryId())->setParameter('product_id', $TargetProductCategory->getProductId())->getQuery()->execute(); } $this->_em->persist($TargetProductCategory); $this->_em->flush(); } $this->_em->getConnection()->commit(); return $status; } catch (\Exception $e) { $this->_em->getConnection()->rollback(); $this->_em->close(); $this->app->log($e); } return false; }