. */ namespace Volkszaehler\Model; use Doctrine\ORM\Mapping; use Volkszaehler\Interpreter; use Doctrine\ORM; use Doctrine\Common\Collections; use Doctrine\Common\Collections\ArrayCollection; /** * Aggregator entity * * @author Steffen Vogel * @package default * @todo use nested sets: http://github.com/blt04/doctrine2-nestedset * * @Entity */ class Aggregator extends Entity { /** * @ManyToMany(targetEntity="Entity", inversedBy="parents") * @JoinTable(name="entities_in_aggregator", * joinColumns={@JoinColumn(name="parent_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="child_id", referencedColumnName="id")} * ) */ protected $children = NULL; /** * Construct */ public function __construct($properties = array()) { parent::__construct($properties); $this->channels = new ArrayCollection(); $this->children = new ArrayCollection(); } /** * Adds entity as new child * * @param Entity $child * @todo check if the entity is already member of the group * @todo add bidrectional association */ public function addChild(Entity $child) { if ($this->children->contains($child)) { throw new \Exception('Entity is already a child of the group'); } if ($child instanceof Aggregator && $child->contains($this)) { throw new \Exception('Recursion detected! Can\'t group to itself'); } $this->children->add($child); } /** * Checks if aggregator contains given entity * * @param Entity $entity * @param boolean $recursive should we search recursivly? */ protected function contains(Entity $entity, $recursive = FALSE) { if ($this->children->contains($entity)) { return TRUE; } if ($recursive) { foreach ($this->children as $child) { if ($child->contains($entity, $recursive)) { return TRUE; } } } return FALSE; } /** * Remove child from group * * @param Entity $child * @todo check if the entity is member of the group * @todo add bidrectional association */ public function removeChild(Entity $child) { if (!$this->children->removeElement($child)) { throw new \Exception('This entity is not a child of this group'); } } /* * Setter & getter */ public function getChildren() { return $this->children->toArray(); } } ?>