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 类作为这个 =>

<?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();

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

4

2 回答 2

2

您没有保留详细信息对象。要么手动坚持它

$em->persist($purchaseOrderDetail);

或修复

cascade={"persist"}

在(的@ORM\OneToMany注释中可能是一个错字)。PurchaseOrder::$purchaseOrderDetailscascade={"cascade"}

于 2016-09-23T09:56:20.030 回答
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();

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

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

@ORM\OneToMany(targetEntity="AppBundle\Entity\PurchaseOrderDetails", mappedBy="purchaseOrder",cascade={"persist"})
于 2016-09-23T11:11:02.577 回答