原则不保存一对一参考字段

时间:2016-10-17 01:38:12

标签: symfony doctrine-orm

我有一个RequestForEstimate实体,在我的逻辑中的某个时刻到达我创建PurchaseOrder的点,我需要在PurchaseOrder表中插入一个RequestId。我通过使用学说中的一对一关联来参考它。由于某种原因,DB调用成功,但是当我检查表时,看起来request_estimate_id字段为null。这是我的逻辑:

RequestForEstimate实体:

<?php

namespace InboundBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RequestForEstimate
 *
 * @ORM\Table(name="request_for_estimate")
 * @ORM\Entity(repositoryClass="InboundBundle\Repository\RequestForEstimateRepository")
 */
class RequestForEstimate
{
    /**
     * @var int
     *
     * @ORM\Column(name="request_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_time", type="datetime")
     */
    private $createTime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="update_time", type="datetime")
     */
    private $updateTime;    
    /**
     * @ORM\ManyToOne(targetEntity="RequestStatus", cascade={"persist"})
     */

    private $status;
    /**
     * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Product")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
     */
    private $product;

    /**
     * @var int
     *
     * @ORM\Column(name="quantity", type="integer")
     */
    private $quantity = 0;

    /**
     * @var string
     *
     * @ORM\Column(name="price_per_unit", type="decimal", precision=10, scale=2)
     */
    private $pricePerUnit = 0;

    /**
     * @var string
     *
     * @ORM\Column(name="shipping_cost", type="decimal", precision=10, scale=2)
     */
    private $shippingCost = 0;

    /**
     * @var string
     *
     * @ORM\Column(name="package_cost", type="decimal", precision=10, scale=2)
     */
    private $packageCost = 0;

    /**
     * @var string
     *
     * @ORM\Column(name="other_fees", type="decimal", precision=10, scale=2)
     */
    private $otherFees = 0;

    /**
     * @var integer
     *
     * @ORM\Column(name="deposit_required", type="integer")
     */
    private $depositRequired = 0;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set product
     *
     * @param integer $product
     *
     * @return RequestForEstimate
     */
    public function setProduct($product)
    {
        $this->product = $product;

        return $this;
    }

    /**
     * Get product
     *
     * @return int
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     *
     * @return RequestForEstimate
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * Get quantity
     *
     * @return int
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * Set pricePerUnit
     *
     * @param string $pricePerUnit
     *
     * @return RequestForEstimate
     */
    public function setPricePerUnit($pricePerUnit)
    {
        $this->pricePerUnit = $pricePerUnit;

        return $this;
    }

    /**
     * Get pricePerUnit
     *
     * @return string
     */
    public function getPricePerUnit()
    {
        return $this->pricePerUnit;
    }

    /**
     * Set shippingCost
     *
     * @param string $shippingCost
     *
     * @return RequestForEstimate
     */
    public function setShippingCost($shippingCost)
    {
        $this->shippingCost = $shippingCost;

        return $this;
    }

    /**
     * Get shippingCost
     *
     * @return string
     */
    public function getShippingCost()
    {
        return $this->shippingCost;
    }

    /**
     * Set otherFees
     *
     * @param string $otherFees
     *
     * @return RequestForEstimate
     */
    public function setOtherFees($otherFees)
    {
        $this->otherFees = $otherFees;

        return $this;
    }

    /**
     * Get otherFees
     *
     * @return string
     */
    public function getOtherFees()
    {
        return $this->otherFees;
    }

    /**
     * Set requestId
     *
     * @param \InboundBundle\Entity\RequestForEstimate $requestId
     *
     * @return RequestForEstimate
     */
    public function setRequestId(\InboundBundle\Entity\RequestForEstimate $requestId = null)
    {
        $this->requestId = $requestId;

        return $this;
    }

    /**
     * Get requestId
     *
     * @return \InboundBundle\Entity\RequestForEstimate
     */
    public function getRequestId()
    {
        return $this->requestId;
    }

    /**
     * Set productId
     *
     * @param \InboundBundle\Entity\Product $productId
     *
     * @return RequestForEstimate
     */
    public function setProductId(\InboundBundle\Entity\Product $productId = null)
    {
        $this->productId = $productId;

        return $this;
    }

    /**
     * Get productId
     *
     * @return \InboundBundle\Entity\Product
     */
    public function getProductId()
    {
        return $this->productId;
    }

    /**
     * Constructor
     */
    public function __construct()
    {

    }

    /**
     * Set depositRequired
     *
     * @param string $depositRequired
     *
     * @return RequestForEstimate
     */
    public function setDepositRequired($depositRequired)
    {
        $this->depositRequired = $depositRequired;

        return $this;
    }

    /**
     * Get depositRequired
     *
     * @return string
     */
    public function getDepositRequired()
    {
        return $this->depositRequired;
    }

    /**
     * Set packageCost
     *
     * @param string $packageCost
     *
     * @return RequestForEstimate
     */
    public function setPackageCost($packageCost)
    {
        $this->packageCost = $packageCost;

        return $this;
    }

    /**
     * Get packageCost
     *
     * @return string
     */
    public function getPackageCost()
    {
        return $this->packageCost;
    }

    /**
     * Set status
     *
     * @param \InboundBundle\Entity\RequestStatus $status
     *
     * @return RequestForEstimate
     */
    public function setStatus(\InboundBundle\Entity\RequestStatus $status = null)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return \InboundBundle\Entity\RequestStatus
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set createTime
     *
     * @param \DateTime $createTime
     *
     * @return RequestForEstimate
     */
    public function setCreateTime($createTime)
    {
        $this->createTime = $createTime;

        return $this;
    }

    /**
     * Get createTime
     *
     * @return \DateTime
     */
    public function getCreateTime()
    {
        return $this->createTime;
    }

    /**
     * Set updateTime
     *
     * @param \DateTime $updateTime
     *
     * @return RequestForEstimate
     */
    public function setUpdateTime($updateTime)
    {
        $this->updateTime = $updateTime;

        return $this;
    }

    /**
     * Get updateTime
     *
     * @return \DateTime
     */
    public function getUpdateTime()
    {
        return $this->updateTime;
    }
}

PurchaseOrder实体:

<?php

namespace InboundBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * PurchaseOrder
 *
 * @ORM\Table(name="purchase_order")
 * @ORM\Entity(repositoryClass="InboundBundle\Repository\PurchaseOrderRepository")
 */
class PurchaseOrder
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="RequestForEstimate", cascade={"persist"})
     * @ORM\JoinColumn(name="request_estimate_id", referencedColumnName="request_id")
     */
    private $requestId;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_time", type="datetime")
     */
    private $createTime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="update_time", type="datetime")
     */
    private $updateTime;

    /**
     * @var int
     *
     * @ORM\Column(name="status", type="integer")
     */
    private $status;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="ship_date", type="date", nullable=true)
     */
    private $shipDate;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set createTime
     *
     * @param \DateTime $createTime
     *
     * @return PurchaseOrder
     */
    public function setCreateTime($createTime)
    {
        $this->createTime = $createTime;

        return $this;
    }

    /**
     * Get createTime
     *
     * @return \DateTime
     */
    public function getCreateTime()
    {
        return $this->createTime;
    }

    /**
     * Set updateTime
     *
     * @param \DateTime $updateTime
     *
     * @return PurchaseOrder
     */
    public function setUpdateTime($updateTime)
    {
        $this->updateTime = $updateTime;

        return $this;
    }

    /**
     * Get updateTime
     *
     * @return \DateTime
     */
    public function getUpdateTime()
    {
        return $this->updateTime;
    }

    /**
     * Set status
     *
     * @param integer $status
     *
     * @return PurchaseOrder
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return int
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set shipDate
     *
     * @param \DateTime $shipDate
     *
     * @return PurchaseOrder
     */
    public function setShipDate($shipDate)
    {
        $this->shipDate = $shipDate;

        return $this;
    }

    /**
     * Get shipDate
     *
     * @return \DateTime
     */
    public function getShipDate()
    {
        return $this->shipDate;
    }

    /**
     * Set requestForEstimate
     *
     * @param \InboundBundle\Entity\RequestForEstimate $requestForEstimate
     *
     * @return PurchaseOrder
     */
    public function setRequestForEstimate(\InboundBundle\Entity\RequestForEstimate $requestForEstimate = null)
    {
        $this->requestForEstimate = $requestForEstimate;

        return $this;
    }

    /**
     * Get requestForEstimate
     *
     * @return \InboundBundle\Entity\RequestForEstimate
     */
    public function getRequestForEstimate()
    {
        return $this->requestForEstimate;
    }

    /**
     * Set requestId
     *
     * @param $requestId
     *
     * @return PurchaseOrder
     */
    public function setRequestId($requestId)
    {
        $this->request_id = $requestId;

        return $this;
    }

    /**
     * Get requestId
     *
     * @return \InboundBundle\Entity\RequestForEstimate
     */
    public function getRequestId()
    {
        return $this->request_id;
    }
}

我的控制器:

 /**
     * @Route("request-for-estimate/confirm/{id}", name="request_confirm")
     */
    public function confirmRequest(RequestForEstimate $RequestForEstimate)
    {

        $repo = $this->getDoctrine()->getRepository('InboundBundle:RequestStatus'); 
        $status = $repo->findOneById('6');
        $RequestForEstimate->setupdateTime(new \DateTime());
        $RequestForEstimate->setstatus($status);

        $PurchaseOrder = new PurchaseOrder();
        $PurchaseOrder->setRequestId($RequestForEstimate);

        $PurchaseOrder->setupdateTime(new \DateTime());
        $PurchaseOrder->setcreateTime(new \DateTime());
        $PurchaseOrder->setstatus(1);
        $em1 = $this->getDoctrine()->getManager();
        $em1->persist($PurchaseOrder);
        $em1->flush();

        $em = $this->getDoctrine()->getManager();
        $em->persist($RequestForEstimate);
        $em->flush();



        return $this->redirectToRoute('requests_for_estimate_view');

    }  

1 个答案:

答案 0 :(得分:1)

我将从我的一个项目中给你一个例子。整个技巧是实体设置器和级联器配置。 希望它会有所帮助。

实体:

class Agreement
{
    // ...

    /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Requisites", mappedBy="agreement", cascade={"persist"})
     */
    private $requisites;

    // ...

    public function setRequisites(Requisites $requisites = null)
    {
        $this->requisites = $requisites;
        $requisites->setAgreement($this);

        return $this;
    }

    public function getRequisites()
    {
        return $this->requisites;
    }
}

class Requisites
{
    // ...

    /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Agreement", inversedBy="requisites")
     * @ORM\JoinColumn(name="Agreement_id", referencedColumnName="id")
     */
    private $agreement;

    // ...

    public function setAgreement(Agreement $agreement = null)
    {
        $this->agreement = $agreement;

        return $this;
    }

    public function getAgreement()
    {
        return $this->agreement;
    }
}

控制器:

if (null === $agreement->getRequisites()) {
    $agreement->setRequisites(new Requisites());
}

$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($agreement);
$entityManager->flush();