Doctrine 2只保存头对象为什么?

时间:2016-09-23 09:51:19

标签: php doctrine entity persistence symfony-3.1

我希望在采购订单详细信息中保存采购订单标题。这是我的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类为this =&gt;

<?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代码如下=&gt;

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

没有发生任何错误,只有采购订单持续存在且采购订单明细不存在

2 个答案:

答案 0 :(得分:1)

您没有持久保存详细信息对象。使用

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

或修复

cascade={"persist"}
@ORM\OneToMany PurchaseOrder::$purchaseOrderDetails注释中的

cascade={"cascade"}可能是拼写错误。)

答案 1 :(得分:1)

您还需要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所提到的,如果您的业务逻辑需要,并且您在创建新persist时并不想PurchaseOrderDetails单独PurchaseOrder实体。您可以考虑配置cascade_persist。这将自动保持关联的实体。

为此,请将cascade选项添加到关联配置:

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