From b767617d52205c87a7d0279314778318502dbfba Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 20 Dec 2013 10:45:13 +0200 Subject: [PATCH] Update only changed entity values --- application/Espo/Core/ORM/EntityManager.php | 2 +- application/Espo/Core/ORM/Repository.php | 2 +- application/Espo/ORM/DB/Mapper.php | 9 +++- application/Espo/ORM/Entity.php | 46 +++++++++++++++------ application/Espo/ORM/EntityCollection.php | 3 +- application/Espo/ORM/Repository.php | 1 + 6 files changed, 46 insertions(+), 17 deletions(-) diff --git a/application/Espo/Core/ORM/EntityManager.php b/application/Espo/Core/ORM/EntityManager.php index 921f602f36..ba47ef5879 100644 --- a/application/Espo/Core/ORM/EntityManager.php +++ b/application/Espo/Core/ORM/EntityManager.php @@ -8,7 +8,7 @@ class EntityManager extends \Espo\ORM\EntityManager protected $user; - public function setUser() + public function setUser($user) { $this->user = $user; } diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 128725c831..90fc1b804d 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -9,7 +9,7 @@ class Repository extends \Espo\ORM\Repository public function save(Entity $entity) { - $nowString = date('Y-d-m H:i:s', time()); + $nowString = date('Y-m-d H:i:s', time()); if ($entity->isNew()) { if ($entity->hasField('createdAt')) { diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index f4f009bb3e..a312f94db6 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -646,15 +646,20 @@ abstract class Mapper implements IMapper public function update(IEntity $entity) { $dataArr = $this->toArray($entity); - + $setArr = array(); foreach ($dataArr as $field => $value) { if ($field == 'id') { continue; - } + } if ($entity->fields[$field]['type'] == IEntity::FOREIGN) { continue; } + + if ($entity->getFetchedValue($field) === $value) { + continue; + } + $setArr[] = $this->toDb($field) . " = " . $this->pdo->quote($value); } diff --git a/application/Espo/ORM/Entity.php b/application/Espo/ORM/Entity.php index e2a3043252..9ca62549de 100644 --- a/application/Espo/ORM/Entity.php +++ b/application/Espo/ORM/Entity.php @@ -24,19 +24,24 @@ abstract class Entity implements IEntity * @var array Defenition of relations. * @todo make protected */ - public $relations = array(); - + public $relations = array(); /** * @var array Field-Value pairs. */ - protected $container = array(); + protected $valuesContainer = array(); + + /** + * @var array Field-Value pairs of initial values (fetched from DB). + */ + protected $initialValuesContainer = array(); /** * @var EntityManager Entity Manager. */ protected $entityManager; + public function __construct($defs = array(), EntityManager $entityManager = null) { @@ -57,12 +62,12 @@ abstract class Entity implements IEntity public function clear($name) { - unset($this->container[$name]); + unset($this->valuesContainer[$name]); } public function reset() { - $this->container = array(); + $this->valuesContainer = array(); } public function set($p1, $p2 = null) @@ -72,7 +77,6 @@ abstract class Entity implements IEntity $p2 = false; } $this->populateFromArray($p1, $p2); - return; } else { $name = $p1; $value = $p2; @@ -80,7 +84,7 @@ abstract class Entity implements IEntity $this->id = $value; } if (array_key_exists($name, $this->fields)) { - $this->container[$name] = $value; + $this->valuesContainer[$name] = $value; } } } @@ -95,8 +99,8 @@ abstract class Entity implements IEntity return $this->$method(); } - if (isset($this->container[$name])) { - return $this->container[$name]; + if (isset($this->valuesContainer[$name])) { + return $this->valuesContainer[$name]; } if (!empty($this->relations[$name])) { @@ -112,7 +116,7 @@ abstract class Entity implements IEntity if ($name == 'id') { return isset($this->id); } - if (isset($this->container[$name])) { + if (isset($this->valuesContainer[$name])) { return true; } return false; @@ -136,7 +140,7 @@ abstract class Entity implements IEntity } } - $this->container[$field] = $arr[$field]; + $this->valuesContainer[$field] = $arr[$field]; } } } @@ -168,11 +172,29 @@ abstract class Entity implements IEntity public function toArray() { - $arr = $this->container; + $arr = $this->valuesContainer; if (isset($this->id)) { $arr['id'] = $this->id; } return $arr; } + + public function getFetchedValue($fieldName) + { + if (isset($this->initialValuesContainer[$fieldName])) { + return $this->initialValuesContainer[$fieldName]; + } + return null; + } + + public function resetFetchedValues() + { + $this->initialValuesContainer = array(); + } + + public function setFresh() + { + $this->initialValuesContainer = $this->valuesContainer; + } } diff --git a/application/Espo/ORM/EntityCollection.php b/application/Espo/ORM/EntityCollection.php index 3a9e6fc712..7a66ddacc4 100644 --- a/application/Espo/ORM/EntityCollection.php +++ b/application/Espo/ORM/EntityCollection.php @@ -124,7 +124,8 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable protected function buildEntityFromArray(array $dataArray) { $entity = $this->entityFactory->create($this->entityName); - $entity->set($dataArray); + $entity->set($dataArray); + $entity->setFresh(); return $entity; } diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index 394570dcb6..4086aa362f 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -71,6 +71,7 @@ class Repository return $entity; } if ($this->mapper->selectById($entity, $id)) { + $entity->setFresh(); return $entity; } return null;