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

标签 php symfony doctrine-orm

我正在为我的应用程序使用 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/

相关文章:

php - 从提取的 PDF 文本中删除多余的空格

php - symfony2测试带有发布数据的客户请求

php - 如何访问其他用户的数据?

php - Doctrine,如何左连接一个未生成的实体?

php - MySQL重复条目错误,即使它不是重复条目

php - 使用 2 个查询扩展 URI(即 'viewauthorbooks.php?authorid=4' 和 'orderby=returndate")可能吗?

php - 生产环境中的 Doctrine 模式更新错误

Symfony2/Doctrine - postFlush

javascript - 是否可以使用 php 或 javascript 打开一个页面并单击另一个页面上的按钮

symfony - 覆盖 FOSUserBundle 登录表单