注文書の詳細を含む注文書ヘッダーを保存したい.これは私の PurchaseOrder Entity Class =>
namespace AppBundle\Entity;
use AppBundle\Entity\PurchaseInvoiceDetail;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* PurchaseOrder
*
* @ORM\Table(name="purchase_order", indexes={@ORM\Index(name="fk_purchase_order_supplier1_idx", columns={"supplier_id"})})
* @ORM\Entity
*/
class PurchaseOrder
{
/**
* @var PurchaseOrderDetails
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\PurchaseOrderDetails", mappedBy="purchaseOrder",cascade={"cascade"})
* @JMS\Type("ArrayCollection<FinanceBundle\Entity\AutoAllocation>")
*/
private $purchaseOrderDetails;
public function __construct()
{
$this->purchaseOrderDetails = new ArrayCollection();
}
public function addPurchaseOrderDetail(PurchaseOrderDetails $purchaseOrderDetails)
{
$this->purchaseOrderDetails->add($purchaseOrderDetails);
}
/**
* @return PurchaseOrderDetails
*/
public function getPurchaseOrderDetails()
{
return $this->purchaseOrderDetails;
}
/**
* @param PurchaseOrderDetails $purchaseOrderDetails
*/
public function setPurchaseOrderDetails($purchaseOrderDetails)
{
$this->purchaseOrderDetails = $purchaseOrderDetails;
}
}
および PurchaseOrderDetail クラスとして this =>
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* PurchaseOrderDetails
*
* @ORM\Table(name="purchase_order_details", indexes={@ORM\Index(name="fk_purchase_order_details_purchase_order1_idx", columns={"purchase_order_id"}), @ORM\Index(name="fk_purchase_order_details_invt_item1_idx", columns={"id_item"})})
* @ORM\Entity
*/
class PurchaseOrderDetails
{
/**
* @var \AppBundle\Entity\PurchaseOrder
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\PurchaseOrder",inversedBy="purchaseOrderDetails")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="purchase_order_id", referencedColumnName="id")
* })
*/
private $purchaseOrder;
/**
* Set purchaseOrder
*
* @param \AppBundle\Entity\PurchaseOrder $purchaseOrder
*
* @return PurchaseOrderDetails
*/
public function setPurchaseOrder(\AppBundle\Entity\PurchaseOrder $purchaseOrder = null)
{
$this->purchaseOrder = $purchaseOrder;
return $this;
}
/**
* Get purchaseOrder
*
* @return \AppBundle\Entity\PurchaseOrder
*/
public function getPurchaseOrder()
{
return $this->purchaseOrder;
}
}
次のようにsymfony 3.1の私のphpコード=>
$em = $this->getDoctrine()->getManager();
$purchaseOrder = new PurchaseOrder();
$puchaseOrderDetail = new PurchaseOrderDetails();
$puchaseOrderDetail->setPrice(100);
$purchaseOrder->setPurchaseOrderDetails($puchaseOrderDetail);
$puchaseOrderDetail->setPurchaseOrder($purchaseOrder);
$em->persist($purchaseOrder);
$em->flush();
エラーは発生せず、発注書のみが保持され、発注書の詳細は保持されません