diff --git a/application/Espo/Controllers/Team.php b/application/Espo/Controllers/Team.php new file mode 100644 index 0000000000..5ac3c82d01 --- /dev/null +++ b/application/Espo/Controllers/Team.php @@ -0,0 +1,9 @@ +isNew()) { if ($entity->hasField('createdAt')) { $entity->set('createdAt', $nowString); @@ -54,10 +55,10 @@ class Repository extends \Espo\ORM\Repository { foreach ($entity->getRelations() as $name => $defs) { if ($defs['type'] == $entity::HAS_MANY || $defs['type'] == $entity::MANY_MANY) { - $fieldName = $name . 'Ids'; - if ($entity->has($fieldName)) { + $fieldName = $name . 'Ids'; + if ($entity->has($fieldName)) { $specifiedIds = $entity->get($fieldName); - if (is_array($ids)) { + if (is_array($specifiedIds)) { $toRemoveIds = array(); $existingIds = array(); foreach ($entity->get($name) as $foreignEntity) { @@ -67,7 +68,8 @@ 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 f74dc4a8b2..45c829e01a 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -504,7 +504,12 @@ abstract class Mapper implements IMapper $wherePart = $this->toDb($nearKey) . " = " . $this->pdo->quote($entity->id) . " ". - "AND " . $this->toDb($distantKey) . " = " . $this->pdo->quote($relEntity->id); + "AND " . $this->toDb($distantKey) . " = " . $this->pdo->quote($relEntity->id); + if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) { + foreach ($relOpt['conditions'] as $f => $v) { + $wherePart .= " AND " . $this->toDb($f) . " = " . $this->pdo->quote($v); + } + } $sql = $this->composeSelectQuery($relTable, '*', '', $wherePart); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 9dbe55901a..b9f29be7f1 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -80,13 +80,13 @@ class Record extends \Espo\Core\Services\Base $entity = $this->getRepository()->get($id); if (!empty($id)) { - if ($entity->hasRelation('teams')) { + if ($entity->hasRelation('teams') && $entity->hasField('teamsIds')) { $teams = $entity->get('teams'); $ids = array(); $names = array(); foreach ($teams as $team) { $ids[] = $team->id; - $names[$team->id] = $team->name; + $names[$team->id] = $team->get('name'); } $entity->set('teamsIds', $ids); $entity->set('teamsNames', $names); @@ -123,7 +123,7 @@ class Record extends \Espo\Core\Services\Base throw new Forbidden(); } - $entity->set($data); + $entity->set($data); $this->getRepository()->save($entity); return $entity;