From 6d6c5df6a8051af52dfb7a7efb69e06b2a7e166f Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 6 Dec 2013 13:35:02 +0200 Subject: [PATCH] orm changes --- application/Espo/ORM/DB/Mapper.php | 6 +++--- application/Espo/ORM/Entity.php | 19 ++++++++++++++++--- application/Espo/ORM/EntityFactory.php | 15 +++++++++++---- application/Espo/ORM/EntityManager.php | 20 ++++++++++++++++---- application/Espo/ORM/IEntity.php | 21 +++++++++++++++++++++ application/Espo/ORM/Metadata.php | 22 ++++++++++++++++++++++ tests/Espo/ORM/DB/MapperTest.php | 2 +- 7 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 application/Espo/ORM/Metadata.php diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index d964549598..eda408a6af 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -35,9 +35,9 @@ abstract class Mapper implements IMapper '*' => 'LIKE', '>' => '>', '<' => '<', + '>=' => '>=', + '<=' => '<=', '=' => '=', - 'equals' => '=', - 'like' => 'LIKE', ); // @todo whereClause ? @@ -58,7 +58,7 @@ abstract class Mapper implements IMapper } public function selectById(IEntity $entity, $id) - { + { $selectPart = $this->getSelect($entity); $joinsPart = $this->getBelongsToJoins($entity); $wherePart = $this->getWhere($entity, array('id' => $id, 'deleted' => 0)); diff --git a/application/Espo/ORM/Entity.php b/application/Espo/ORM/Entity.php index 1795210816..e217edc2aa 100644 --- a/application/Espo/ORM/Entity.php +++ b/application/Espo/ORM/Entity.php @@ -16,13 +16,13 @@ abstract class Entity implements IEntity /** * @var array Defenition of fields. - * @todo make protected in PHP 5.4 + * @todo make protected */ public $fields = array(); /** * @var array Defenition of relations. - * @todo make protected in PHP 5.4 + * @todo make protected */ public $relations = array(); @@ -33,11 +33,19 @@ abstract class Entity implements IEntity protected $container = array(); - public function __construct() + public function __construct($defs = array()) { if (empty($this->entityName)) { $this->entityName = end(explode('\\', get_class($this))); } + + if (!empty($defs['fields'])) { + $this->fields = $defs['fields']; + } + + if (!empty($defs['relations'])) { + $this->relations = $defs['relations']; + } } public function clear($name) @@ -75,6 +83,11 @@ abstract class Entity implements IEntity if ($name == 'id') { return $this->id; } + $method = 'get' . ucfirst($name); + if (method_exists($this, $method)) { + return $this->$method(); + } + if (isset($this->container[$name])) { return $this->container[$name]; } diff --git a/application/Espo/ORM/EntityFactory.php b/application/Espo/ORM/EntityFactory.php index 06060f5e88..95e865b20d 100644 --- a/application/Espo/ORM/EntityFactory.php +++ b/application/Espo/ORM/EntityFactory.php @@ -4,11 +4,18 @@ namespace Espo\ORM; class EntityFactory { - public function create($className) + protected $metadata; + + public function __construct(Metadata $metadata) { - $className = $this->normalizeName($name); - - $entity = new $className(); + $this->metadata = $metadata; + + } + public function create($name) + { + $className = $this->normalizeName($name); + $defs = $this->metdata->get($name); + $entity = new $className($defs); return $entity; } diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 83c6d2e588..f1002217c8 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -8,18 +8,25 @@ class EntityManager protected $repositoryFactory; - protected $mapper; + protected $mapper; - protected $repositoryHash = array(); + protected $metadata; + + protected $repositoryHash = array(); public function __construct(\PDO $pdo, $params) - { + { + $this->metadata = new Metadata(); + + if (!empty($params['metadata'])) { + $this->setMetadata($params['metadata']); + } $entityFactoryClassName = '\\Espo\\ORM\\EntityFactory'; if (!empty($params['entityFactoryClassName'])) { $entityFactoryClassName = $params['entityFactoryClassName']; } - $this->entityFactory = new $entityFactoryClassName(); + $this->entityFactory = new $entityFactoryClassName($this->metadata); $mapperClassName = '\\Espo\\ORM\\DB\\MysqlMapper'; if (!empty($params['mapperClassName'])) { @@ -45,6 +52,11 @@ class EntityManager $this->repositoryHash[$name] = $this->repositoryFactory->create($name); } return $this->repositoryHash[$name]; + } + + public function setMetadata(array $data) + { + $this->metadata->setData($data); } } diff --git a/application/Espo/ORM/IEntity.php b/application/Espo/ORM/IEntity.php index 2e092a9f83..0bdb1cacc8 100644 --- a/application/Espo/ORM/IEntity.php +++ b/application/Espo/ORM/IEntity.php @@ -35,6 +35,27 @@ interface IEntity * Resets all fields in the current model. */ function reset(); + + /** + * Set field. + */ + function set($name, $value); + + /** + * Get field. + */ + function get($name); + + /** + * Check field is set. + */ + function has($name); + + /** + * Clear field. + */ + function clear($name); + } diff --git a/application/Espo/ORM/Metadata.php b/application/Espo/ORM/Metadata.php new file mode 100644 index 0000000000..7d554613ab --- /dev/null +++ b/application/Espo/ORM/Metadata.php @@ -0,0 +1,22 @@ +data = $data; + } + + public function get($entityName) + { + return $this->data[$entityName]; + } + +} + + diff --git a/tests/Espo/ORM/DB/MapperTest.php b/tests/Espo/ORM/DB/MapperTest.php index f0aa03089a..4534abcc61 100644 --- a/tests/Espo/ORM/DB/MapperTest.php +++ b/tests/Espo/ORM/DB/MapperTest.php @@ -32,7 +32,7 @@ class DBMapperTest extends PHPUnit_Framework_TestCase return "'" . $args[0] . "'"; })); - $this->entityFactory = $this->getMock('\\Espo\\ORM\\EntityFactory'); + $this->entityFactory = $this->getMockBuilder('\\Espo\\ORM\\EntityFactory')->disableOriginalConstructor()->getMock(); $this->entityFactory->expects($this->any()) ->method('create') ->will($this->returnCallback(function() {