gpt4 book ai didi

doctrine-orm - 为什么 Doctrine 坚持持久化引用的托管实体?

转载 作者:行者123 更新时间:2023-12-02 03:39:26 25 4
gpt4 key购买 nike

根据 Doctrine 2.0 文档 ( http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html ) a

..preexisting managed entity, is ignored by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities are mapped with cascade=PERSIST or cascade=ALL (see “Transitive Persistence”)."

我的理解是,级联到引用的托管实体应该导致引用的实体被持久化操作忽略(案例 3):

Persistence by Reachability: Cascade Persist

  1. New entities in a collection marked as cascade persist will be directly persisted by Doctrine.
  2. New entities in a collection not marked as cascade persist will produce an Exception and rollback the flush() operation.
  3. Collections without new entities are skipped.

然而,事实并非如此。我有一个托管 Channel 实体,其中包含 1:n 个 PropertiesData 实体。创建 Channel 实体时,会检索但不会修改一些属性。

当我添加数据并通过以下方式持久化实体时

$channel->addData(new Model\Data($channel, $timestamp, $value));
$em->persist();

我可以看到用于编写新的 Data 实体的 SQL,但也可以看到对现有 Properties 的更新(具有未更改的旧值)。

为什么 Doctrine (2.4.1) 会保留被管理实体的关系?

实体定义如下所示:

class Channel extends Entity {
/**
* @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"persist"}, orphanRemoval=true)
* @OrderBy({"timestamp" = "ASC"})
*/
protected $data = NULL;

/**
* Constructor
*/
public function __construct($type) {
parent::__construct($type);
$this->data = new ArrayCollection();
}

/**
* Add a new data to the database
*/
public function addData(\Volkszaehler\Model\Data $data) {
$this->data->add($data);
}

...
}

abstract class Entity {

/**
* @OneToMany(targetEntity="Property", mappedBy="entity", cascade={"remove", "persist"}, orphanRemoval=true)
* @OrderBy({"key" = "ASC"})
*/
protected $properties = NULL;

/**
* Constructor
*
* @param string $type
*/
public function __construct($type) {
if (!Definition\EntityDefinition::exists($type)) {
throw new \Exception('Unknown entity type: \'' . $type . '\'');
}

$this->properties = new Collections\ArrayCollection();
}

...
}

最佳答案

我终于发现 Doctrine 坚持持久化引用的托管实体。

在上述情况下,我已经修改了相关实体(更改了某些属性的类型),由于在计算更改集时它的 === 比较,Doctrine 确实解释为更新。

有一些关于使用 PostFlush 事件来防止这种情况的讨论,但目前还没有定论:https://github.com/doctrine/doctrine2/pull/382#issuecomment-43421295

关于doctrine-orm - 为什么 Doctrine 坚持持久化引用的托管实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21042051/

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