nassau/relation-collection
最新稳定版本:v1.0
Composer 安装命令:
composer require nassau/relation-collection
包简介
Doctrine Collection automatically sets the owner when adding an item
README 文档
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
PersistentCollectionwithPeristentAssociationRelationCollection— works out of the box - use
RelationCollectionand 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);
统计信息
- 总下载量: 25.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-03