什么是PersistentCollection?

时间:2015-03-22 19:58:48

标签: symfony collections doctrine-orm

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

我已查阅了学说文档以找到:

  

PersistentCollection表示具有的元素集合   持久的状态。

我不明白这意味着什么。

你可以告诉我:

  1. 使用简单的一对多持久收藏是否正常?
  2. 这些收藏品可能会如何出现? (而不是     通常的ArrayCollection)
  3. 最后,究竟是什么用途         持久收藏?

2 个答案:

答案 0 :(得分:3)

1.使用简单的一对多持久收藏是否正常?

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

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

  1. 这些收藏品可能会如何出现? (而不是通常的ArrayCollection)
  2. 它们是集合,它们看起来与普通的ArrayCollection相同,其中包含类型的实体。

    1. 最后,Persistent Collection的用途究竟是什么?
    2. 正如您在文档中看到的那样,PersistentCollection有一些ArrayCollection没有的函数,而PersistentCollection使用EntityManager,它允许与数据库进行交互而不必持久化,只需刷新。

答案 1 :(得分:2)

添加到Serrar的描述。

PersistentCollections仅备注与另一个实体的连接。如果你正确地制作了OneToMany或ManyToOne映射/链接,那么这是真的。

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 - 顶部的描述描述了目的。

相关问题