定制 nassau/relation-collection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 PersistentCollection with PeristentAssociationRelationCollection — 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 from PersistentCollection

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-03