はじめに
テーブルを追加してControllerなどから利用したい。
テーブル作成~Entity~Repository
テーブル追加
dtb_product2というテーブルを作成
CREATE TABLE `dtb_product2` (
`id` int(10) NOT NULL,
`product_id` int(10) NOT NULL,
`size_a` int(10) NOT NULL,
`size_b` int(10) NOT NULL,
`discriminator_type` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
※"discriminator_type"という列の使い方がちょっと分かっていない。今回の場合は"product2"という値を全ての行に入れる必要があるっぽい。
Entity
app/Customize/Entity/Product2.php
<?php
namespace Customize\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product2
*
* @ORM\Table(name="dtb_product2")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="Customize\Repository\Product2Repository")
*/
class Product2 extends \Eccube\Entity\AbstractEntity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="product_id", type="integer", options={"unsigned":true})
*/
private $product_id;
/**
* @var integer
*
* @ORM\Column(name="size_a", type="integer", options={"unsigned":true})
*/
private $size_a;
/**
* @var integer
*
* @ORM\Column(name="size_b", type="integer", options={"unsigned":true})
*/
private $size_b;
/**
* @return string
*/
public function __toString()
{
return sprintf("[%d] a:%d b:%d", $this->getProductId(), $this->getSizeA(), $this->getSizeB());
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set productId.
*
* @param int|null $productId
*
* @return Product2
*/
public function setProductId($productId = null)
{
$this->product_id = $productId;
return $this;
}
/**
* Get productId.
*
* @return int|null
*/
public function getProductId()
{
return $this->product_id;
}
/**
* Set sizeA.
*
* @param int|null $sizeA
*
* @return Product2
*/
public function setSizeA($sizeA = null)
{
$this->size_a = $sizeA;
return $this;
}
/**
* Get sizeA.
*
* @return int|null
*/
public function getSizeA()
{
return $this->size_a;
}
/**
* Set sizeB.
*
* @param int|null $sizeB
*
* @return Product2
*/
public function setSizeB($sizeB = null)
{
$this->size_b = $sizeB;
return $this;
}
/**
* Get sizeB.
*
* @return int|null
*/
public function getSizeB()
{
return $this->size_b;
}
}
Repository
app/Customize/Repository/Product2Repository.php
<?php
namespace Customize\Repository;
use Customize\Entity\Product2;
use Eccube\Repository\AbstractRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* Product2Repository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class Product2Repository extends AbstractRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Product2::class);
}
}
Controllerよりアクセス
ControllerからRepository経由でアクセス
app/Customize/Controller/SamplePageController.php
namespace Customize\Controller;
use Customize\Repository\Product2Repository;
use Eccube\Controller\AbstractController;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Product;
use Customize\Entity\Product2;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class SamplePageController extends AbstractController
{
/** @var BaseInfo */
protected $baseInfo;
/** @var Product */
protected $productRepository;
/** @var Product2 */
protected $product2Repository;
/**
* SamplePageController constructor.
* @param BaseInfoRepository $baseInfoRepository
* @param ProductRepository $productRepository
* @param Product2Repository $product2Repository
*/
public function __construct(
BaseInfoRepository $baseInfoRepository,
ProductRepository $productRepository,
Product2Repository $product2Repository
)
{
$this->baseInfo = $baseInfoRepository->get();
$this->productRepository = $productRepository;
$this->product2Repository = $product2Repository;
}
/**
* @Route("/sample/{id}", name="sample_index")
* @Template("Sample/index.twig")
*/
public function index($id)
{
$product = $this->productRepository->find($id);
$product2 = $this->product2Repository->find($id);
return [
'shop_name' => $this->baseInfo->getShopName(),
'maker_name' => $product->getMakerName(),
'product_name' => $product->getName(),
'size_a' => $product2->getSizeA(),
];
}
}
Template
app/template/default/Sample/index.twig
{% extends 'default_frame.twig' %}
{% block main %}
店舗名は"{{ shop_name }}"です。<br/>
メーカーは"{{ maker_name }}"です。<br/>
商品名は"{{ product_name }}"です。<br/>
サイズAは"{{ size_a }}"です。<br/>
{% endblock %}
ブラウザよりアクセス
/sample/1 で指定したsize_aが表示された。
まとめ
Entity,Repositoryを作成してテーブルにアクセスする。
テーブルを作成してからEntityを作成したわけだけど、本来であればEntityからSymfonyの機能でテーブルを作成したりするのかな?
Repositoryで外部キーの設定とかきちんとしないといけないけどまた別途。