From 3f64035e87146b8dfdda5c96ecd05c31b6cd0cf3 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 6 Dec 2013 13:52:52 +0200 Subject: [PATCH] orm changes --- .../Espo/Core/Loaders/EntityManager.php | 23 ++++++++----------- application/Espo/ORM/Entity.php | 8 ++++--- application/Espo/ORM/EntityFactory.php | 2 +- application/Espo/ORM/EntityManager.php | 23 +++++++++++++++---- application/Espo/ORM/Repository.php | 3 ++- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/application/Espo/Core/Loaders/EntityManager.php b/application/Espo/Core/Loaders/EntityManager.php index 7e84d38faf..4c387e4d40 100644 --- a/application/Espo/Core/Loaders/EntityManager.php +++ b/application/Espo/Core/Loaders/EntityManager.php @@ -22,22 +22,17 @@ class EntityManager public function load() { - $devMode= !$this->getContainer()->get('config')->get('useCache'); - $doctrineConfig = Setup::createConfiguration($devMode, null, null); - $doctrineConfig->setMetadataDriverImpl(new EspoPHPDriver( - array( - $this->getContainer()->get('config')->get('metadataConfig')->doctrineCache, - $this->getContainer()->get('config')->get('defaultsPath').'/doctrine/metadata' - ) - )); - - $doctrineConn = (array) $this->getContainer()->get('config')->get('database'); + $config = $this->getContainer()->get('config'); - $entityManger = \Doctrine\ORM\EntityManager::create($doctrineConn, $doctrineConfig); + $params = array( + 'host' => $config->get('database.host'), + 'dbname' => $config->get('database.dbname'), + 'user' => $config->get('database.user'), + 'password' => $config->get('database.password'), + ); - $entityMangerDecorator = new \Espo\Core\EntityManager($entityManger); - $entityMangerDecorator->setMetadata($this->getContainer()->get('metadata')); + $entityManager = new \Espo\ORM\EntityManager($params); - return $entityMangerDecorator; + return $entityManager; } } diff --git a/application/Espo/ORM/Entity.php b/application/Espo/ORM/Entity.php index e217edc2aa..ecad15d1e8 100644 --- a/application/Espo/ORM/Entity.php +++ b/application/Espo/ORM/Entity.php @@ -6,7 +6,7 @@ abstract class Entity implements IEntity { public $id = null; - private $isNew = true; + private $isNew = false; /** * Entity name. @@ -131,11 +131,13 @@ abstract class Entity implements IEntity return $this->isNew; } - public function setNotNew() + public function setIsNew($isNew) { - $this->isNew = false; + $this->isNew = $isNew; } + + public function getEntityName() { return $this->entityName; diff --git a/application/Espo/ORM/EntityFactory.php b/application/Espo/ORM/EntityFactory.php index 95e865b20d..1edc529d01 100644 --- a/application/Espo/ORM/EntityFactory.php +++ b/application/Espo/ORM/EntityFactory.php @@ -15,7 +15,7 @@ class EntityFactory { $className = $this->normalizeName($name); $defs = $this->metdata->get($name); - $entity = new $className($defs); + $entity = new $className($defs); return $entity; } diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index f1002217c8..c8faf017c6 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -4,6 +4,9 @@ namespace Espo\ORM; class EntityManager { + + protected $pdo; + protected $entityFactory; protected $repositoryFactory; @@ -12,10 +15,13 @@ class EntityManager protected $metadata; - protected $repositoryHash = array(); + protected $repositoryHash = array(); + - public function __construct(\PDO $pdo, $params) - { + public function __construct($params) + { + $this->initPDO($params); + $this->metadata = new Metadata(); if (!empty($params['metadata'])) { @@ -41,6 +47,11 @@ class EntityManager $this->repositoryFactory = new $repositoryFactoryClassName($this->entityFactory, $this->mapper); } + protected function initPDO($params) + { + $this->pdo = new \PDO('mysql:host='.$params['host'].';dbname=' . $params['dbname'], $params['user'], $params['password']); + } + public function getEntity($name, $id = null) { return $this->getRepository($name)->get($id); @@ -57,7 +68,11 @@ class EntityManager public function setMetadata(array $data) { $this->metadata->setData($data); + } + + public function getPDO() + { + return $this->pdo; } } - diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index ecf4b8f4f7..996cee6413 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -61,6 +61,7 @@ class Repository { $entity = $this->entityFactory->create($this->entityName); if (empty($id)) { + $entity->setIsNew(true); return $entity; } if ($this->mapper->selectById($entity, $id)) { @@ -74,7 +75,7 @@ class Repository if ($entity->isNew()) { $result = $this->mapper->insert($entity); if ($result) { - $entity->setNotNew(); + $entity->setIsNew(false); } return $result; } else {