make data, token and properties cascading persist operations
fixed several bugs in the new property system
This commit is contained in:
parent
863b9d2d72
commit
fd2daa02ff
7 changed files with 90 additions and 50 deletions
|
@ -102,6 +102,11 @@ class Aggregator extends Entity {
|
|||
public function removeChannel(Channel $child) {
|
||||
$this->channels->removeElement($child);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo return array via ArrayCollection::toArray()?
|
||||
*/
|
||||
public function getChannels() { return $this->channels; }
|
||||
}
|
||||
|
||||
?>
|
|
@ -35,7 +35,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||
*/
|
||||
class Channel extends Entity {
|
||||
/**
|
||||
* @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove"})
|
||||
* @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove", "persist"})
|
||||
* @OrderBy({"timestamp" = "ASC"})
|
||||
*/
|
||||
protected $data = NULL;
|
||||
|
|
|
@ -31,6 +31,7 @@ use Volkszaehler\Model;
|
|||
*
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @package default
|
||||
* @todo rename? Bsp: DataSample, Sample, Reading
|
||||
*
|
||||
* @Entity
|
||||
* @Table(name="data")
|
||||
|
|
|
@ -38,7 +38,7 @@ use Volkszaehler\Util;
|
|||
* @DiscriminatorColumn(name="class", type="string")
|
||||
* @DiscriminatorMap({
|
||||
* "channel" = "Channel",
|
||||
* "group" = "Aggregator"
|
||||
* "aggregator" = "Aggregator"
|
||||
* })
|
||||
* @HasLifecycleCallbacks
|
||||
*/
|
||||
|
@ -57,12 +57,12 @@ abstract class Entity {
|
|||
protected $type;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="Token", mappedBy="entity")
|
||||
* @OneToMany(targetEntity="Token", mappedBy="entity", cascade={"remove", "persist"})
|
||||
*/
|
||||
protected $tokens = NULL;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="Property", mappedBy="entity")
|
||||
* @OneToMany(targetEntity="Property", mappedBy="entity", cascade={"remove", "persist"})
|
||||
* @OrderBy({"name" = "ASC"})
|
||||
*/
|
||||
protected $properties = NULL;
|
||||
|
@ -73,7 +73,7 @@ abstract class Entity {
|
|||
* @param string $type
|
||||
* @param array $properties of Model\Property
|
||||
*/
|
||||
public function __construct($type, $properties = NULL) {
|
||||
public function __construct($type) {
|
||||
if (!EntityDefinition::exists($type)) {
|
||||
throw new \Exception('unknown entity type');
|
||||
}
|
||||
|
@ -83,12 +83,6 @@ abstract class Entity {
|
|||
|
||||
$this->tokens = new Collections\ArrayCollection();
|
||||
$this->properties = new Collections\ArrayCollection();
|
||||
|
||||
if (isset($properties)) {
|
||||
foreach($properties as $property) {
|
||||
$this->properies->add($property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,10 +130,10 @@ abstract class Entity {
|
|||
/**
|
||||
* @param string $name of the property
|
||||
* @param string|integer|float $value of the property
|
||||
* @todo to be implemented
|
||||
* @todo check if already set for this entity
|
||||
*/
|
||||
public function setProperty($name, $value) {
|
||||
|
||||
public function setProperty(Property $property) {
|
||||
$this->properties->add($property);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,33 +166,4 @@ abstract class Entity {
|
|||
}
|
||||
}
|
||||
|
||||
class EntityDefinition extends Util\Definition {
|
||||
/** @var string File containing the JSON definitons */
|
||||
const FILE = '/share/entities.json';
|
||||
|
||||
/** @var array list of required properties */
|
||||
protected $required = array();
|
||||
|
||||
/** @var array list of optional properties */
|
||||
protected $optional = array();
|
||||
|
||||
/** @var string classname of intepreter (see backend/lib/Interpreter/) */
|
||||
protected $interpreter;
|
||||
|
||||
/** @var string optional for Aggregator class entities */
|
||||
protected $unit;
|
||||
|
||||
/**
|
||||
* @todo url relative or absolute?
|
||||
* @var string
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
/*
|
||||
* Setter & Getter
|
||||
*/
|
||||
public function getInterpreter() { return $this->interpreter; }
|
||||
public function getUnit() { return $this->unit; }
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
63
backend/lib/Model/EntityDefinition.php
Normal file
63
backend/lib/Model/EntityDefinition.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/**
|
||||
* @package default
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
*/
|
||||
/*
|
||||
* This file is part of volkzaehler.org
|
||||
*
|
||||
* volkzaehler.org is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* volkzaehler.org is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Volkszaehler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @todo extract to extra file
|
||||
*
|
||||
*/
|
||||
class EntityDefinition extends Util\Definition {
|
||||
/** @var string File containing the JSON definitons */
|
||||
const FILE = '/share/entities.json';
|
||||
|
||||
/** @var array list of required properties */
|
||||
protected $required = array();
|
||||
|
||||
/** @var array list of optional properties */
|
||||
protected $optional = array();
|
||||
|
||||
/** @var string classname of intepreter (see backend/lib/Interpreter/) */
|
||||
protected $interpreter;
|
||||
|
||||
/** @var string optional for Aggregator class entities */
|
||||
protected $unit;
|
||||
|
||||
static protected $definitions = NULL;
|
||||
|
||||
/**
|
||||
* @todo url relative or absolute?
|
||||
* @var string
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
/*
|
||||
* Setter & Getter
|
||||
*/
|
||||
public function getInterpreter() { return $this->interpreter; }
|
||||
public function getUnit() { return $this->unit; }
|
||||
}
|
||||
|
||||
?>
|
|
@ -66,9 +66,11 @@ class Property {
|
|||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function __construct($name, $value) {
|
||||
public function __construct(Model\Entity $entity, $name, $value) {
|
||||
$this->setName($name);
|
||||
$this->setValue($value);
|
||||
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,12 +92,14 @@ class Property {
|
|||
*/
|
||||
public function getName() { return $this->name; }
|
||||
public function getValue() { return $this->value; }
|
||||
public function getDefinition() { return PropertyDefinition::get($name); }
|
||||
public function getDefinition() { return PropertyDefinition::get($this->name); }
|
||||
|
||||
public function setValue($value) {
|
||||
if (!$this->definition->validate($value)) {
|
||||
throw new \Exception('invalid property value'); $this->value = $value;
|
||||
if (!$this->getDefinition()->validateValue($value)) {
|
||||
throw new \Exception('invalid property value');
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,6 +56,8 @@ class PropertyDefinition extends Util\Definition {
|
|||
*/
|
||||
protected $choices = array();
|
||||
|
||||
protected static $definitions = NULL;
|
||||
|
||||
|
||||
/**
|
||||
* File containing the JSON definitons
|
||||
|
@ -80,11 +82,11 @@ class PropertyDefinition extends Util\Definition {
|
|||
break;
|
||||
|
||||
case 'integer':
|
||||
$invalid = !is_int($value);
|
||||
$invalid = !is_int($value); // TODO check for numeric string
|
||||
break;
|
||||
|
||||
case 'float':
|
||||
$invalid = !is_float($value);
|
||||
$invalid = !is_float($value); // TODO check for numeric string
|
||||
break;
|
||||
|
||||
case 'multiple':
|
||||
|
@ -95,7 +97,7 @@ class PropertyDefinition extends Util\Definition {
|
|||
throw new \Exception('unknown property type');
|
||||
}
|
||||
|
||||
if ($type == 'integer' || $type == 'float') {
|
||||
if ($this->type == 'integer' || $this->type == 'float') {
|
||||
$invalid |= isset($this->min) && $value < $this->min;
|
||||
$invalid |= isset($this->max) && $value > $this->max;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue