質問 > 管理機能 > 商品新規登録でエラーになる |
管理機能
スレッド表示 | 古いものから | 前のトピック | 次のトピック | 下へ |
投稿者 | スレッド |
---|---|
Rights |
投稿日時: 2020/11/6 18:54
対応状況: 解決済
|
一人前 登録日: 2019/3/12 居住地: 投稿: 99 |
Re: 商品新規登録でエラーになる 自己解決しました。
前回参考にした記事 + こちらの投稿を参考に今回の件、解決いたしました。 https://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=24468&forum=2 ありがとうございました。 |
Rights |
投稿日時: 2020/10/23 21:50
対応状況: −−−
|
一人前 登録日: 2019/3/12 居住地: 投稿: 99 |
Re: 商品新規登録でエラーになる その後、本件以外にも商品情報の複製を行った際に、追加した属性情報のみ複製されていませんでした。
Productエンティティに追加したgetProductDeliveryClasses()メソッドですが、 Productエンティティにあるカテゴリー処理のメソッドを置き換えて追加しています。 <?php namespace Customize\Entity; use Doctrine\ORM\Mapping as ORM; use Eccube\Annotation as Eccube; use Symfony\Component\Validator\Constraints as Assert; /** * @Eccube\EntityExtension("Eccube\Entity\Product") */ trait ProductTrait { /** * @var \Doctrine\Common\Collections\Collection * * @ORM\OneToMany(targetEntity="Customize\Entity\ProductDeliveryClass", mappedBy="Product", cascade={"persist","remove"}) */ private $ProductDeliveryClasses; /** * Constructor */ public function __construct() { $this->ProductCategories = new \Doctrine\Common\Collections\ArrayCollection(); $this->ProductClasses = new \Doctrine\Common\Collections\ArrayCollection(); $this->ProductImage = new \Doctrine\Common\Collections\ArrayCollection(); $this->ProductTag = new \Doctrine\Common\Collections\ArrayCollection(); $this->CustomerFavoriteProducts = new \Doctrine\Common\Collections\ArrayCollection(); $this->ProductDeliveryClasses = new \Doctrine\Common\Collections\ArrayCollection(); } public function copy() { // コピー対象外 $this->CustomerFavoriteProducts = new ArrayCollection(); $Categories = $this->getProductCategories(); $this->ProductCategories = new ArrayCollection(); foreach ($Categories as $Category) { $CopyCategory = clone $Category; $this->addProductCategory($CopyCategory); $CopyCategory->setProduct($this); } $DeliveryClasses = $this->getProductDeliveryClasses(); $this->ProductDeliveryClasses = new ArrayCollection(); foreach ($DeliveryClasses as $DeliveryClass) { $CopyDeliveryClass = clone $DeliveryClass; $this->addProductDeliveryClass($CopyDeliveryClass); $CopyDeliveryClass->setProduct($this); } $Classes = $this->getProductClasses(); $this->ProductClasses = new ArrayCollection(); foreach ($Classes as $Class) { $CopyClass = clone $Class; $this->addProductClass($CopyClass); $CopyClass->setProduct($this); } $Images = $this->getProductImage(); $this->ProductImage = new ArrayCollection(); foreach ($Images as $Image) { $CloneImage = clone $Image; $this->addProductImage($CloneImage); $CloneImage->setProduct($this); } $Tags = $this->getProductTag(); $this->ProductTag = new ArrayCollection(); foreach ($Tags as $Tag) { $CloneTag = clone $Tag; $this->addProductTag($CloneTag); $CloneTag->setProduct($this); } return $this; } /** * Add productDeliveryClass. * * @param \Customize\Entity\ProductDeliveryClass $productDeliveryClass * * @return Product */ public function addProductDeliveryClass(\Customize\Entity\ProductDeliveryClass $productDeliveryClass) { $this->ProductDeliveryClasses[] = $productDeliveryClass; return $this; } /** * Remove productDeliveryClass. * * @param \Customize\Entity\ProductDeliveryClass $productDeliveryClass * * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. */ public function removeProductDeliveryClass(\Customize\Entity\ProductDeliveryClass $productDeliveryClass) { return $this->ProductDeliveryClasses->removeElement($productDeliveryClass); } /** * Get productDeliveryClasses. * * @return \Doctrine\Common\Collections\Collection */ public function getProductDeliveryClasses() { return $this->ProductDeliveryClasses; } } こちらの記事で後から気づきましたが、生成したプロキシを確認した所、拡張前のproductエンティティの内容と同じでした。 既存メソッドが上書きされていない事と、拡張したメソッドが追加されていませんでした。 (参考記事) https://qiita.com/izayoi256/items/cdd8074f7264b5d90f90 その他確認した事として、 /src/Eccube/Entity/Product.php を直接変更 → 今回の現象は再現せず、新規作成〇、複製時の元のデータの属性情報はコピーされていました。 一度キャッシュをクリア → プロキシ生成 → プロキシファイルの内容は、/src/Eccube/Entity/Product.php と同じ → こちらも同様に動作しました。 取り急ぎ、既存ソースに直接手を入れて、今回の現象は再現しなくなりました。 今回のような場合は、参考記事のような付帯処理を入れなければならないでしょうか? 考え方として誤りや不足があれば、ご教示ください。 |
468 |
投稿日時: 2020/10/23 18:24
対応状況: −−−
|
神 登録日: 2008/10/26 居住地: 投稿: 3217 |
Re: 商品新規登録でエラーになる $ProductDeliveryClasses = $Product->getProductDeliveryClasses();
で$ProductDeliveryClasses に空の配列ではなくnullが返ってきていないでしょうか? Productエンティティに追加したgetProductDeliveryClasses()メソッドに何かあるのではないかと思います。 もしくはgetProductDeliveryClassesメソッドで返すフィールド変数の初期化が誤っている可能性はないでしょうか?
|
Rights |
投稿日時: 2020/10/23 14:23
対応状況: −−−
|
一人前 登録日: 2019/3/12 居住地: 投稿: 99 |
商品新規登録でエラーになる 商品情報登録でカテゴリー選択をして登録するロジックを流用して、別の属性情報を登録するロジック追加しましたが、既存の商品情報では追加属性情報の登録・変更はできますが、新規で商品情報を登録する際にエラーとなります。
/app/Customize/Controller/Admin/Product 既存のProductController.phpを複製してカスタマイズ /app/Customize/Entity/ ProductTrait.php 追加属性情報用のEntityを下記ファイルを流用して作成 Category.php ProductCategory.php /app/Customize/Repository/ 追加属性情報用のRepositoryを下記ファイルを流用して作成 CategoryRepository.php ProductCategoryRepository.php /app/Customize/Form/Type /Admin 追加属性情報用のTypeを下記ファイルを流用して作成 CategoryType.php /app/Customize/Form/Extension 商品登録画面の拡張 ProductTypeExtension.php /app/template/admin/Product テンプレートの変更 product.twig 既存商品の追加属性情報の選択追加・変更 → 〇 新規商品の登録 → 商品登録のナビゲーションをクリックした時点でエラー ProductController.php public function edit(Request $request, $id = null, RouterInterface $router, CacheUtil $cacheUtil) (途中省略) $categories = []; $ProductCategories = $Product->getProductCategories(); foreach ($ProductCategories as $ProductCategory) { /* @var $ProductCategory \Eccube\Entity\ProductCategory */ $categories[] = $ProductCategory->getCategory(); } $form['Category']->setData($categories); // ▼ 追加 $delivery_classes = []; $ProductDeliveryClasses = $Product->getProductDeliveryClasses(); foreach ($ProductDeliveryClasses as $ProductDeliveryClass) { ← Warning: Invalid argument supplied for foreach() /* @var $ProductDeliveryClass \Customize\Entity\ProductDeliveryClass */ $delivery_classes[] = $ProductDeliveryClass->getDeliveryClass(); } $form['DeliveryClass']->setData($delivery_classes); // ▲ 追加 商品に関連づけた追加の属性情報が取得できていないため(新規なので取得できていない)だと考えますが、この場合のロジックを実装している箇所が見当たりません。 何か参考になる情報があればご教示ください。 |
スレッド表示 | 古いものから | 前のトピック | 次のトピック | トップ |