#symfony #doctrine-orm
#symfony #doctrine-orm
Вопрос:
У меня есть объект RequestForEstimate, который в какой-то момент моей логики доходит до того, что я создаю PurchaseOrder, и мне нужно вставить RequestID в таблицу PurchaseOrder. Я ссылаюсь на него, используя ассоциацию «Один к одному» в doctrine. По какой-то причине вызов БД выполнен успешно, однако, когда я проверяю таблицу, похоже, что поле request_estimate_id равно нулю. Вот моя логика:
Объект RequestForEstimate:
<?php
namespace InboundBundleEntity;
use DoctrineORMMapping as ORM;
/**
* RequestForEstimate
*
* @ORMTable(name="request_for_estimate")
* @ORMEntity(repositoryClass="InboundBundleRepositoryRequestForEstimateRepository")
*/
class RequestForEstimate
{
/**
* @var int
*
* @ORMColumn(name="request_id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DateTime
*
* @ORMColumn(name="create_time", type="datetime")
*/
private $createTime;
/**
* @var DateTime
*
* @ORMColumn(name="update_time", type="datetime")
*/
private $updateTime;
/**
* @ORMManyToOne(targetEntity="RequestStatus", cascade={"persist"})
*/
private $status;
/**
* @ORMManyToOne(targetEntity="CoreBundleEntityProduct")
* @ORMJoinColumn(name="product_id", referencedColumnName="product_id")
*/
private $product;
/**
* @var int
*
* @ORMColumn(name="quantity", type="integer")
*/
private $quantity = 0;
/**
* @var string
*
* @ORMColumn(name="price_per_unit", type="decimal", precision=10, scale=2)
*/
private $pricePerUnit = 0;
/**
* @var string
*
* @ORMColumn(name="shipping_cost", type="decimal", precision=10, scale=2)
*/
private $shippingCost = 0;
/**
* @var string
*
* @ORMColumn(name="package_cost", type="decimal", precision=10, scale=2)
*/
private $packageCost = 0;
/**
* @var string
*
* @ORMColumn(name="other_fees", type="decimal", precision=10, scale=2)
*/
private $otherFees = 0;
/**
* @var integer
*
* @ORMColumn(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 InboundBundleEntityRequestForEstimate $requestId
*
* @return RequestForEstimate
*/
public function setRequestId(InboundBundleEntityRequestForEstimate $requestId = null)
{
$this->requestId = $requestId;
return $this;
}
/**
* Get requestId
*
* @return InboundBundleEntityRequestForEstimate
*/
public function getRequestId()
{
return $this->requestId;
}
/**
* Set productId
*
* @param InboundBundleEntityProduct $productId
*
* @return RequestForEstimate
*/
public function setProductId(InboundBundleEntityProduct $productId = null)
{
$this->productId = $productId;
return $this;
}
/**
* Get productId
*
* @return InboundBundleEntityProduct
*/
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 InboundBundleEntityRequestStatus $status
*
* @return RequestForEstimate
*/
public function setStatus(InboundBundleEntityRequestStatus $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return InboundBundleEntityRequestStatus
*/
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 InboundBundleEntity;
use DoctrineORMMapping as ORM;
/**
* PurchaseOrder
*
* @ORMTable(name="purchase_order")
* @ORMEntity(repositoryClass="InboundBundleRepositoryPurchaseOrderRepository")
*/
class PurchaseOrder
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMOneToOne(targetEntity="RequestForEstimate", cascade={"persist"})
* @ORMJoinColumn(name="request_estimate_id", referencedColumnName="request_id")
*/
private $requestId;
/**
* @var DateTime
*
* @ORMColumn(name="create_time", type="datetime")
*/
private $createTime;
/**
* @var DateTime
*
* @ORMColumn(name="update_time", type="datetime")
*/
private $updateTime;
/**
* @var int
*
* @ORMColumn(name="status", type="integer")
*/
private $status;
/**
* @var DateTime
*
* @ORMColumn(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 InboundBundleEntityRequestForEstimate $requestForEstimate
*
* @return PurchaseOrder
*/
public function setRequestForEstimate(InboundBundleEntityRequestForEstimate $requestForEstimate = null)
{
$this->requestForEstimate = $requestForEstimate;
return $this;
}
/**
* Get requestForEstimate
*
* @return InboundBundleEntityRequestForEstimate
*/
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 InboundBundleEntityRequestForEstimate
*/
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:
Я приведу вам пример из одного из моих проектов. Весь фокус заключается в установщиках сущностей и конфигурациях каскадеров. Надеюсь, это поможет.
Сущности:
class Agreement
{
// ...
/**
* @ORMOneToOne(targetEntity="AppBundleEntityRequisites", 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
{
// ...
/**
* @ORMOneToOne(targetEntity="AppBundleEntityAgreement", inversedBy="requisites")
* @ORMJoinColumn(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();
Комментарии:
1. Прекрасно! Большое спасибо!