This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
espocrm-base/application/Espo/ORM/EntityManager.php
T
2014-01-23 19:59:16 +02:00

121 lines
2.5 KiB
PHP

<?php
namespace Espo\ORM;
class EntityManager
{
protected $pdo;
protected $entityFactory;
protected $repositoryFactory;
protected $mappers = array();
protected $metadata;
protected $repositoryHash = array();
public function __construct($params)
{
$this->initPDO($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, $this->metadata);
$repositoryFactoryClassName = '\\Espo\\ORM\\RepositoryFactory';
if (!empty($params['repositoryFactoryClassName'])) {
$repositoryFactoryClassName = $params['repositoryFactoryClassName'];
}
$this->repositoryFactory = new $repositoryFactoryClassName($this, $this->entityFactory);
$this->init();
}
public function getMapper($className)
{
if (empty($this->mappers[$className])) {
$this->mappers[$className] = new $className($this->pdo, $this->entityFactory);
}
return $this->mappers[$className];
}
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);
}
public function saveEntity(Entity $entity)
{
$entityName = $entity->getEntityName();
return $this->getRepository($entityName)->save($entity);
}
public function removeEntity(Entity $entity)
{
$entityName = $entity->getEntityName();
return $this->getRepository($entityName)->remove($entity);
}
public function getRepository($name)
{
if (empty($this->repositoryHash[$name])) {
$this->repositoryHash[$name] = $this->repositoryFactory->create($name);
}
return $this->repositoryHash[$name];
}
public function setMetadata(array $data)
{
$this->metadata->setData($data);
}
public function getMetadata()
{
return $this->metadata;
}
public function getPDO()
{
return $this->pdo;
}
public function normalizeRepositoryName($name)
{
return $name;
}
public function normalizeEntityName($name)
{
return $name;
}
public function createCollection($entityName, $data = array())
{
$seed = $this->getEntity($entityName);
$collection = new EntityCollection($data, $seed, $this->entityFactory);
return $collection;
}
protected function init()
{
}
}