简体   繁体   English

Doctrine2 和 Symfony2 的级联问题

[英]Cascade persisting issues with Doctrine2 and Symfony2

I am using Symfony2 for my application and I am using two Entity Managers;我正在为我的应用程序使用 Symfony2,并且我正在使用两个实体管理器; one is for read and other is for write.一个用于读取,另一个用于写入。

I am creating entity manager object like that:我正在创建这样的实体管理器对象:

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

Initially it was working fine but now the following error is coming:最初它工作正常,但现在出现以下错误:

A new entity was found through the relationship ' AppBundle\\Entity\\ProfileViewer#viewer ' that was not configured to cascade persist operations for entity: shamsi .通过关系 ' AppBundle\\Entity\\ProfileViewer#viewer ' 发现了一个新实体,该AppBundle\\Entity\\ProfileViewer#viewer未配置为对实体进行级联持久化操作: shamsi Explicitly persist the new entity or configure cascading persist operations on the relationship.显式持久化新实体或在关系上配置级联持久化操作。

Here is my ProfileViewer Entity:这是我的 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;
}

}

This error comes when I have created two entity managers.当我创建了两个实体管理器时会出现此错误。

By default, no operations are cascaded in Doctrine2.默认情况下,Doctrine2 中没有级联操作

You can add cascade={"persist"} to your association:您可以将cascade={"persist"}到您的关联中:

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

You can read this to understand cascade operations on associations in doctrine.您可以阅读本文以了解学说中关联的级联操作。 This is important to underline:强调这一点很重要:

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.确保权衡您定义的每个级联操作的利弊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM