OneToMany データ追加(Insert)

# はじめに
OneToManyの関係のテーブルにデータを追加(Insert)する。
dtb_product_commentというテーブルを作成し、dtb_productとproduct_idでOneToManyする。

# Entity作成～テーブル作成
## dtb_product_commentテーブル作成
dtb_product_sizeは以下のイメージ

```sql
CREATE TABLE `dtb_product_comment` (
  `id` int(10) UNSIGNED NOT NULL,
  `product_id` int(10) UNSIGNED NOT NULL,
  `comment` longtext DEFAULT NULL,
  `create_date` datetime NOT NULL COMMENT '(DC2Type:datetimetz)',
  `update_date` datetime NOT NULL COMMENT '(DC2Type:datetimetz)',
  `discriminator_type` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

## Entity(1)

```php:app/Customize/Entity/ProductComment.php
<?php

namespace Customize\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;


/**
 * ProductComment
 *
 * @ORM\Table(name="dtb_product_comment")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity(repositoryClass="Customize\Repository\ProductCommentRepository")
 */
class ProductComment 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 string|null
     *
     * @ORM\Column(name="comment", type="text", nullable=true)
     */
    private $comment;

    /**
     * @return string
     */
    public function __toString()
    {
        return sprintf("[%d] %s", $this->getProductId(), $this->getComment());
    }

    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set productId.
     *
     * @param int|null $productId
     *
     * @return ProductSize
     */
    public function setProductId($productId = null)
    {
        $this->product_id = $productId;

        return $this;
    }

    /**
     * Get productId.
     *
     * @return int|null
     */
    public function getProductId()
    {
        return $this->product_id;
    }

    /**
     * Set comment.
     *
     * @param string|null $comment
     *
     * @return ProductComment
     */
    public function setComment($comment = null)
    {
        $this->comment = $comment;

        return $this;
    }

    /**
     * Get comment.
     *
     * @return string|null
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_date", type="datetimetz")
     */
    private $create_date;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="update_date", type="datetimetz")
     */
    private $update_date;

    /**
     * Set createDate.
     *
     * @param \DateTime $createDate
     *
     * @return Product
     */
    public function setCreateDate($createDate)
    {
        $this->create_date = $createDate;

        return $this;
    }

    /**
     * Get createDate.
     *
     * @return \DateTime
     */
    public function getCreateDate()
    {
        return $this->create_date;
    }

    /**
     * Set updateDate.
     *
     * @param \DateTime $updateDate
     *
     * @return Product
     */
    public function setUpdateDate($updateDate)
    {
        $this->update_date = $updateDate;

        return $this;
    }

    /**
     * Get updateDate.
     *
     * @return \DateTime
     */
    public function getUpdateDate()
    {
        return $this->update_date;
    }

    /**
     * @var \Eccube\Entity\Product
     *
     * @ORM\ManyToOne(targetEntity="Eccube\Entity\Product", inversedBy="ProductComments")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     * })
     */
    private $Product;

    /**
     * Set product.
     *
     * @param \Eccube\Entity\Product|null $product
     *
     * @return ProductSize
     */
    public function setProduct(\Eccube\Entity\Product $product = null)
    {
        $this->Product = $product;

        return $this;
    }

    /**
     * Get product.
     *
     * @return \Eccube\Entity\Product|null
     */
    public function getProduct()
    {
        return $this->Product;
    }

}
```

## Repository

```php:app/Customize/Repository/ProductCommentRepository.php
<?php

namespace Customize\Repository;

use Customize\Entity\ProductComment;
use Eccube\Repository\AbstractRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * ProductCommentRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductCommentRepository extends AbstractRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, ProductComment::class);
    }
}
```

## Entity(2)

```php:app/Customize/Entity/ProductTrait.php
<?php

namespace Customize\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation\EntityExtension;

/**
  * @EntityExtension("Eccube\Entity\Product")
 */
trait ProductTrait
{
    :
    :
    /**
     * @var \Customize\Entity\ProductComment
     *
     * @ORM\OneToMany(targetEntity="Customize\Entity\ProductComment", mappedBy="Product", cascade={"persist","remove"})
     */
    private $ProductComments;

    /**
     * Add productComment.
     *
     * @param \Customize\Entity\ProductComment $productComment
     *
     * @return Product
     */
    public function addProductComment(\Customize\Entity\ProductComment $productComment)
    {
        if ($this->ProductComments == null) {
            $this->ProductComments = new \Doctrine\Common\Collections\ArrayCollection();
        }
        $this->ProductComments[] = $productComment;

        return $this;
    }

    /**
     * Remove productComment.
     *
     * @param \Customize\Entity\ProductComment $productComment
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeProductComment(\Customize\Entity\ProductComment $productComment)
    {
        if ($this->ProductComments == null) {
            $this->ProductComments = new \Doctrine\Common\Collections\ArrayCollection();
        }
        return $this->ProductComments->removeElement($productComment);
    }

    /**
     * Get productComments.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getProductComments()
    {
        if ($this->ProductComments == null) {
            $this->ProductComments = new \Doctrine\Common\Collections\ArrayCollection();
        }
        return $this->ProductComments;
    }

}
```

## mappedBy/inversedBy
dtb_productが親テーブルでOneToMany dtb_product_commentが子テーブル

親エンティティでmappedBy→子エンティティで指定する自分

```php:app/Customize/Entity/ProductTrait.php
/**
 * @var \Customize\Entity\ProductComment
 *
 * @ORM\OneToMany(targetEntity="Customize\Entity\ProductComment", mappedBy="Product", cascade={"persist","remove"})
 */
private $ProductComments;
```


子エンティティでManyToOne inversedBy→親エンティティで指定する自分

```php:app/Customize/Entity/ProductComment.php
/**
 * @var \Eccube\Entity\Product
 *
 * @ORM\ManyToOne(targetEntity="Eccube\Entity\Product", inversedBy="ProductComments")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 * })
 */
private $Product;
```

## テーブル作成
doctrine機能を利用してテーブル作成
(自動で外部キーとかも設定してくれるので)

```sh
php bin/console cache:clear --no-warmup
php bin/console eccube:generate:proxies #dtb_product用proxyを作成していない場合
php bin/console doctrine:schema:update --dump-sql
php bin/console doctrine:schema:update --dump-sql --force
```

作成結果

```sql
CREATE TABLE dtb_product_comment (
 id INT UNSIGNED AUTO_INCREMENT NOT NULL, 
 product_id INT UNSIGNED NOT NULL, 
 comment LONGTEXT DEFAULT NULL, 
 create_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)', 
 update_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)', 
 discriminator_type VARCHAR(255) NOT NULL, 
 INDEX IDX_E9383F534584665A (product_id), 
 PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB;
ALTER TABLE dtb_product_comment ADD CONSTRAINT FK_E9383F534584665A FOREIGN KEY (product_id) REFERENCES dtb_product (id);
```

# データ追加(Insert)
## Controllerよりアクセス
ControllerからentityManager経由でアクセス

```php:app/Customize/Controller/SamplePageController.php
/**
 * @Route("/sample/{id}", name="sample_index")
 * @Template("Sample/index.twig")
 */
public function index($id)
{
    $product = $this->productRepository->find($id);

    $comments = $product->getProductComments();
    if (!$comments->isEmpty()) {
    } else {
        $comment1 = new ProductComment();
        $comment = sprintf("=%s\nproductId%d\n==1==", date("Y/m/d H:i:s"),$product->getId());
        $comment1->setProductId($product->getId())->setComment($comment)->setProduct($product);
        $comment2 = new ProductComment();
        $comment = sprintf("=%s\r\nproductId%d\r\n==2==", date("Y/m/d H:i:s"),$product->getId());
        $comment2->setProductId($product->getId())->setComment($comment)->setProduct($product);
        $product->addProductComment($comment1)->addProductComment($comment2);
        dump($product);
        $this->entityManager->persist($product);
        $this->entityManager->flush();
    }

    return [
        'shop_name' => $this->baseInfo->getShopName(),
        'maker_name' => $product->getMakerName(),
        'product_name' => $product->getName(),
        'size_a' => $productSize->getSizeA(),
    ];
}
```


## ブラウザよりアクセス
/sample/1 でdtb_productとdtb_product_commentにデータ追加(Insert)された。

```
INSERT INTO dtb_product_comment (product_id, comment, create_date, update_date, discriminator_type) VALUES (?, ?, ?, ?, ?)

Parameters:

[▼
  1 => 1
  2 => """
    =2019/01/08 12:17:58\n
    productId1\n
    ==1==
    """
  3 => "2019-01-08 03:17:58"
  4 => "2019-01-08 03:17:58"
  5 => "productcomment"
]

INSERT INTO dtb_product_comment (product_id, comment, create_date, update_date, discriminator_type) VALUES (?, ?, ?, ?, ?)

Parameters:

[▼
  1 => 1
  2 => """
    =2019/01/08 12:17:58\r\n
    productId1\r\n
    ==2==
    """
  3 => "2019-01-08 03:17:58"
  4 => "2019-01-08 03:17:58"
  5 => "productcomment"
]
```

#まとめ
EntityのmappedBy/inversedByでそれぞれ相手オブジェクトを指定してpersist()/flush()する。

#参照
[OneToOne データ追加(Insert)](https://qiita.com/...)