orm changes
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace Espo\Core\Loaders;
|
||||
|
||||
use Doctrine\ORM\Tools\Setup,
|
||||
Espo\Core\Doctrine\ORM\Mapping\Driver\EspoPHPDriver;
|
||||
Espo\Core\Doctrine\ORM\Mapping\Driver\EspoPHPDriver;
|
||||
|
||||
class EntityManager
|
||||
{
|
||||
|
||||
@@ -32,7 +32,7 @@ class Auth extends \Slim\Middleware
|
||||
if (!empty($routes[0])) {
|
||||
$routeConditions = $routes[0]->getConditions();
|
||||
if (isset($routeConditions['auth']) && $routeConditions['auth'] === false) {
|
||||
$this->container->setUser(new \Espo\Entities\User());
|
||||
$this->container->setUser($this->entityManager->getEntity('User'));
|
||||
$this->next->call();
|
||||
return;
|
||||
}
|
||||
@@ -49,9 +49,9 @@ class Auth extends \Slim\Middleware
|
||||
$password = $authSec;
|
||||
|
||||
|
||||
$user = $this->entityManager->getRepository('User')->findOneBy(array('username' => $username));
|
||||
$user = $this->entityManager->getRepository('User')->findOne(array('username' => $username));
|
||||
if ($user) {
|
||||
if ($password == $user->getPassword()) {
|
||||
if ($password == $user->get('password')) {
|
||||
$this->container->setUser($user);
|
||||
$isAuthenticated = true;
|
||||
}
|
||||
|
||||
@@ -7,50 +7,10 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||
/**
|
||||
* @Entity @Table(name="users")
|
||||
*/
|
||||
class User
|
||||
class User extends \Espo\ORM\Entity
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $username;
|
||||
|
||||
protected $password;
|
||||
|
||||
protected $isAdmin;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function isAdmin()
|
||||
{
|
||||
return $this->isAdmin;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
|
||||
return $this->get('isAdmin');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,10 +134,8 @@ abstract class Entity implements IEntity
|
||||
public function setIsNew($isNew)
|
||||
{
|
||||
$this->isNew = $isNew;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getEntityName()
|
||||
{
|
||||
return $this->entityName;
|
||||
@@ -152,5 +150,14 @@ abstract class Entity implements IEntity
|
||||
{
|
||||
return isset($this->relations[$relationName]);
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$arr = $this->container;
|
||||
if (isset($this->id)) {
|
||||
$arr['id'] = $this->id;
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -190,6 +190,15 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$arr = array();
|
||||
foreach ($this as $entity) {
|
||||
$arr[] = $entity->toArray();
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,15 @@ class Repository
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function findOne(array $params = array())
|
||||
{
|
||||
$collection = $this->find($params);
|
||||
if (count($collection)) {
|
||||
return $collection[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findRelated(Entity $entity, $relationName, array $params = array())
|
||||
{
|
||||
$dataArr = $this->mapper->selectRelated($entity, $relationName, $params);
|
||||
|
||||
@@ -21,7 +21,7 @@ class Record extends \Espo\Core\Services\Base
|
||||
|
||||
private $metadata;
|
||||
|
||||
private $queryManager;
|
||||
private $selectManager;
|
||||
|
||||
private $acl;
|
||||
|
||||
@@ -70,43 +70,47 @@ class Record extends \Espo\Core\Services\Base
|
||||
{
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
public function getEntity($id)
|
||||
|
||||
protected function getRepository()
|
||||
{
|
||||
return $this->getEntityManager()->getRepository($this->name)->find($id);
|
||||
return $this->getEntityManager()->getRepository($this->name);
|
||||
}
|
||||
|
||||
public function getEntity($id = null)
|
||||
{
|
||||
return $this->getRepository()->get($id);
|
||||
}
|
||||
|
||||
protected function getQueryManager()
|
||||
protected function getSelectManager()
|
||||
{
|
||||
if (empty($this->queryManager)) {
|
||||
$this->queryManager = new QueryManager($this->entityManager, $this->getUser(), $this->getAcl());
|
||||
if (empty($this->selectManager)) {
|
||||
$this->selectManager = new SelectManager($this->entityManager, $this->getUser(), $this->getAcl());
|
||||
}
|
||||
return $this->queryManager;
|
||||
return $this->selectManager;
|
||||
}
|
||||
|
||||
public function createEntity($data)
|
||||
{
|
||||
// TODO validate $data
|
||||
$entity = $this->getEntityManager()->createEntity($this->name);
|
||||
$entity->fromArray($data);
|
||||
$this->getEntityManager()->persist($entity);
|
||||
$this->getEntityManager()->flush();
|
||||
return $entity;
|
||||
$entity = $this->getEntity();
|
||||
|
||||
$entity->set($data);
|
||||
$this->getRepository()->save($entity);
|
||||
return $entity->toArray();
|
||||
}
|
||||
|
||||
public function updateEntity($id, $data)
|
||||
{
|
||||
// TODO validate $data
|
||||
$entity = $this->getEntity($id);
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'edit')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
// TODO validate $data
|
||||
$entity->fromArray($data);
|
||||
$this->getEntityManager()->persist($entity);
|
||||
$this->getEntityManager()->flush();
|
||||
return $entity;
|
||||
|
||||
$entity->set($data);
|
||||
$this->getRepository()->save($entity);
|
||||
return $entity->toArray();
|
||||
}
|
||||
|
||||
public function deleteEntity($id)
|
||||
@@ -117,47 +121,48 @@ class Record extends \Espo\Core\Services\Base
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$this->getEntityManager()->remove($entity);
|
||||
$this->getEntityManager()->flush();
|
||||
return true;
|
||||
return $this->getRepository()->remove($entity);
|
||||
}
|
||||
|
||||
public function findEntities($params)
|
||||
{
|
||||
$collection = $this->getEntityManager()->getRepository($this->name)->find();
|
||||
$qu = $this->getQueryManager()->createListQuery($this->name, $params);
|
||||
{
|
||||
$selectParams = $this->getSelectManager()->getSelectParams($this->name, $params, true);
|
||||
$collection = $this->getRepository()->find($selectParams);
|
||||
|
||||
$collection = $qu->getResult();
|
||||
return $collection;
|
||||
return array(
|
||||
'total' => $this->getRepository()->count($selectParams),
|
||||
'list' => $collection->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function findLinkedEntities($id, $link, $params)
|
||||
{
|
||||
$entity = $this->getEntity($id);
|
||||
|
||||
$entityName = $this->getEntityManager()->getEntityName($entity);
|
||||
$foreignEntityName = $this->getMetadata()->get('entityDefs.' . $entityName . '.links.' . $link . '.entity');
|
||||
|
||||
{
|
||||
$entity = $this->getEntity($id);
|
||||
$foreignEntityName = $entity->relations[$link]['entity'];
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'read')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
if (!$this->getAcl()->check($foreignEntityName, 'read')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$qu = $this->getQueryManager()->createLinkedListQuery($entity, $link, $params);
|
||||
}
|
||||
|
||||
$selectParams = $this->getSelectManager()->getSelectParams($foreignEntityName, $params, true);
|
||||
$collection = $this->getRepository()->findRelated($entity, $link, $selectParams);
|
||||
|
||||
$collection = $qu->getResult();
|
||||
return $collection;
|
||||
return array(
|
||||
'total' => $this->getRepository()->countRelated($entity, $link, $selectParams),
|
||||
'list' => $collection->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function linkEntity($id, $link, $foreignId)
|
||||
{
|
||||
$entity = $this->getEntity($id);
|
||||
|
||||
$entityName = $this->getEntityManager()->getEntityName($entity);
|
||||
$foreignEntityName = $this->getMetadata()->get('entityDefs.' . $entityName . '.links.' . $link . '.entity');
|
||||
|
||||
$entityName = $entity->getEntityName($entity);
|
||||
$foreignEntityName = $entity->relations[$link]['entity'];
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'edit')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
@@ -166,29 +171,36 @@ class Record extends \Espo\Core\Services\Base
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$methodName = 'get' . ucfirst($link);
|
||||
$foreignEntity = $this->getEntityManager()->getRepository($foreignEntityName)->find($foreignId);
|
||||
$foreignEntity = $this->getEntityManager()->getEntity($foreignEntityName, $foreignId);
|
||||
|
||||
|
||||
if (!empty($foreignEntity)) {
|
||||
$entity->$methodName()->add($foreignEntity);
|
||||
$this->getRepository()->relate($entity, $link, $foreignEntity);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function unlinkEntity($id, $link, $foreignId)
|
||||
{
|
||||
$entity = $this->getEntity($id);
|
||||
$entity = $this->getEntity($id);
|
||||
|
||||
$entityName = $this->getEntityManager()->getEntityName($entity);
|
||||
$foreignEntityName = $this->getMetadata()->get('entityDefs.' . $entityName . '.links.' . $link . '.entity');
|
||||
$entityName = $entity->getEntityName($entity);
|
||||
$foreignEntityName = $entity->relations[$link]['entity'];
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'edit')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
if (empty($foreignEntityName)) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$methodName = 'get' . ucfirst($link);
|
||||
$entity->$methodName()->remove($foreignId);
|
||||
return true;
|
||||
$foreignEntity = $this->getEntityManager()->getEntity($foreignEntityName, $foreignId);
|
||||
|
||||
if (!empty($foreignEntity)) {
|
||||
$this->getRepository()->unrelate($entity, $link, $foreignEntity);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user