PHP code example of nassau / relation-collection

1. Go to this page and download the library: Download nassau/relation-collection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nassau / relation-collection example snippets




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




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