nassau / relation-collection
Doctrine Collection automatically sets the owner when adding an item
Installs: 25 416
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- doctrine/orm: ^2.5
Requires (Dev)
- phpunit/phpunit: ^4.7
This package is auto-updated.
Last update: 2024-11-23 19:26:39 UTC
README
This Collection knows about their parent and sets it on an item when adding, .i.e:
$collection = $foo->getBars();
$collection->add($bar); // behind the scenes: $bar->setFoo($foo);
This way you can manage this relation via Symfony Forms without the ugly by_reference
and addBar()/removeBar()
methods.
Requirements
You need to either:
- wrap an initialized
PersistentCollection
withPeristentAssociationRelationCollection
— works out of the box - use
RelationCollection
and tell it how to set the owner of an object using a closure. the former does this automatically basing on mapping information retrieved fromPersistentCollection
Why?
Because I don’t want to write adders/removers boilerplate for every relation. And also because of reasons.
Example usage
With existing doctrine relations
<?php use Nassau\RelationCollection\PersistentAssociationRelationCollection; /** * @ORM\Entity **/ class Foo { /** * @var Bar[] * @ORM\OneToMany(targetEntity="Bar", mappedBy="foo", cascade={"all"}, orphanRemoval=true) **/ private $bars; public function getBars() { return new PersistentAssociationRelationCollection($this->bars); } public function setBars($bars) { // noop, handled by reference, $this->getBars()->add($bar); } }
Manually
<?php use Nassau\RelationCollection\RelationCollection; class Foo { private $bars; public function __construct() { $this->bars = new RelationCollection(function (Bar $bar) { $bar->setFoo($this); }); } public function getBars() { return $this->bars; } } class Bar { /** @var Foo */ private $foo; public function setFoo(Foo $foo) { $this->foo = $foo; } } $foo = new Foo; $foo->getBars()->add(new Bar);