/** * 商品カテゴリの削除、登録 */ 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 . '」が存在しません。'); } } }
/** * Product オブジェクトを生成して返す. * * $product_class_num = 0 とすると商品規格の無い商品を生成する. * * @param string $product_name 商品名. null の場合はランダムな文字列が生成される. * @param integer $product_class_num 商品規格の生成数 * @return \Eccube\Entity\Product */ public function createProduct($product_name = null, $product_class_num = 3) { $faker = $this->getFaker(); $Member = $this->app['eccube.repository.member']->find(2); $Disp = $this->app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_SHOW); $ProductType = $this->app['eccube.repository.master.product_type']->find(1); $DeliveryDates = $this->app['eccube.repository.delivery_date']->findAll(); $Product = new Product(); if (is_null($product_name)) { $product_name = $faker->word; } $Product->setName($product_name)->setCreator($Member)->setStatus($Disp)->setDelFlg(Constant::DISABLED)->setDescriptionList($faker->paragraph())->setDescriptionDetail($faker->text()); $this->app['orm.em']->persist($Product); $this->app['orm.em']->flush($Product); for ($i = 0; $i < 3; $i++) { $ProductImage = new ProductImage(); $ProductImage->setCreator($Member)->setFileName($faker->word . '.jpg')->setRank($i)->setProduct($Product); $this->app['orm.em']->persist($ProductImage); $this->app['orm.em']->flush($ProductImage); $Product->addProductImage($ProductImage); } $ClassNames = $this->app['eccube.repository.class_name']->findAll(); $ClassName1 = $ClassNames[$faker->numberBetween(0, count($ClassNames) - 1)]; $ClassName2 = $ClassNames[$faker->numberBetween(0, count($ClassNames) - 1)]; // 同じ ClassName が選択された場合は ClassName1 のみ if ($ClassName1->getId() === $ClassName2->getId()) { $ClassName2 = null; } $ClassCategories1 = $this->app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName1)); $ClassCategories2 = array(); if (is_object($ClassName2)) { $ClassCategories2 = $this->app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName2)); } for ($i = 0; $i < $product_class_num; $i++) { $ProductStock = new ProductStock(); $ProductStock->setCreator($Member)->setStock($faker->randomNumber(3)); $this->app['orm.em']->persist($ProductStock); $this->app['orm.em']->flush($ProductStock); $ProductClass = new ProductClass(); $ProductClass->setCode($faker->word)->setCreator($Member)->setStock($ProductStock->getStock())->setProductStock($ProductStock)->setProduct($Product)->setProductType($ProductType)->setStockUnlimited(false)->setPrice02($faker->randomNumber(5))->setDeliveryDate($DeliveryDates[$faker->numberBetween(0, 8)])->setDelFlg(Constant::DISABLED); if (array_key_exists($i, $ClassCategories1)) { $ProductClass->setClassCategory1($ClassCategories1[$i]); } if (array_key_exists($i, $ClassCategories2)) { $ProductClass->setClassCategory2($ClassCategories2[$i]); } $this->app['orm.em']->persist($ProductClass); $this->app['orm.em']->flush($ProductClass); $ProductStock->setProductClass($ProductClass); $ProductStock->setProductClassId($ProductClass->getId()); $this->app['orm.em']->flush($ProductStock); $Product->addProductClass($ProductClass); } // デフォルトの商品規格生成 $ProductStock = new ProductStock(); $ProductStock->setCreator($Member)->setStock($faker->randomNumber(3)); $this->app['orm.em']->persist($ProductStock); $this->app['orm.em']->flush($ProductStock); $ProductClass = new ProductClass(); if ($product_class_num > 0) { $ProductClass->setDelFlg(Constant::ENABLED); } else { $ProductClass->setDelFlg(Constant::DISABLED); } $ProductClass->setCode($faker->word)->setCreator($Member)->setStock($ProductStock->getStock())->setProductStock($ProductStock)->setProduct($Product)->setProductType($ProductType)->setPrice02($faker->randomNumber(5))->setDeliveryDate($DeliveryDates[$faker->numberBetween(0, 8)])->setStockUnlimited(false)->setProduct($Product); $this->app['orm.em']->persist($ProductClass); $this->app['orm.em']->flush($ProductClass); $ProductStock->setProductClass($ProductClass); $ProductStock->setProductClassId($ProductClass->getId()); $this->app['orm.em']->flush($ProductStock); $Product->addProductClass($ProductClass); $Categories = $this->app['eccube.repository.category']->findAll(); $i = 0; foreach ($Categories as $Category) { $ProductCategory = new ProductCategory(); $ProductCategory->setCategory($Category)->setProduct($Product)->setCategoryId($Category->getId())->setProductId($Product->getId())->setRank($i); $this->app['orm.em']->persist($ProductCategory); $this->app['orm.em']->flush($ProductCategory); $Product->addProductCategory($ProductCategory); $i++; } $this->app['orm.em']->flush($Product); return $Product; }
/** * Product オブジェクトを生成して返す. * * $product_class_num = 0 としても、商品規格の無い商品を生成できない. 単に ProductClass が生成されないだけなので注意すること. * * @param string $product_name 商品名. null の場合はランダムな文字列が生成される. * @param integer $product_class_num 商品規格の生成数 * @return \Eccube\Entity\Product */ public function createProduct($product_name = null, $product_class_num = 3) { $faker = $this->getFaker(); $Member = $this->app['eccube.repository.member']->find(2); $Disp = $this->app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_SHOW); $ProductType = $this->app['eccube.repository.master.product_type']->find(1); $Product = new Product(); if (is_null($product_name)) { $product_name = $faker->word; } $Product->setName($product_name)->setCreator($Member)->setStatus($Disp)->setDelFlg(Constant::DISABLED)->setDescriptionList($faker->paragraph())->setDescriptionDetail($faker->text()); $this->app['orm.em']->persist($Product); $this->app['orm.em']->flush($Product); for ($i = 0; $i < 3; $i++) { $ProductImage = new ProductImage(); $ProductImage->setCreator($Member)->setFileName($faker->word . '.jpg')->setRank($i)->setProduct($Product); $this->app['orm.em']->persist($ProductImage); $this->app['orm.em']->flush($ProductImage); $Product->addProductImage($ProductImage); } for ($i = 0; $i < $product_class_num; $i++) { $ProductStock = new ProductStock(); $ProductStock->setCreator($Member)->setStock($faker->randomNumber()); $this->app['orm.em']->persist($ProductStock); $this->app['orm.em']->flush($ProductStock); $ProductClass = new ProductClass(); $ProductClass->setCreator($Member)->setProductStock($ProductStock)->setProduct($Product)->setProductType($ProductType)->setStockUnlimited(false)->setPrice02($faker->randomNumber(5))->setDelFlg(Constant::DISABLED); $this->app['orm.em']->persist($ProductClass); $this->app['orm.em']->flush($ProductClass); $Product->addProductClass($ProductClass); } $Categories = $this->app['eccube.repository.category']->findAll(); $i = 0; foreach ($Categories as $Category) { $ProductCategory = new ProductCategory(); $ProductCategory->setCategory($Category)->setProduct($Product)->setCategoryId($Category->getId())->setProductId($Product->getId())->setRank($i); $this->app['orm.em']->persist($ProductCategory); $this->app['orm.em']->flush($ProductCategory); $Product->addProductCategory($ProductCategory); $i++; } $this->app['orm.em']->flush($Product); return $Product; }
/** * @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; }