gpt4 book ai didi

symfony - entityManager 保存和刷新

转载 作者:行者123 更新时间:2023-12-04 21:48:50 27 4
gpt4 key购买 nike

我正在尝试以级联方式保存某个对象并检索它。
我有 3 个对象超过 3 个实体。

实体:

class Order
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var object $basket
*
* @ORM\OneToOne(targetEntity="Entity\Basket", inversedBy="order")
*/
protected $basket;
...
}

class Basket
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var array $declinations
*
* @ORM\OneToMany(targetEntity="Entity\BasketDeclination", mappedBy="basket")
*/
protected $declinations;

/**
* Order owner (reversed side)
*
* @var OrderClient $order
*
* @ORM\OneToOne(targetEntity="Entity\Order", mappedBy="basket")
*/
protected $order;
...
}

class BasketDeclination
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var integer $basket
*
* @ORM\ManyToOne(targetEntity="Entity\Basket", inversedBy="declinations")
*/
protected $basket;
...
}

对象覆盖实体:
class OrderObject
{
function __construct(
EntityManager $em,
Order $entity = null,
BasketObject $basket = null
)
{
$this->em = $em;

if (!$entity) {
$this->entity = new Order();

$this->basket = $basket;
} else {
$this->setDataFromEntity($entity);
}
}

protected function setDataFromEntity(Order $entity)
{
$basketFactory = new BasketFactory($this->em);

$this->entity = $entity;

$this->basket = $basketFactory->getBasket($entity->getBasket()->getId());
}

public function save($flush = false)
{
// save subObject
$this->basket->save();

// set link
$this->entity->setBasket($this->basket->getEntity());

$this->em->persist($this->entity);

if ($flush) {
$this->em->flush();
}
}

public function refresh()
{
$this->em->refresh($this->entity);
$this->setDataFromEntity($this->entity);
}
...
}

class BasketObject
{
function __construct(EntityManager $em, Basket $entity = null)
{
$this->em = $em;

if (!$entity) {
$this->entity = new Basket();
$this->declinations = array();
} else {
$this->setDataFromEntity($entity);
}
}

protected function setDataFromEntity(Basket $entity)
{
$this->entity = $entity;

$this->declinations = array();
foreach ($entity->getDeclinations() as $declination) {
$this->declinations[] = new BasketDeclinationObject($this->em, $declination);
}
}

public function save($flush = false)
{
foreach ($this->declinations as $declination) {
$declination->save();
}
$this->em->persist($this->entity);
if ($flush) {
$this->em->flush();
}
}
...
}

class BasketDeclinationObject
{
public function __construct(
EntityManager $em,
BasketDeclination $entity= null,
BasketObject $basket = null)
{
$this->em = $em;

if (!$entity) {
$this->entity = new BasketDeclination();

$this->basket = $basket;
} else {
$this->setDataFromEntity($entity);
}
}

protected function setDataFromEntity(BasketDeclination $entity)
{
$this->entity = $entity;

$declinationFactory = new DeclinationFactory($this->em);
$this->declination = $declinationFactory->getDeclination($entity->getDeclination()->getId());
}

public function save($flush = false)
{
if ($this->quantity <= 0) {
$this->em->remove($this->entity);
$this->remove = true;
return ;
}
if (!$this->entity->getId()) {
$this->entity->setBasket($this->basket->getEntity());
}
$this->entity->setQuantity($this->quantity);
$this->em->persist($this->entity);
if ($flush) {
$this->em->flush();
}
}
...
}

问题是,在我的测试中,当我尝试为一个篮子添加 BasketDeclination 然后保存
Basket 已保存,BasketDeclination 也已保存。
然后当我 $basket->refresh() 篮子刷新并且 BasketDeclinaiton 从实体重建

但是当我有一个带有篮子的订单并且我添加了 BasketDeclinaiton ($order->basket->addDeclination(...))
当我保存所有实体时
然后当我刷新订单时,我会取回订单和购物篮。
但是实体 $basket->getDeclinations() 没有任何东西

我做错了什么?

最佳答案

如果问题确实是实体管理器不是refresh ing 关联(如 Mohammad AbuShady 所建议的),告诉您的实体级联 refresh 的答案手术。

class Basket
{
// ...

/**
* @var array $declinations
*
* @ORM\OneToMany(
* targetEntity="Entity\BasketDeclination",
* mappedBy="basket",
* cascade={"refresh"} // THIS LINE ADDED
* )
*/
protected $declinations;

// ...
...
}

关于symfony - entityManager 保存和刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9360209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com