diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 4197db57ed..50aa19b68f 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -7,8 +7,7 @@ class Repository extends \Espo\ORM\Repository public function save(Entity $entity) { - $nowString = date('Y-m-d H:i:s', time()); - + $nowString = date('Y-m-d H:i:s', time()); $restoreData = array(); if ($entity->isNew()) { @@ -47,6 +46,40 @@ class Repository extends \Espo\ORM\Repository parent::save($entity); $entity->set($restoreData); + + $this->handleSpecifiedRelations($entity); + } + + protected function handleSpecifiedRelations(Entity $entity) + { + foreach ($entity->getRelations() as $name => $defs) { + if ($defs['type'] == $entity::HAS_MANY || $defs['type'] == $entity::MANY_MANY) { + $fieldName = $name . 'Ids'; + if ($entity->has($fieldName)) { + $specifiedIds = $entity->get($fieldName); + if (is_array($ids)) { + $toRemoveIds = array(); + $existingIds = array(); + foreach ($entity->get($name) as $foreignEntity) { + $existingIds[] = $foreignEntity->id; + } + foreach ($existingIds as $id) { + if (!in_array($id, $specifiedIds)) { + $toRemoveIds[] = $id; + } + } + foreach ($specifiedIds as $id) { + if (!in_array($id, $existingIds)) { + $this->relate($entity, $name, $id); + } + } + foreach ($toRemoveIds as $id) { + $this->unrelate($entity, $name, $id); + } + } + } + } + } } } diff --git a/application/Espo/ORM/Entity.php b/application/Espo/ORM/Entity.php index b44ae53ad0..02902adfac 100644 --- a/application/Espo/ORM/Entity.php +++ b/application/Espo/ORM/Entity.php @@ -171,13 +171,7 @@ abstract class Entity implements IEntity } public function toArray() - { - /*$arr = $this->valuesContainer; - if (isset($this->id)) { - $arr['id'] = $this->id; - } - return $arr;*/ - + { $arr = array(); if (isset($this->id)) { $arr['id'] = $this->id; @@ -192,6 +186,16 @@ abstract class Entity implements IEntity return $arr; } + public function getFields() + { + return $this->fields; + } + + public function getRelations() + { + return $this->relations; + } + public function getFetchedValue($fieldName) { if (isset($this->initialValuesContainer[$fieldName])) { diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index 4086aa362f..d2ca53c3f6 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -131,14 +131,29 @@ class Repository return $this->mapper->countRelated($entity, $relationName, $params); } - public function relate(Entity $entity, $relationName, $foreignEntity) + public function relate(Entity $entity, $relationName, $foreign) { - return $this->mapper->relate($entity, $relationName, $foreignEntity); + if ($foreign instanceof Entity) { + return $this->mapper->relate($entity, $relationName, $foreign); + } + if (is_string($foreign)) { + return $this->mapper->addRelation($entity, $relationName, $foreign); + } + return false; } - public function unrelate(Entity $entity, $relationName, $foreignEntity) + public function unrelate(Entity $entity, $relationName, $foreign) { - return $this->mapper->unrelate($entity, $relationName, $foreignEntity); + if ($foreign instanceof Entity) { + return $this->mapper->unrelate($entity, $relationName, $foreign); + } + if (is_string($foreign)) { + return $this->mapper->removeRelation($entity, $relationName, $foreign); + } + if ($foreign === true) { + return $this->mapper->removeAllRelations($entity, $relationName); + } + return false; } public function getAll() diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 8c668ca6b6..9dbe55901a 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -77,7 +77,23 @@ class Record extends \Espo\Core\Services\Base public function getEntity($id = null) { - return $this->getRepository()->get($id); + $entity = $this->getRepository()->get($id); + + if (!empty($id)) { + if ($entity->hasRelation('teams')) { + $teams = $entity->get('teams'); + $ids = array(); + $names = array(); + foreach ($teams as $team) { + $ids[] = $team->id; + $names[$team->id] = $team->name; + } + $entity->set('teamsIds', $ids); + $entity->set('teamsNames', $names); + } + } + + return $entity; } protected function getSelectManager()