什么是 PersistentCollection?

What is a PersistentCollection?

我的数据库中有一对多关系。 编辑、删除和添加 元素曾经工作得很好。但是,经过一些更改(我无法全部追溯),它不再起作用了。
现在,当我获取具有一对多关系的对象时,我得到一个持久集合来表示我的多方集合。我想以前不是这样的。在我的构造函数中,我创建了一个新的 Array Collection 而不是 Persistent Collection。

我查阅了学说文档发现:

A PersistentCollection represents a collection of elements that have persistent state.

我不明白那是什么意思。

你能告诉我吗:

  1. 简单的一对多持久化集合正常吗?
  2. 这些合集会如何显示? (而不是 通常的 ArrayCollection)
  3. 最后,a到底有什么用 永久集合?

1.Is 具有简单一对多的持久集合正常吗?

不,ArrayCollection 是正常的,我以前从未使用过 PersistentCollection,但它有一些有用的功能,在某些情况下可能会用到。

http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.PersistentCollection.html

  1. 这些合集会如何显示? (而不是通常的 ArrayCollection)

它们是集合,而且它们看起来与普通的 ArrayCollection 相同,其中包含实体及其类型。

  1. 最后,持久化集合到底有什么用?

正如您在文档中看到的那样,PersistentCollection 具有 ArrayCollection 所没有的大量功能,并且 PersistentCollection 使用 EntityManager,它允许与数据库进行交互而无需持久化,只需刷新。

添加到 Serrar 的描述中。

仅限 PersistentCollections 注意 与另一个实体的连接。如果你正确地制作了 OneToMany 或 ManyToOne mapping/link.

use Doctrine\Common\Collections\ArrayCollection;
public function __construct()
{
    $this->comments = new ArrayCollection();
}
// @ORM\OneToMany(targetEntity="Comments", mappedBy="pages")
private $comments;
public function getComments()
{
    return $this->comments;
}
public function setComments(Comments $comments)
{
    $this->comments = $comments;
}

他们不给你数组数据。

在上面的示例中,使用 PageEntity 的 getComments 方法提取与当前页面相关的评论。假设您有页面对象。 在构造中设置新的 ArrayCollection 对象很重要: Relationship Mapping Metadatalink

Doctrine: Class PersistentCollection - 顶部的描述说明了用途。