我想保存带有采购订单详细信息的采购订单标题。这是我的 PurchaseOrder 实体类=>
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 类作为这个 =>
<?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();
没有发生错误,只有采购订单持续存在,采购订单详细信息不存在