class PersistentCollection extends AbstractLazyCollection implements Selectable (View source)

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

Collections of entities represent only the associations (links) to those entities. That means, if the collection is part of a many-many mapping and you remove entities from the collection, only the links in the relation table are removed (on flush). Similarly, if you remove entities from a collection that is part of a one-many mapping this will only result in the nulling out of the foreign keys on flush.

Methods

__construct(EntityManagerInterface $em, ClassMetadata $class, Collection $collection)

Creates a new persistent collection.

setOwner($entity, ToManyAssociationMetadata $association)

INTERNAL: Sets the collection's owning entity together with the AssociationMapping that describes the association between the owner and the elements of the collection.

object
getOwner()

INTERNAL: Gets the collection owner.

getTypeClass()

No description

hydrateAdd(mixed $element)

INTERNAL: Adds an element to a collection during hydration. This will automatically complete bidirectional associations in the case of a one-to-many association.

hydrateSet(mixed $key, mixed $element)

INTERNAL: Sets a keyed element in the collection during hydration.

initialize()

Initializes the collection by loading its contents from the database if the collection is not yet initialized.

takeSnapshot()

INTERNAL: Tells this collection to take a snapshot of its current state.

object[]
getSnapshot()

INTERNAL: Returns the last snapshot of the elements in the collection.

object[]
getDeleteDiff()

INTERNAL: getDeleteDiff

object[]
getInsertDiff()

INTERNAL: getInsertDiff

getMapping()

INTERNAL: Gets the association mapping of the collection.

bool
isDirty()

Gets a boolean flag indicating whether this collection is dirty which means its state needs to be synchronized with the database.

setDirty(bool $dirty)

Sets a boolean flag, indicating whether this collection is dirty.

setInitialized(bool $bool)

Sets the initialized flag of the collection, forcing it into that state.

remove($key)

{@inheritdoc}

removeElement($element)

{@inheritdoc}

containsKey($key)

{@inheritdoc}

contains($element)

{@inheritdoc}

get($key)

{@inheritdoc}

count()

{@inheritdoc}

set($key, $value)

{@inheritdoc}

add($value)

{@inheritdoc}

offsetExists($offset)

{@inheritdoc}

offsetGet($offset)

{@inheritdoc}

offsetSet($offset, $value)

{@inheritdoc}

offsetUnset($offset)

{@inheritdoc}

isEmpty()

{@inheritdoc}

clear()

{@inheritdoc}

string[]
__sleep()

Called by PHP when this collection is serialized. Ensures that only the elements are properly serialized.

object[]
slice(int $offset, int|null $length = null)

Extracts a slice of $length elements starting at position $offset from the Collection.

__clone()

Cleans up internal state of cloned persistent collection.

Collection|object[]
matching(Criteria $criteria)

Selects all elements from a selectable that match the expression and return a new collection containing these elements.

Collection|object[]
unwrap()

Retrieves the wrapped Collection instance.

doInitialize()

{@inheritdoc}

Details

__construct(EntityManagerInterface $em, ClassMetadata $class, Collection $collection)

Creates a new persistent collection.

Parameters

EntityManagerInterface $em The EntityManager the collection will be associated with.
ClassMetadata $class The class descriptor of the entity type of this collection.
Collection $collection The collection elements.

setOwner($entity, ToManyAssociationMetadata $association)

INTERNAL: Sets the collection's owning entity together with the AssociationMapping that describes the association between the owner and the elements of the collection.

Parameters

$entity
ToManyAssociationMetadata $association

object getOwner()

INTERNAL: Gets the collection owner.

Return Value

object

ClassMetadata getTypeClass()

Return Value

ClassMetadata

hydrateAdd(mixed $element)

INTERNAL: Adds an element to a collection during hydration. This will automatically complete bidirectional associations in the case of a one-to-many association.

Parameters

mixed $element The element to add.

hydrateSet(mixed $key, mixed $element)

INTERNAL: Sets a keyed element in the collection during hydration.

Parameters

mixed $key The key to set.
mixed $element The element to set.

initialize()

Initializes the collection by loading its contents from the database if the collection is not yet initialized.

takeSnapshot()

INTERNAL: Tells this collection to take a snapshot of its current state.

object[] getSnapshot()

INTERNAL: Returns the last snapshot of the elements in the collection.

Return Value

object[] The last snapshot of the elements.

object[] getDeleteDiff()

INTERNAL: getDeleteDiff

Return Value

object[]

object[] getInsertDiff()

INTERNAL: getInsertDiff

Return Value

object[]

AssociationMetadata getMapping()

INTERNAL: Gets the association mapping of the collection.

Return Value

AssociationMetadata

bool isDirty()

Gets a boolean flag indicating whether this collection is dirty which means its state needs to be synchronized with the database.

Return Value

bool TRUE if the collection is dirty, FALSE otherwise.

setDirty(bool $dirty)

Sets a boolean flag, indicating whether this collection is dirty.

Parameters

bool $dirty Whether the collection should be marked dirty or not.

setInitialized(bool $bool)

Sets the initialized flag of the collection, forcing it into that state.

Parameters

bool $bool

remove($key)

{@inheritdoc}

Parameters

$key

removeElement($element)

{@inheritdoc}

Parameters

$element

containsKey($key)

{@inheritdoc}

Parameters

$key

contains($element)

{@inheritdoc}

Parameters

$element

get($key)

{@inheritdoc}

Parameters

$key

count()

{@inheritdoc}

set($key, $value)

{@inheritdoc}

Parameters

$key
$value

add($value)

{@inheritdoc}

Parameters

$value

offsetExists($offset)

{@inheritdoc}

Parameters

$offset

offsetGet($offset)

{@inheritdoc}

Parameters

$offset

offsetSet($offset, $value)

{@inheritdoc}

Parameters

$offset
$value

offsetUnset($offset)

{@inheritdoc}

Parameters

$offset

isEmpty()

{@inheritdoc}

clear()

{@inheritdoc}

string[] __sleep()

Called by PHP when this collection is serialized. Ensures that only the elements are properly serialized.

{@internal Tried to implement Serializable first but that did not work well with circular references. This solution seems simpler and works well. }}

Return Value

string[]

object[] slice(int $offset, int|null $length = null)

Extracts a slice of $length elements starting at position $offset from the Collection.

If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Parameters

int $offset
int|null $length

Return Value

object[]

__clone()

Cleans up internal state of cloned persistent collection.

The following problems have to be prevented: 1. Added entities are added to old PC 2. New collection is not dirty, if reused on other entity nothing changes. 3. Snapshot leads to invalid diffs being generated. 4. Lazy loading grabs entities from old owner object. 5. New collection is connected to old owner and leads to duplicate keys.

Collection|object[] matching(Criteria $criteria)

Selects all elements from a selectable that match the expression and return a new collection containing these elements.

Parameters

Criteria $criteria

Return Value

Collection|object[]

Exceptions

RuntimeException

Collection|object[] unwrap()

Retrieves the wrapped Collection instance.

Return Value

Collection|object[]

protected doInitialize()

{@inheritdoc}