mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
orm refactoring. preferences
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
data/logs/*
|
||||
data/cache/*
|
||||
data/upload/*
|
||||
data/preferences/*
|
||||
application/config.php
|
||||
/test.php
|
||||
/test2.php
|
||||
|
||||
@@ -9,7 +9,7 @@ class App extends \Espo\Core\Controllers\Record
|
||||
return array(
|
||||
'user' => $this->getUser()->toArray(),
|
||||
'acl' => $this->getAcl()->toArray(),
|
||||
'preferences' => array(),
|
||||
'preferences' => $this->getPreferences()->toArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
37
application/Espo/Controllers/Preferences.php
Normal file
37
application/Espo/Controllers/Preferences.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Controllers;
|
||||
|
||||
use \Espo\Core\Exceptions\Error;
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\NotFound;
|
||||
|
||||
class Preferences extends \Espo\Core\Controllers\Base
|
||||
{
|
||||
protected function getPreferences()
|
||||
{
|
||||
return $this->getContainer()->get('preferences');
|
||||
}
|
||||
|
||||
protected function getEntityManager()
|
||||
{
|
||||
return $this->getContainer()->get('entityManager');
|
||||
}
|
||||
|
||||
public function actionRead($params)
|
||||
{
|
||||
$userId = $params['id'];
|
||||
if (!$this->getUser()->isAdmin()) {
|
||||
if ($this->getUser()->id != $userId) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
$entity = $this->getEntityManager()->getEntity('Preferences', $userId);
|
||||
if ($entity) {
|
||||
return $entity->toArray();
|
||||
}
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,6 +67,11 @@ class Container
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function loadPreferences()
|
||||
{
|
||||
return $this->get('entityManager')->getEntity('Preferences', $this->get('user')->id);
|
||||
}
|
||||
|
||||
private function loadConfig()
|
||||
{
|
||||
@@ -123,7 +128,6 @@ class Container
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private function loadLayout()
|
||||
{
|
||||
return new \Espo\Core\Utils\Layout(
|
||||
@@ -165,7 +169,6 @@ class Container
|
||||
public function setUser($user)
|
||||
{
|
||||
$this->data['user'] = $user;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,11 @@ abstract class Base
|
||||
return $this->container->get('config');
|
||||
}
|
||||
|
||||
protected function getPreferences()
|
||||
{
|
||||
return $this->container->get('preferences');
|
||||
}
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->container->get('metadata');
|
||||
|
||||
9
application/Espo/Core/ORM/DB/MysqlMapper.php
Normal file
9
application/Espo/Core/ORM/DB/MysqlMapper.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Core\ORM\DB;
|
||||
|
||||
class MysqlMapper extends \Espo\ORM\DB\MysqlMapper
|
||||
{
|
||||
protected $returnCollection = false;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ class EntityManager extends \Espo\ORM\EntityManager
|
||||
public function setEspoMetadata($espoMetadata)
|
||||
{
|
||||
$this->espoMetadata = $espoMetadata;
|
||||
$this->repositoryFactory->setEspoMetadata($espoMetadata);
|
||||
}
|
||||
|
||||
public function setHookManager(\Espo\Core\HookManager $hookManager)
|
||||
@@ -45,11 +46,5 @@ class EntityManager extends \Espo\ORM\EntityManager
|
||||
{
|
||||
return $this->espoMetadata->getEntityPath($name);
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->mapper->setReturnCollection(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,16 @@ namespace Espo\Core\ORM;
|
||||
|
||||
class Repository extends \Espo\ORM\Repository
|
||||
{
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
public function setMetadata($metadata)
|
||||
{
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
protected function handleSelectParams(&$params, $entityName = false)
|
||||
{
|
||||
|
||||
@@ -3,9 +3,22 @@
|
||||
namespace Espo\Core\ORM;
|
||||
|
||||
class RepositoryFactory extends \Espo\ORM\RepositoryFactory
|
||||
{
|
||||
{
|
||||
protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repository';
|
||||
|
||||
protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repository';
|
||||
protected $espoMetadata = false;
|
||||
|
||||
public function setEspoMetadata($espoMetadata)
|
||||
{
|
||||
$this->espoMetadata = $espoMetadata;
|
||||
}
|
||||
|
||||
public function create($name)
|
||||
{
|
||||
$repository = parent::create($name);
|
||||
$repository->setMetadata($this->espoMetadata);
|
||||
return $repository;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
8
application/Espo/Entities/Preferences.php
Normal file
8
application/Espo/Entities/Preferences.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Entities;
|
||||
|
||||
class Preferences extends \Espo\Core\ORM\Entity
|
||||
{
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class EntityManager
|
||||
|
||||
protected $repositoryFactory;
|
||||
|
||||
protected $mapper;
|
||||
protected $mappers = array();
|
||||
|
||||
protected $metadata;
|
||||
|
||||
@@ -32,21 +32,24 @@ class EntityManager
|
||||
$entityFactoryClassName = $params['entityFactoryClassName'];
|
||||
}
|
||||
$this->entityFactory = new $entityFactoryClassName($this, $this->metadata);
|
||||
|
||||
$mapperClassName = '\\Espo\\ORM\\DB\\MysqlMapper';
|
||||
if (!empty($params['mapperClassName'])) {
|
||||
$mapperClassName = $params['mapperClassName'];
|
||||
}
|
||||
$this->mapper = new $mapperClassName($this->pdo, $this->entityFactory);
|
||||
|
||||
|
||||
$repositoryFactoryClassName = '\\Espo\\ORM\\RepositoryFactory';
|
||||
if (!empty($params['repositoryFactoryClassName'])) {
|
||||
$repositoryFactoryClassName = $params['repositoryFactoryClassName'];
|
||||
}
|
||||
$this->repositoryFactory = new $repositoryFactoryClassName($this, $this->entityFactory, $this->mapper);
|
||||
$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)
|
||||
{
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace Espo\ORM;
|
||||
|
||||
|
||||
// TODO make it abstract; use Mysql Repostitory
|
||||
class Repository
|
||||
{
|
||||
|
||||
public static $mapperClassName = '\\Espo\\Core\\ORM\\DB\\MysqlMapper';
|
||||
|
||||
/**
|
||||
* @var EntityFactory EntityFactory object.
|
||||
*/
|
||||
@@ -17,7 +19,7 @@ class Repository
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* @var \MyApp\DB\iMapper DB Mapper.
|
||||
* @var Object Mapper.
|
||||
*/
|
||||
protected $mapper;
|
||||
|
||||
@@ -46,16 +48,23 @@ class Repository
|
||||
*/
|
||||
protected $listParams = array();
|
||||
|
||||
public function __construct($entityName, EntityManager $entityManager, EntityFactory $entityFactory, DB\iMapper $mapper)
|
||||
|
||||
public function __construct($entityName, EntityManager $entityManager, EntityFactory $entityFactory)
|
||||
{
|
||||
$this->entityName = $entityName;
|
||||
$this->entityFactory = $entityFactory;
|
||||
$this->seed = $this->entityFactory->create($entityName);
|
||||
$this->entityClassName = get_class($this->seed);
|
||||
$this->entityManager = $entityManager;
|
||||
|
||||
$this->mapper = $mapper;
|
||||
}
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
protected function getMapper()
|
||||
{
|
||||
if (empty($this->mapper)) {
|
||||
$this->mapper = $this->getEntityManager()->getMapper(self::$mapperClassName);
|
||||
}
|
||||
return $this->mapper;
|
||||
}
|
||||
|
||||
protected function handleSelectParams(&$params)
|
||||
{
|
||||
@@ -90,7 +99,7 @@ class Repository
|
||||
$this->handleSelectParams($params);
|
||||
|
||||
$entity = $this->entityFactory->create($this->entityName);
|
||||
if ($this->mapper->selectById($entity, $id, $params)) {
|
||||
if ($this->getMapper()->selectById($entity, $id, $params)) {
|
||||
$entity->setAsFetched();
|
||||
return $entity;
|
||||
}
|
||||
@@ -117,12 +126,12 @@ class Repository
|
||||
{
|
||||
$this->beforeSave($entity);
|
||||
if ($entity->isNew()) {
|
||||
$result = $this->mapper->insert($entity);
|
||||
$result = $this->getMapper()->insert($entity);
|
||||
if ($result) {
|
||||
$entity->setIsNew(false);
|
||||
}
|
||||
} else {
|
||||
$result = $this->mapper->update($entity);
|
||||
$result = $this->getMapper()->update($entity);
|
||||
}
|
||||
if ($result) {
|
||||
$this->afterSave($entity);
|
||||
@@ -141,7 +150,7 @@ class Repository
|
||||
public function remove(Entity $entity)
|
||||
{
|
||||
$this->beforeRemove($entity);
|
||||
$result = $this->mapper->delete($entity);
|
||||
$result = $this->getMapper()->delete($entity);
|
||||
if ($result) {
|
||||
$this->afterRemove($entity);
|
||||
}
|
||||
@@ -153,7 +162,7 @@ class Repository
|
||||
$this->handleSelectParams($params);
|
||||
$params = $this->getSelectParams($params);
|
||||
|
||||
$dataArr = $this->mapper->select($this->seed, $params);
|
||||
$dataArr = $this->getMapper()->select($this->seed, $params);
|
||||
|
||||
$collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory);
|
||||
$this->reset();
|
||||
@@ -175,7 +184,7 @@ class Repository
|
||||
$entityName = $entity->relations[$relationName]['entity'];
|
||||
$this->handleSelectParams($params, $entityName);
|
||||
|
||||
$dataArr = $this->mapper->selectRelated($entity, $relationName, $params);
|
||||
$dataArr = $this->getMapper()->selectRelated($entity, $relationName, $params);
|
||||
|
||||
$collection = new EntityCollection($dataArr, $entityName, $this->entityFactory);
|
||||
return $collection;
|
||||
@@ -186,16 +195,16 @@ class Repository
|
||||
$entityName = $entity->relations[$relationName]['entity'];
|
||||
$this->handleSelectParams($params, $entityName);
|
||||
|
||||
return $this->mapper->countRelated($entity, $relationName, $params);
|
||||
return $this->getMapper()->countRelated($entity, $relationName, $params);
|
||||
}
|
||||
|
||||
public function relate(Entity $entity, $relationName, $foreign)
|
||||
{
|
||||
if ($foreign instanceof Entity) {
|
||||
return $this->mapper->relate($entity, $relationName, $foreign);
|
||||
return $this->getMapper()->relate($entity, $relationName, $foreign);
|
||||
}
|
||||
if (is_string($foreign)) {
|
||||
return $this->mapper->addRelation($entity, $relationName, $foreign);
|
||||
return $this->getMapper()->addRelation($entity, $relationName, $foreign);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -203,13 +212,13 @@ class Repository
|
||||
public function unrelate(Entity $entity, $relationName, $foreign)
|
||||
{
|
||||
if ($foreign instanceof Entity) {
|
||||
return $this->mapper->unrelate($entity, $relationName, $foreign);
|
||||
return $this->getMapper()->unrelate($entity, $relationName, $foreign);
|
||||
}
|
||||
if (is_string($foreign)) {
|
||||
return $this->mapper->removeRelation($entity, $relationName, $foreign);
|
||||
return $this->getMapper()->removeRelation($entity, $relationName, $foreign);
|
||||
}
|
||||
if ($foreign === true) {
|
||||
return $this->mapper->removeAllRelations($entity, $relationName);
|
||||
return $this->getMapper()->removeAllRelations($entity, $relationName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -225,25 +234,25 @@ class Repository
|
||||
$this->handleSelectParams($params);
|
||||
|
||||
$params = $this->getSelectParams($params);
|
||||
return $this->mapper->count($this->seed, $params);
|
||||
return $this->getMapper()->count($this->seed, $params);
|
||||
}
|
||||
|
||||
public function max($field)
|
||||
{
|
||||
$params = $this->getSelectParams();
|
||||
return $this->mapper->max($this->seed, $params, $field);
|
||||
return $this->getMapper()->max($this->seed, $params, $field);
|
||||
}
|
||||
|
||||
public function min($field)
|
||||
{
|
||||
$params = $this->getSelectParams();
|
||||
return $this->mapper->min($this->seed, $params, $field);
|
||||
return $this->getMapper()->min($this->seed, $params, $field);
|
||||
}
|
||||
|
||||
public function sum($field)
|
||||
{
|
||||
$params = $this->getSelectParams();
|
||||
return $this->mapper->sum($this->seed, $params, $field);
|
||||
return $this->getMapper()->sum($this->seed, $params, $field);
|
||||
}
|
||||
|
||||
// @TODO use abstract class for list params
|
||||
|
||||
@@ -10,11 +10,10 @@ class RepositoryFactory
|
||||
|
||||
protected $defaultRepositoryClassName = '\\Espo\\ORM\\Repository';
|
||||
|
||||
public function __construct(EntityManager $entityManager, EntityFactory $entityFactroy, DB\IMapper $mapper)
|
||||
public function __construct(EntityManager $entityManager, EntityFactory $entityFactroy)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->entityFactroy = $entityFactroy;
|
||||
$this->mapper = $mapper;
|
||||
}
|
||||
|
||||
public function create($name)
|
||||
@@ -24,7 +23,8 @@ class RepositoryFactory
|
||||
if (!class_exists($className)) {
|
||||
$className = $this->defaultRepositoryClassName;
|
||||
}
|
||||
$repository = new $className($name, $this->entityManager, $this->entityFactroy, $this->mapper);
|
||||
|
||||
$repository = new $className($name, $this->entityManager, $this->entityFactroy);
|
||||
return $repository;
|
||||
}
|
||||
|
||||
|
||||
38
application/Espo/Repositories/Preferences.php
Normal file
38
application/Espo/Repositories/Preferences.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Repositories;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class Preferences extends \Espo\Core\ORM\Repository
|
||||
{
|
||||
protected $data = array();
|
||||
|
||||
protected $entityName = 'Preferences';
|
||||
|
||||
public function get($id = null)
|
||||
{
|
||||
if ($id) {
|
||||
$entity = $this->entityFactory->create('Preferences');
|
||||
if (empty($this->data[$id])) {
|
||||
$fileName = 'data/preferences/' . $id;
|
||||
if (file_exists($fileName)) {
|
||||
$this->data[$id] = include ($fileName);
|
||||
} else {
|
||||
$fields = $this->getMetadata()->get('entityDefs.Preferences.fields');
|
||||
$defaults = array();
|
||||
foreach ($fields as $field => $d) {
|
||||
if (array_key_exists('default', $d)) {
|
||||
$defaults[$field] = $d['default'];
|
||||
}
|
||||
}
|
||||
$this->data[$id] = $defaults;
|
||||
}
|
||||
}
|
||||
$entity->set($this->data[$id]);
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user