orm changes
This commit is contained in:
@@ -35,9 +35,9 @@ abstract class Mapper implements IMapper
|
||||
'*' => 'LIKE',
|
||||
'>' => '>',
|
||||
'<' => '<',
|
||||
'>=' => '>=',
|
||||
'<=' => '<=',
|
||||
'=' => '=',
|
||||
'equals' => '=',
|
||||
'like' => 'LIKE',
|
||||
);
|
||||
|
||||
// @todo whereClause ?
|
||||
@@ -58,7 +58,7 @@ abstract class Mapper implements IMapper
|
||||
}
|
||||
|
||||
public function selectById(IEntity $entity, $id)
|
||||
{
|
||||
{
|
||||
$selectPart = $this->getSelect($entity);
|
||||
$joinsPart = $this->getBelongsToJoins($entity);
|
||||
$wherePart = $this->getWhere($entity, array('id' => $id, 'deleted' => 0));
|
||||
|
||||
@@ -16,13 +16,13 @@ abstract class Entity implements IEntity
|
||||
|
||||
/**
|
||||
* @var array Defenition of fields.
|
||||
* @todo make protected in PHP 5.4
|
||||
* @todo make protected
|
||||
*/
|
||||
public $fields = array();
|
||||
|
||||
/**
|
||||
* @var array Defenition of relations.
|
||||
* @todo make protected in PHP 5.4
|
||||
* @todo make protected
|
||||
*/
|
||||
public $relations = array();
|
||||
|
||||
@@ -33,11 +33,19 @@ abstract class Entity implements IEntity
|
||||
protected $container = array();
|
||||
|
||||
|
||||
public function __construct()
|
||||
public function __construct($defs = array())
|
||||
{
|
||||
if (empty($this->entityName)) {
|
||||
$this->entityName = end(explode('\\', get_class($this)));
|
||||
}
|
||||
|
||||
if (!empty($defs['fields'])) {
|
||||
$this->fields = $defs['fields'];
|
||||
}
|
||||
|
||||
if (!empty($defs['relations'])) {
|
||||
$this->relations = $defs['relations'];
|
||||
}
|
||||
}
|
||||
|
||||
public function clear($name)
|
||||
@@ -75,6 +83,11 @@ abstract class Entity implements IEntity
|
||||
if ($name == 'id') {
|
||||
return $this->id;
|
||||
}
|
||||
$method = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->$method();
|
||||
}
|
||||
|
||||
if (isset($this->container[$name])) {
|
||||
return $this->container[$name];
|
||||
}
|
||||
|
||||
@@ -4,11 +4,18 @@ namespace Espo\ORM;
|
||||
|
||||
class EntityFactory
|
||||
{
|
||||
public function create($className)
|
||||
protected $metadata;
|
||||
|
||||
public function __construct(Metadata $metadata)
|
||||
{
|
||||
$className = $this->normalizeName($name);
|
||||
|
||||
$entity = new $className();
|
||||
$this->metadata = $metadata;
|
||||
|
||||
}
|
||||
public function create($name)
|
||||
{
|
||||
$className = $this->normalizeName($name);
|
||||
$defs = $this->metdata->get($name);
|
||||
$entity = new $className($defs);
|
||||
return $entity;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,18 +8,25 @@ class EntityManager
|
||||
|
||||
protected $repositoryFactory;
|
||||
|
||||
protected $mapper;
|
||||
protected $mapper;
|
||||
|
||||
protected $repositoryHash = array();
|
||||
protected $metadata;
|
||||
|
||||
protected $repositoryHash = array();
|
||||
|
||||
public function __construct(\PDO $pdo, $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->entityFactory = new $entityFactoryClassName($this->metadata);
|
||||
|
||||
$mapperClassName = '\\Espo\\ORM\\DB\\MysqlMapper';
|
||||
if (!empty($params['mapperClassName'])) {
|
||||
@@ -45,6 +52,11 @@ class EntityManager
|
||||
$this->repositoryHash[$name] = $this->repositoryFactory->create($name);
|
||||
}
|
||||
return $this->repositoryHash[$name];
|
||||
}
|
||||
|
||||
public function setMetadata(array $data)
|
||||
{
|
||||
$this->metadata->setData($data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,27 @@ interface IEntity
|
||||
* Resets all fields in the current model.
|
||||
*/
|
||||
function reset();
|
||||
|
||||
/**
|
||||
* Set field.
|
||||
*/
|
||||
function set($name, $value);
|
||||
|
||||
/**
|
||||
* Get field.
|
||||
*/
|
||||
function get($name);
|
||||
|
||||
/**
|
||||
* Check field is set.
|
||||
*/
|
||||
function has($name);
|
||||
|
||||
/**
|
||||
* Clear field.
|
||||
*/
|
||||
function clear($name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\ORM;
|
||||
|
||||
class Metadata
|
||||
{
|
||||
|
||||
private $data = array();
|
||||
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function get($entityName)
|
||||
{
|
||||
return $this->data[$entityName];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
return "'" . $args[0] . "'";
|
||||
}));
|
||||
|
||||
$this->entityFactory = $this->getMock('\\Espo\\ORM\\EntityFactory');
|
||||
$this->entityFactory = $this->getMockBuilder('\\Espo\\ORM\\EntityFactory')->disableOriginalConstructor()->getMock();
|
||||
$this->entityFactory->expects($this->any())
|
||||
->method('create')
|
||||
->will($this->returnCallback(function() {
|
||||
|
||||
Reference in New Issue
Block a user