2017 © Pedro Peláez
 

library relation-collection

Doctrine Collection automatically sets the owner when adding an item

image

nassau/relation-collection

Doctrine Collection automatically sets the owner when adding an item

  • Friday, July 3, 2015
  • by mlebkowski
  • Repository
  • 1 Watchers
  • 1 Stars
  • 19,953 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Doctrine Relation Collection

This Collection knows about their parent and sets it on an item when adding, .i.e:, (*1)

$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., (*2)

Requirements

You need to either:, (*3)

  • 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., (*4)

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);

The Versions

03/07 2015

dev-master

9999999-dev

Doctrine Collection automatically sets the owner when adding an item

  Sources   Download

MIT

The Requires

 

The Development Requires

03/07 2015

v1.0

1.0.0.0

Doctrine Collection automatically sets the owner when adding an item

  Sources   Download

MIT

The Requires

 

The Development Requires