Doctrine 2 persist only save header object 为什么?

Doctrine 2 persist only save header object why?

我想保存采购订单 header 和采购订单 details.This 我的采购订单实体 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 class 像这样 =>

<?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;
}
}

我的php在symfony 3.1中的代码如下=>

$em = $this->getDoctrine()->getManager();
$purchaseOrder = new PurchaseOrder();
$puchaseOrderDetail = new PurchaseOrderDetails();
$puchaseOrderDetail->setPrice(100);
$purchaseOrder->setPurchaseOrderDetails($puchaseOrderDetail);
$puchaseOrderDetail->setPurchaseOrder($purchaseOrder);
$em->persist($purchaseOrder);
$em->flush();

没有发生错误,只有采购订单存在,采购订单详细信息没有

您没有保留详细信息对象。使用

手动保留它
$em->persist($purchaseOrderDetail);

或修复

cascade={"persist"}

PurchaseOrder::$purchaseOrderDetails@ORM\OneToMany注释中(cascade={"cascade"}可能是打字错误)。

您还需要 persist PurchaseOrderDetails

下面的代码应该保存你的两个实体。

$em = $this->getDoctrine()->getManager();
$purchaseOrder = new PurchaseOrder();
$puchaseOrderDetail = new PurchaseOrderDetails();
$puchaseOrderDetail->setPrice(100);
$purchaseOrder->setPurchaseOrderDetails($puchaseOrderDetail);
$puchaseOrderDetail->setPurchaseOrder($purchaseOrder);
$em->persist($purchaseOrder);
$em->persist($puchaseOrderDetail);
$em->flush();

正如@Finwe 所提到的,如果您的业务逻辑需要,并且您不想在创建新的 PurchaseOrder 时单独 persist PurchaseOrderDetails 实体。您可以考虑配置 cascade_persist。这将持续自动关联的实体。

为此,请将级联选项添加到您的关联配置中:

@ORM\OneToMany(targetEntity="AppBundle\Entity\PurchaseOrderDetails", mappedBy="purchaseOrder",cascade={"persist"})