From 65fdabf83ce10ebd167b00501c792cecac417019 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 27 Dec 2013 13:53:47 +0200 Subject: [PATCH] changes and dev --- application/Espo/Core/ORM/Repository.php | 3 +-- application/Espo/ORM/DB/Mapper.php | 6 +++--- application/Espo/ORM/DB/MysqlMapper.php | 2 +- application/Espo/ORM/EntityCollection.php | 3 ++- application/Espo/Services/Record.php | 24 ++++++++++++++++++++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 04e5800415..a41bfe7e14 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -72,8 +72,7 @@ class Repository extends \Espo\ORM\Repository if (!in_array($id, $specifiedIds)) { $toRemoveIds[] = $id; } - } - + } foreach ($specifiedIds as $id) { if (!in_array($id, $existingIds)) { $this->relate($entity, $name, $id); diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index 0d19ef0ea1..47b212e6a6 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -678,7 +678,7 @@ abstract class Mapper implements IMapper $valuesPart = implode(", ", $valArr); $sql = $this->composeInsertQuery($this->toDb($entity->getEntityName()), $fieldsPart, $valuesPart); - + if ($this->pdo->query($sql)) { return $entity->id; } @@ -1074,7 +1074,7 @@ abstract class Mapper implements IMapper protected function composeInsertQuery($table, $fields, $values) { - $sql = "INSERT INTO {$table}"; + $sql = "INSERT INTO `{$table}`"; $sql .= " ({$fields})"; if (!is_array($values)) { $sql .= " VALUES ({$values})"; @@ -1087,7 +1087,7 @@ abstract class Mapper implements IMapper protected function composeUpdateQuery($table, $set, $where) { - $sql = "UPDATE {$table} SET {$set} WHERE {$where}"; + $sql = "UPDATE `{$table}` SET {$set} WHERE {$where}"; return $sql; } diff --git a/application/Espo/ORM/DB/MysqlMapper.php b/application/Espo/ORM/DB/MysqlMapper.php index 8aaf074331..f829a2cf3d 100644 --- a/application/Espo/ORM/DB/MysqlMapper.php +++ b/application/Espo/ORM/DB/MysqlMapper.php @@ -21,7 +21,7 @@ class MysqlMapper extends Mapper $sql .= " DISTINCT"; } - $sql .= " {$select} FROM {$table}"; + $sql .= " {$select} FROM `{$table}`"; if (!empty($joins)) { $sql .= " {$joins}"; diff --git a/application/Espo/ORM/EntityCollection.php b/application/Espo/ORM/EntityCollection.php index 7a66ddacc4..b003f7518f 100644 --- a/application/Espo/ORM/EntityCollection.php +++ b/application/Espo/ORM/EntityCollection.php @@ -9,9 +9,10 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable private $entityName; private $position = 0; + protected $container = array(); - public function __construct(array $data, $seed = null, EntityFactory $entityFactory = null) + public function __construct($data, $seed = null, EntityFactory $entityFactory = null) { $this->container = $data; diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index b1e469c610..0e4f37e673 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -79,7 +79,8 @@ class Record extends \Espo\Core\Services\Base { $entity = $this->getRepository()->get($id); if (!empty($entity) && !empty($id)) { - $this->loadLinkMultipleFields($entity); + $this->loadLinkMultipleFields($entity); + $this->loadParentNameFields($entity); } return $entity; } @@ -105,6 +106,23 @@ class Record extends \Espo\Core\Services\Base } } + protected function loadParentNameFields($entity) + { + $fieldDefs = $this->getMetadata()->get('entityDefs.' . $this->entityName . '.fields', array()); + foreach ($fieldDefs as $field => $defs) { + if ($defs['type'] == 'linkParent') { + $id = $entity->get($field . 'Id'); + $scope = $entity->get($field . 'Type'); + + if ($scope) { + if ($foreignEntity = $this->getEntityManager()->getEntity($scope, $id)) { + $entity->set($field . 'Name', $foreignEntity->get('name')); + } + } + } + } + } + protected function getSelectManager() { if (empty($this->selectManager)) { @@ -118,11 +136,11 @@ class Record extends \Espo\Core\Services\Base // TODO validate $data $entity = $this->getEntity(); - $entity->set($data); + $entity->set($data); if ($this->getRepository()->save($entity)) { return $entity; - } + } throw new Error(); }