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の機能やデザインのカスタマイズ承ります。