バージョン選択

フォーラム

メニュー

オンライン状況

80 人のユーザが現在オンラインです。 (67 人のユーザが フォーラム を参照しています。)
登録ユーザ: 1
ゲスト: 79
zerostaff もっと...

サイト内検索

質問 > 管理機能 > 商品に紐づくテーブルで Undefined index

管理機能

新規スレッドを追加する

スレッド表示 | 新しいものから 前のトピック | 次のトピック | 下へ
投稿者 スレッド
h_tanaka
投稿日時: 2018/1/22 10:37
対応状況: 解決済
登録日: 2016/7/22
居住地: 愛媛県
投稿: 1646
商品に紐づくテーブルで Undefined index
EC-CUBE3.0.15

本体カスタマイズで、特集テーブルを作成し、特集に対して複数の商品をひも付けたいと思っています。

テーブルの関連を次のように設定しました。
Feature <- FeatureProduct
Product <- FeatureProduct

管理画面の特集編集ページを作成したのですが、Twigをレンダリングするタイミングでエラーになってしまいます。
どうすれば解決できますでしょうか?

■エラー
Twig_Error_Runtime in Template.php line 230:
An exception has been thrown during the rendering of a template ("Notice: Undefined index: product_id") in "__string_template__49444eaea12e1c4eaf45dff2745c87a2728d331d43ac34790bb480f2ac70b6b9" at line 348.

以下、ソースです。

■SQL
CREATE TABLE IF NOT EXISTS `dtb_feature` (
  `feature_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`feature_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `dtb_feature_product` (
  `feature_product_id` int(11) NOT NULL AUTO_INCREMENT,
  `feature_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  PRIMARY KEY (`feature_product_id`),
  KEY `dtb_feature_product_feature_id` (`feature_id`),
  KEY `dtb_feature_product_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `dtb_feature_product`
  ADD CONSTRAINT `dtb_feature_product_feature_id` FOREIGN KEY (`feature_id`) REFERENCES `dtb_feature` (`feature_id`),
  ADD CONSTRAINT `dtb_feature_product_product_id` FOREIGN KEY (`product_id`) REFERENCES `dtb_product` (`product_id`);


■src/Eccube/Resource/doctrine/Eccube.Entity.Feature.dcm.yml
Eccube\Entity\Feature:
    type: entity
    table: dtb_feature
    repositoryClass: Eccube\Repository\FeatureRepository
    id:
        id:
            type: integer
            nullable: false
            unsigned: false
            id: true
            column: feature_id
            generator:
                strategy: AUTO
    oneToMany:
        FeatureProducts:
            targetEntity: Eccube\Entity\FeatureProduct
            mappedBy: Product
            orderBy:
                rank: ASC
    lifecycleCallbacks: {  }


■src/Eccube/Resource/doctrine/Eccube.Entity.FeatureProduct.dcm.yml
Eccube\Entity\FeatureProduct:
    type: entity
    table: dtb_feature_product
    repositoryClass: Eccube\Repository\FeatureProductRepository
    indexes:
        dtb_feature_product_product_id:
            columns:
                - product_id
    id:
        id:
            type: integer
            nullable: false
            unsigned: false
            id: true
            column: feature_product_id
            generator:
                strategy: AUTO
    manyToOne:
        Product:
            targetEntity: Eccube\Entity\Product
            inversedBy: FeatureProducts
            joinColumn:
                name: product_id
                referencedColumnName: product_id
                nullable: false
        Feature:
            targetEntity: Eccube\Entity\Feature
            joinColumn:
                name: feature_id
                referencedColumnName: feature_id
                nullable: false
    lifecycleCallbacks: {  }


■src/Eccube/Entity/Feature.php
class Feature extends \Eccube\Entity\AbstractEntity
{
    private $id;
    private $FeatureProducts;

    public function __construct()
    {
        $this->FeatureProducts = new ArrayCollection();
    }

    public function addFeatureProduct(\Eccube\Entity\FeatureProduct $featureProduct)
    {
        $this->FeatureProducts[] = $featureProduct;

        return $this;
    }

    public function removeFeatureProduct(\Eccube\Entity\FeatureProduct $featureProduct)
    {
        $this->FeatureProducts->removeElement($featureProduct);
    }

    public function getFeatureProducts()
    {
        return $this->FeatureProducts;
    }
}


■src/Eccube/Entity/FeatureProduct.php
class FeatureProduct extends \Eccube\Entity\AbstractEntity
{
    private $id;
    private $Product;
    private $Feature;

    // Get, Set 省略
}


■src/Eccube/Controller/Admin/Content/FeatureController.php
    public function edit(Application $app, Request $request, $id = null)
    {
        if (is_null($id)) {
            $Feature = new \Eccube\Entity\Feature();
        } else {
            $Feature = $app['eccube.repository.feature']->find($id);
        }
        return $app->render('Content/feature_edit.twig', array(
            'Feature' => $Feature,
            'id' => $id,
        ));
    }


■src/Eccube/Resource/template/admin/Content/feature_edit.twig
                            <table class="table table-hover">
                                <thead style="background-color: #F9F9F9;">
                                <tr>
                                    <td width="20%">商品ID</td>
                                    <td width="70%">商品名</td>
                                    <td width="10%"><input type="checkbox" id="toggle-check-all"></td>
                                </tr>
                                </thead>
                                <tbody>
                                {% for FeatureProduct in Feature.FeatureProducts %} {# ★ ここでエラーになっている ★ #}
                                    <tr class="item_box" data-productid="{{ FeatureProduct.Product.id }}">
                                        <td width="20%">{{ FeatureProduct.Product.id }}</td>
                                        <td width="70%">{{ FeatureProduct.Product.name }}</td>
                                        <td width="10%"><input type="checkbox" id="check-{{ FeatureProduct.Product.id }}" data-productid="{{ FeatureProduct.Product.id }}"></td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>


----------------
EC-CUBE 《プラチナ》ランクパートナー
トエビス株式会社 田中 宏典
EC-CUBEの機能やデザインのカスタマイズ承ります。

h_tanaka
投稿日時: 2018/1/22 11:18
対応状況: −−−
登録日: 2016/7/22
居住地: 愛媛県
投稿: 1646
Re: 商品に紐づくテーブルで Undefined index
自己解決しました。
結局エラーの原因はわかりませんでしたが、直接テーブルからデータを取得することができました。

■src/Eccube/Controller/Admin/Content/FeatureController.php
        $FeatureProducts = $app['eccube.repository.feature_product']->findBy(array('Feature' => $id));

        return $app->render('Content/feature_edit.twig', array(
            'Feature' => $Feature,
            'FeatureProducts' => $arrFeatureProducts,
            'id' => $id,
        ));


----------------
EC-CUBE 《プラチナ》ランクパートナー
トエビス株式会社 田中 宏典
EC-CUBEの機能やデザインのカスタマイズ承ります。

スレッド表示 | 新しいものから 前のトピック | 次のトピック | トップ


 



ログイン


EC-CUBE公式 Amazon Payプラグイン

統計情報

総メンバー数は88,857名です
総投稿数は109,992件です

投稿数ランキング

1
seasoft
7367
2
468
3217
3
AMUAMU
2712
4
nanasess
2313
5
umebius
2085
6
yuh
1819
7
h_tanaka
1646
8
red
1570
9
mcontact
1292
10
tsuji
958
11
fukap
907
12
shutta
835
13
tao_s
799
14 ramrun 789
15 karin 689
16 sumida 641
17
homan
633
18 DELIGHT 572
19
patapata
502
20
flealog
485


ネットショップの壺

EC-CUBEインテグレートパートナー

Copyright© EC-CUBE CO.,LTD. All Rights Reserved.