gpt4 book ai didi

php - 级联 Doctrine2 和 Symfony2 的持续问题

转载 作者:行者123 更新时间:2023-12-04 21:01:40 24 4
gpt4 key购买 nike

我正在为我的应用程序使用 Symfony2,并且我正在使用两个实体管理器;一个用于读取,另一个用于写入。

我正在创建这样的实体管理器对象:

$em = $this->getDoctrine()->getEntityManager('write');
$em = $this->getDoctrine()->getEntityManager('read');

最初它工作正常,但现在出现以下错误:

A new entity was found through the relationship 'AppBundle\Entity\ProfileViewer#viewer' that was not configured to cascade persist operations for entity: shamsi. Explicitly persist the new entity or configure cascading persist operations on the relationship.

这是我的 ProfileViewer 实体:

/**
* AppBundle\Entity\ProfileViewer
*
* @ORM\Table(name="profile_viewer")
* @ORM\Entity
*/
class ProfileViewer
{
/**
* @var bigint $id
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var bigint $userId
*
* @ORM\Column(name="user_id", type="bigint", nullable=false)
*/
private $userId;

/**
* @var datetime $viewedAt
*
* @ORM\Column(name="viewed_at", type="datetime", nullable=true)
*/
private $viewedAt;

/**
* @ORM\ManyToOne(targetEntity="user", inversedBy="viewers")
* @ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/
private $viewer;

public function __construct()
{
$this->viewedAt = new \DateTime();
}
/**
* Get id
*
* @return bigint
*/
public function getId()
{
return $this->id;
}

/**
* Set userId
*
* @param bigint $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}

/**
* Get UserId
*
* @return bigint
*/
public function getUserId()
{
return $this->userId;
}

/**
* Set viewedAt
*
* @param datetime $viewedAt
*/
public function setViewedAt($viewedAt)
{
$this->viewedAt = $viewedAt;
}

/**
* Get viewedAt
*
* @return datetime
*/
public function getViewedAt()
{
return $this->viewedAt;
}

/**
* Set viewer
*
* @param AppBundle\Entity\User $viewer
*/
public function setViewer(AppBundle\Entity\User $viewer)
{
$this->viewer = $viewer;
}

/**
* Get viewer
*
* @return AppBundle\Entity\User
*/
public function getViewer()
{
return $this->viewer;
}

}

当我创建了两个实体管理器时出现此错误。

最佳答案

默认情况下,在 Doctrine2 中没有级联操作

您可以将 cascade={"persist"} 添加到您的关联中:

 /**
* @ORM\ManyToOne(targetEntity="user", inversedBy="viewers", cascade={"persist"})
* @ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/

您可以阅读 this了解学说中关联的级联操作。强调这一点很重要:

Cascade operations are performed in memory. That means collections and related entities are fetched into memory, even if they are still marked as lazy when the cascade operation is about to be performed. However this approach allows entity lifecycle events to be performed for each of these operations.

However, pulling objects graph into memory on cascade can cause considerable performance overhead, especially when cascading collections are large. Makes sure to weigh the benefits and downsides of each cascade operation that you define.

关于php - 级联 Doctrine2 和 Symfony2 的持续问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12180864/

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