Last active
December 18, 2015 01:39
-
-
Save dominikzogg/5705377 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Bundle\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Table(name="one") | |
* @ORM\Entity | |
*/ | |
class One | |
{ | |
/** | |
* @var Many[]|Collection | |
* @ORM\OneToMany(targetEntity="Many", mappedBy="one", cascade={"persist"}) | |
*/ | |
protected $manies; | |
public function __construct() | |
{ | |
$this->manies = new ArrayCollection(); | |
} | |
/** | |
* @param Many $many | |
* @param bool $stopPropagation | |
* @return $this | |
*/ | |
public function addMany(Many $many, $stopPropagation = false) | |
{ | |
$this->manies->add($many); | |
if(!$stopPropagation) { | |
$many->setOne($this, true); | |
} | |
return $this; | |
} | |
/** | |
* @param Many $many | |
* @param bool $stopPropagation | |
* @return $this | |
*/ | |
public function removeMany(Many $many, $stopPropagation = false) | |
{ | |
$this->manies->removeElement($many); | |
if(!$stopPropagation) { | |
$many->setOne(null, true); | |
} | |
return $this; | |
} | |
/** | |
* @param Many[] $manies | |
* @return $this | |
*/ | |
public function setManies($manies) | |
{ | |
foreach($this->manies as $many) { | |
$this->removeMany($many); | |
} | |
foreach($manies as $many) { | |
$this->addMany($many); | |
} | |
return $this; | |
} | |
/** | |
* @return Many[]|Collection | |
*/ | |
public function getManies() | |
{ | |
return $this->manies; | |
} | |
} | |
/** | |
* @ORM\Table(name="many") | |
* @ORM\Entity | |
*/ | |
class Many | |
{ | |
/** | |
* @var One | |
* @ORM\ManyToOne(targetEntity="One", inversedBy="manies") | |
* @ORM\JoinColumn(name="one_id", referencedColumnName="id") | |
*/ | |
protected $one; | |
/** | |
* @param One $one | |
* @param bool $stopPropagation | |
* @return $this | |
*/ | |
public function setOne(One $one = null, $stopPropagation = false) | |
{ | |
if(!$stopPropagation) { | |
if(!is_null($this->one)) { | |
$this->one->removeMany($this, true); | |
} | |
if(!is_null($one)) { | |
$one->addMany($this, true); | |
} | |
} | |
$this->one = $one; | |
return $this; | |
} | |
/** | |
* @return One | |
*/ | |
public function getOne() | |
{ | |
return $this->one; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ElectricMaxxx
Its in a very early state, and i will be in 2.0 release, cause BC break:
saxulum-legacy/saxulum-accessor@43a050e