orm changes

This commit is contained in:
Yuri Kuznetsov
2013-12-06 13:52:52 +02:00
parent 5aa3b55ba6
commit 3f64035e87
5 changed files with 36 additions and 23 deletions
@@ -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;
}
}
+5 -3
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+19 -4
View File
@@ -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;
}
}
+2 -1
View File
@@ -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 {