質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
EC-CUBE

EC-CUBEは、主に日本国内で開発されているECコンテンツ管理システムです。ロックオン社のECKitを元にしてオープンソース化され、商品管理・受注管理・顧客管理・売上集計などECに特化した様々な機能を備えています。

Q&A

0回答

5475閲覧

EC-CUBE3.0.10 商品一覧での複数カテゴリ絞り込み repository拡張

Tenchara

総合スコア8

EC-CUBE

EC-CUBEは、主に日本国内で開発されているECコンテンツ管理システムです。ロックオン社のECKitを元にしてオープンソース化され、商品管理・受注管理・顧客管理・売上集計などECに特化した様々な機能を備えています。

0グッド

0クリップ

投稿2016/09/27 04:54

###前提・実現したいこと
ec-cube(3.0.10)を使ってECサイトを構築しています。

商品一覧ページでカテゴリAとカテゴリBのAND条件による絞り込み(dtb_productテーブルの"category_id"を最大2つ指定)の実装をしているのですが、詰まってしまったので相談させてください。

もしよければアドバイスをお願いします。

###現在の状態
「ec-cube本体は修正しない」という方針のため、プラグインによる実装を選択しました。

ざっくりとですが、以下1〜4の処理を実装して絞り込みは実現できました。

  1. 商品一覧ページに"親category_id"と"子category_id"をGETパラメータで渡すようリンク設置。(formに追加済)
  2. プラグインのproductRepository.phpで上記2を利用したクエリ追加。
  3. serviceProviderでプラグインのproductRepository.phpに記載した処理を参照するよう指定。

しかし、管理画面の商品編集ページをひらくとシステムエラー(ec-cube側のエラー)が発生するようになってしまいました。

そのため、管理画面に影響のないよう修正したいのですが、どのように修正すればよいかの方針が立てられていない状態です。

###エラーメッセージ
ec-cube管理画面から参照できる"EC-CUBEログ"

[2016-09-27 12:02:33] eccube.INFO: < 500 [] [] SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.product_id' in 'where clause' (uncaught exception) at /var/www/html/ecsite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 71 {"exception":"[object] (Doctrine\\DBAL\\Exception\\InvalidFieldNameException(code: 0): An exception occurred while executing 'SELECT t1.product_id AS product_id2, t1.name AS name3, t1.note AS note4, t1.description_list AS description_list5, t1.description_detail AS description_detail6, t1.search_word AS search_word7, t1.free_area AS free_area8, t1.del_flg AS del_flg9, t1.create_date AS create_date10, t1.update_date AS update_date11, t1.creator_id AS creator_id12, t1.status AS status13 FROM dtb_product t1 WHERE t0.product_id = ? AND ((t1.del_flg = 0))' with params [\"8\"]:\n\nSQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.product_id' in 'where clause' at /var/www/html/ecsite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:71, Doctrine\\DBAL\\Driver\\PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.product_id' in 'where clause' at /var/www/html/ecsite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:93, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.product_id' in 'where clause' at /var/www/html/ecsite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91)"} [] [2016-09-27 12:02:33] eccube.CRITICAL: Doctrine\DBAL\Exception\InvalidFieldNameException: An exception occurred while executing 'SELECT t1.product_id AS product_id2, t1.name AS name3, t1.note AS note4, t1.description_list AS description_list5, t1.description_detail AS description_detail6, t1.search_word AS search_word7, t1.free_area AS free_area8, t1.del_flg AS del_flg9, t1.create_date AS create_date10, t1.update_date AS update_date11, t1.creator_id AS creator_id12, t1.status AS status13 FROM dtb_product t1 WHERE t0.product_id = ? AND ((t1.del_flg = 0))' with params ["8"]:

※apacheのエラーログは出力されていませんでした。

###該当のソースコード
プライグインのファイル構成

app/ |- Plugin/ |- CustomProductList/ |- config.yml |- event.yml |- CustomProductListEvent.php |- PluginManager.php |- Entity/ |- Product.php |- Repository/ |- ProductRepository.php |- Resource/ |- doctrine/ |- Plugin.CustomProductList.Entity.Product.dcm.yml |- ServiceProvider/ |- CustomProductListServiceProvider.php

config.yml

yaml

1name: カスタム商品一覧 2code: CustomProductList 3version: 1.0.0 4service: 5 - CustomProductListServiceProvider 6orm.path: 7 - /Resource/doctrine 8event: CustomProductListEvent

Entity/product.php

php

1<?php 2namespace Plugin\CustomProductList\Entity; 3 4use Eccube\Entity\Product as BaseProduct; 5 6class Product extends BaseProduct 7{ 8 // 継承のみ 9}

Repository/ProductRepository.php

php

1<?php 2namespace Plugin\CustomProductList\Repository; 3 4use Eccube\Repository\ProductRepository as BaseRepository; 5use Eccube\Common\Constant; 6use Doctrine\ORM\NoResultException; 7use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 8 9class ProductRepository extends BaseRepository 10{ 11 public function getQueryBuilderBySearchData($searchData) 12 { 13 $qb = $this->createQueryBuilder('p') 14 ->andWhere('p.Status = 1'); 15 16 // category 17 $categoryJoin = false; 18 if (!empty($searchData['category_id']) && $searchData['category_id']) { 19 $Categories = $searchData['category_id']->getSelfAndDescendants(); 20 if ($Categories) { 21 $qb 22 ->innerJoin('p.ProductCategories', 'pct') 23 ->innerJoin('pct.Category', 'c') 24 ->andWhere($qb->expr()->in('pct.Category', ':Categories')) 25 ->setParameter('Categories', $Categories); 26 // 複数カテゴリによる絞り込み START 27 if (!is_null($searchData['main_category_id']) && is_numeric($searchData['main_category_id'])) { 28 $qb 29 ->innerJoin('p.ProductCategories', 'pct2') 30 ->innerJoin('pct2.Category', 'c2') 31 ->andWhere($qb->expr()->in('pct2.Category', ':MainCategoryId')) 32 ->setParameter('MainCategoryId', $searchData['main_category_id']); 33 } 34 // 複数カテゴリによる絞り込み END 35 $categoryJoin = true; 36 } 37 } 38 39 // name 40 if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) { 41 $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY); 42 43 foreach ($keywords as $index => $keyword) { 44 $key = sprintf('keyword%s', $index); 45 $qb 46 ->andWhere(sprintf('p.name LIKE :%s OR p.search_word LIKE :%s', $key, $key)) 47 ->setParameter($key, '%' . $keyword . '%'); 48 } 49 } 50 51 // Order By 52 // 価格順 53 if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '1') { 54 //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html 55 $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min'); 56 $qb->innerJoin('p.ProductClasses', 'pc'); 57 $qb->groupBy('p'); 58 $qb->orderBy('price02_min', 'ASC'); 59 // 新着順 60 } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '2') { 61 $qb->orderBy('p.create_date', 'DESC'); 62 } else { 63 if ($categoryJoin === false) { 64 $qb 65 ->leftJoin('p.ProductCategories', 'pct') 66 ->leftJoin('pct.Category', 'c'); 67 } 68 $qb 69 ->addOrderBy('p.id', 'DESC'); 70 } 71 72 return $qb; 73 } 74}

Resource/doctrine/Plugin.CustomProductList.Entity.Product.dcm.yml

yaml

1Plugin\CustomProductList\Entity\Product: 2 type: entity 3 table: dtb_product 4 repositoryClass: Plugin\CustomProductList\Repository\ProductRepository

ServiceProvider/CustomProductListServiceProvider.php

php

1<?php 2namespace Plugin\CustomProductList\ServiceProvider; 3 4use Eccube\Application; 5use Silex\Application as BaseApplication; 6use Silex\ServiceProviderInterface; 7 8class CustomProductListServiceProvider implements ServiceProviderInterface 9{ 10 public function register(BaseApplication $app) 11 { 12 $app['eccube.repository.product'] = $app->share(function () use ($app) { 13 $productRepository = $app['orm.em']->getRepository('Plugin\CustomProductList\Entity\Product'); 14 return $productRepository; 15 }); 16 } 17 18 public function boot(BaseApplication $app) 19 { 20 } 21}

###試したこと
ServiceProvider/CustomProductListServiceProvider.php

上記プログラムの"register"の処理をコメントアウト
→ プラグイン側のgetQueryBuilderBySearchDataが読み込まれない。(複数カテゴリ絞り込めない)
→ 管理画面側のエラーはなくなる。

となるので、entityの指定方法がおかしい可能性が高そうなんですが「じゃあどうすればいい」という方針まで辿りつかず・・。

###補足情報(言語/FW/ツール等のバージョンなど)
PHP 7.0.10
MySQL 5.5.45
ec-cube 3.0.10

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問