diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index 1981bdd..17f8de6 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -55,20 +55,13 @@ class AggregatorController extends EntityController { if ($uuid = $this->view->request->getParameter('uuid')) { $ec = new EntityController($this->view, $this->em); - $entity = $ec->get($uuid); - - if ($entity instanceof Model\Channel) { - $aggregator->addChannel($entity); - } - elseif ($entity instanceof Model\Aggregator) { - $aggregator->addAggregator($entity); - } + $aggregator->addChild($ec->get($uuid)); } - else { // create new aggregator + else { throw new \Exception('You have to specifiy a uuid to add'); } } - else { + else { // create new aggregator $aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types foreach ($this->view->request->getParameters() as $parameter => $value) { @@ -94,22 +87,15 @@ class AggregatorController extends EntityController { if ($uuid) { $ec = new EntityController($this->view, $this->em); - $entity = $ec->get($uuid); - - if ($entity instanceof Model\Channel) { - $aggregator->removeChannel($entity); - } - elseif ($entity instanceof Model\Aggregator) { - $aggregator->removeAggregator($entity); - } + $aggregator->removeChild($ec->get($uuid)); $this->em->flush(); } - else { // remove aggregator + else { throw new \Exception('You have to specifiy a uuid to remove'); } } - else { + else { // remove aggregator parent::delete($identifier); } diff --git a/backend/lib/Model/Aggregator.php b/backend/lib/Model/Aggregator.php index 63d58c9..b04a9a9 100644 --- a/backend/lib/Model/Aggregator.php +++ b/backend/lib/Model/Aggregator.php @@ -39,28 +39,14 @@ use Doctrine\Common\Collections\ArrayCollection; */ class Aggregator extends Entity { /** - * @ManyToMany(targetEntity="Channel", inversedBy="groups") - * @JoinTable(name="groups_channel", - * joinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}, - * inverseJoinColumns={@JoinColumn(name="channel_id", referencedColumnName="id")} - * ) - */ - protected $channels = NULL; - - /** - * @ManyToMany(targetEntity="Aggregator", inversedBy="parents") - * @JoinTable(name="groups_groups", + * @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; - /** - * @ManyToMany(targetEntity="Aggregator", mappedBy="children") - */ - protected $parents = NULL; - /** * Construct */ @@ -69,44 +55,33 @@ class Aggregator extends Entity { $this->channels = new ArrayCollection(); $this->children = new ArrayCollection(); - $this->parents = new ArrayCollection(); } /** - * Adds group as new child + * Adds entity as new child * - * @param Aggregator $child - * @todo check against endless recursion - * @todo check if the group is already member of the group - * @todo add bidirectional association + * @param Entity $child + * @todo check if the entity is already member of the group + * @todo add bidrectional association */ - public function addAggregator(Aggregator $child) { + public function addChild(Entity $child) { $this->children->add($child); } - public function removeAggregator(Aggregator $child) { - return $this->children->removeElement($child); - } - /** - * Adds channel as new child + * Remove child from group * - * @param Channel $child - * @todo check if the channel is already member of the group + * @param Entity $child + * @todo check if the entity is member of the group * @todo add bidrectional association */ - public function addChannel(Channel $child) { - $this->channels->add($child); - } - - public function removeChannel(Channel $child) { - return $this->channels->removeElement($child); + public function removeChild(Entity $child) { + return $this->children->removeElement($child); } /* * Setter & getter */ - public function getChannels() { return $this->channels->toArray(); } public function getChildren() { return $this->children->toArray(); } } diff --git a/backend/lib/Model/Entity.php b/backend/lib/Model/Entity.php index da209bd..a50e889 100644 --- a/backend/lib/Model/Entity.php +++ b/backend/lib/Model/Entity.php @@ -69,6 +69,11 @@ abstract class Entity { */ protected $properties = NULL; + /** + * @ManyToMany(targetEntity="Aggregator", mappedBy="children") + */ + protected $parents = NULL; + /** * Constructor * @@ -84,6 +89,7 @@ abstract class Entity { $this->tokens = new Collections\ArrayCollection(); $this->properties = new Collections\ArrayCollection(); + $this->parents = new Collections\ArrayCollection(); } diff --git a/backend/lib/Router.php b/backend/lib/Router.php index cfdb731..f6043ad 100644 --- a/backend/lib/Router.php +++ b/backend/lib/Router.php @@ -76,7 +76,8 @@ class Router { 'group' => 'Volkszaehler\Controller\AggregatorController', 'token' => 'Volkszaehler\Controller\TokenController', 'capabilities' => 'Volkszaehler\Controller\CapabilitiesController', - 'data' => 'Volkszaehler\Controller\DataController' + 'data' => 'Volkszaehler\Controller\DataController', + 'entity' => 'Volkszaehler\Controller\EntityController' ); /** diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 241cd19..629392b 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -172,12 +172,14 @@ class JSON extends View { protected static function convertAggregator(Model\Aggregator $aggregator) { $jsonAggregator = self::convertEntity($aggregator); - foreach ($aggregator->getChannels() as $channel) { - $jsonAggregator['channels'][] = self::convertEntity($channel); - } + foreach ($aggregator->getChildren() as $entity) { - foreach ($aggregator->getChildren() as $subAggregator) { - $jsonAggregator['groups'][] = self::convertAggregator($subAggregator); // recursion + if ($entity instanceof Model\Channel) { + $jsonAggregator['channels'][] = self::convertEntity($entity); + } + elseif ($entity instanceof Model\Aggregator) { + $jsonAggregator['groups'][] = self::convertAggregator($entity); + } } return $jsonAggregator; diff --git a/share/sql/mysql.sql b/share/sql/mysql.sql index e06bba4..7d167c3 100644 --- a/share/sql/mysql.sql +++ b/share/sql/mysql.sql @@ -1,13 +1,12 @@ -CREATE TABLE entities (id SMALLINT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, type VARCHAR(255) NOT NULL, class VARCHAR(255) NOT NULL, UNIQUE INDEX entities_uuid_uniq (uuid), PRIMARY KEY(id)) ENGINE = InnoDB; -CREATE TABLE groups_channel (group_id SMALLINT NOT NULL, channel_id SMALLINT NOT NULL, PRIMARY KEY(group_id, channel_id)) ENGINE = InnoDB; -CREATE TABLE groups_groups (parent_id SMALLINT NOT NULL, child_id SMALLINT NOT NULL, PRIMARY KEY(parent_id, child_id)) ENGINE = InnoDB; -CREATE TABLE data (id SMALLINT AUTO_INCREMENT NOT NULL, channel_id SMALLINT DEFAULT NULL, timestamp BIGINT NOT NULL, value NUMERIC(5, 2) NOT NULL, UNIQUE INDEX unique_timestamp (timestamp, channel_id), PRIMARY KEY(id)) ENGINE = InnoDB; -CREATE TABLE tokens (id SMALLINT AUTO_INCREMENT NOT NULL, entity_id SMALLINT DEFAULT NULL, token VARCHAR(255) NOT NULL, valid BIGINT NOT NULL, UNIQUE INDEX tokens_token_uniq (token), PRIMARY KEY(id)) ENGINE = InnoDB; -CREATE TABLE properties (id SMALLINT AUTO_INCREMENT NOT NULL, entity_id SMALLINT DEFAULT NULL, `key` VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, UNIQUE INDEX unique_keys (entity_id, `key`), PRIMARY KEY(id)) ENGINE = InnoDB; -ALTER TABLE groups_channel ADD FOREIGN KEY (group_id) REFERENCES entities(id); -ALTER TABLE groups_channel ADD FOREIGN KEY (channel_id) REFERENCES entities(id); -ALTER TABLE groups_groups ADD FOREIGN KEY (parent_id) REFERENCES entities(id); -ALTER TABLE groups_groups ADD FOREIGN KEY (child_id) REFERENCES entities(id); -ALTER TABLE data ADD FOREIGN KEY (channel_id) REFERENCES entities(id); -ALTER TABLE tokens ADD FOREIGN KEY (entity_id) REFERENCES entities(id); -ALTER TABLE properties ADD FOREIGN KEY (entity_id) REFERENCES entities(id) +Creating database schema... + + + + [PDOException] + SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'entities' already exists + + + +orm:schema-tool:create [--dump-sql] [-h|--help] [-q|--quiet] [-v|--verbose] [-V|--version] [-a|--ansi] [-n|--no-interaction] command + +