diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index dba5d9018a..bf5bf561c3 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -4,26 +4,19 @@ namespace Espo\Core\ORM; class Repository extends \Espo\ORM\Repository { - protected function getEntityById($id) - { - $entity = $this->getEntityFactory()->create($this->entityName); - $params = array(); - $this->handleEmailAddressParams($params); - if ($this->mapper->selectById($entity, $id, $params)) { - $entity->setFresh(); - return $entity; + + protected function handleSelectParams(&$params, $entityName = false) + { + $this->handleEmailAddressParams($params, $entityName); + } + + protected function handleEmailAddressParams(&$params, $entityName = false) + { + if (empty($entityName)) { + $entityName = $this->entityName; } - return null; - } - - protected function handleSelectParams(&$params) - { - $this->handleEmailAddressParams($params); - } - - protected function handleEmailAddressParams(&$params) - { - $defs = $this->getEntityManager()->getMetadata()->get($this->entityName); + + $defs = $this->getEntityManager()->getMetadata()->get($entityName); if (!empty($defs['relations']) && array_key_exists('emailAddresses', $defs['relations'])) { if (empty($params['leftJoins'])) { $params['leftJoins'] = array(); @@ -102,75 +95,43 @@ class Repository extends \Espo\ORM\Repository $email = $entity->get('emailAddress'); $pdo = $this->getPDO(); + $emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress'); + if (!empty($email)) { - if ($email != $entity->getFetchedValue('emailAddress')) { - $emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress'); - $emailAddress = $emailAddressRepository->where(array('lower' => strtolower($email)))->findOne(); + if ($email != $entity->getFetchedValue('emailAddress')) { + + $emailAddressNew = $emailAddressRepository->where(array('lower' => strtolower($email)))->findOne(); $isNewEmailAddress = false; - if (!$emailAddress) { - $emailAddress = $emailAddressRepository->get(); - $emailAddress->set('name', $email); - $emailAddressRepository->save($emailAddress); + if (!$emailAddressNew) { + $emailAddressNew = $emailAddressRepository->get(); + $emailAddressNew->set('name', $email); + $emailAddressRepository->save($emailAddressNew); $isNewEmailAddress = true; } + $emailOld = $entity->getFetchedValue('emailAddress'); + if (!empty($emailOld)) { + $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); + $this->unrelate($entity, 'emailAddresses', $emailAddressOld); + } + $this->relate($entity, 'emailAddresses', $emailAddressNew); + $query = " UPDATE entity_email_address - SET `primary` = 0 + SET `primary` = 1 WHERE entity_id = ".$pdo->quote($entity->id)." AND - entity_type = ".$pdo->quote($this->entityName)." + entity_type = ".$pdo->quote($this->entityName)." AND + email_address_id = ".$pdo->quote($emailAddressNew->id)." "; $sth = $pdo->prepare($query); $sth->execute(); - - $sth = null; - if (!$isNewEmailAddress) { - $query = " - SELECT * FROM entity_email_address - WHERE - entity_id = ".$pdo->quote($entity->id)." AND - entity_type = ".$pdo->quote($this->entityName)." AND - email_address_id = ".$pdo->quote($emailAddress->id)." - "; - $sth = $pdo->prepare($query); - $sth->execute(); - } - if (!$isNewEmailAddress && $sth->fetch()) { - $query = " - UPDATE entity_email_address - SET `primary` = 1 - WHERE - entity_id = ".$pdo->quote($entity->id)." AND - entity_type = ".$pdo->quote($this->entityName)." AND - email_address_id = ".$pdo->quote($emailAddress->id)." - "; - $sth = $pdo->prepare($query); - $sth->execute(); - } else { - $query = " - INSERT INTO entity_email_address - (entity_id, entity_type, email_address_id, `primary`) - VALUES - (".$pdo->quote($entity->id).", ".$pdo->quote($this->entityName).", ".$pdo->quote($emailAddress->id).", 1) - "; - - $sth = $pdo->prepare($query); - $sth->execute(); - } } } else { - $fetched = $entity->getFetchedValue('emailAddress'); - if (!empty($fetched)) { - $query = " - DELETE FROM entity_email_address - WHERE - entity_id = ".$pdo->quote($entity->id)." AND - entity_type = ".$pdo->quote($this->entityName)." AND - primary = 1 - "; - $sth = $pdo->prepare($query); - $sth->execute(); + $emailOld = $entity->getFetchedValue('emailAddress'); + if (!empty($emailOld)) { + $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); + $this->unrelate($entity, 'emailAddresses', $emailAddressOld); } } } diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index e62910c525..8a0d13706a 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -262,7 +262,27 @@ abstract class Mapper implements IMapper if (!$totalCount) { $selectPart = $this->getSelect($relEntity); $joinsPart = $this->getBelongsToJoins($relEntity); - $orderPart = $this->getOrder($relEntity, $orderBy, $order); + $orderPart = $this->getOrder($relEntity, $orderBy, $order); + + if (!empty($joins) && is_array($joins)) { + $joinsRelated = $this->getJoins($relEntity, $joins, false, $joinConditions); + if (!empty($joinsRelated)) { + if (!empty($joinsPart)) { + $joinsPart .= ' '; + } + $joinsPart .= $joinsRelated; + } + } + + if (!empty($leftJoins) && is_array($leftJoins)) { + $joinsRelated = $this->getJoins($relEntity, $leftJoins, true, $joinConditions); + if (!empty($joinsRelated)) { + if (!empty($joinsPart)) { + $joinsPart .= ' '; + } + $joinsPart .= $joinsRelated; + } + } } else { $selectPart = $this->getAggregationSelect($relEntity, 'COUNT', 'id'); @@ -322,7 +342,7 @@ abstract class Mapper implements IMapper $dataArr = array(); $sql = $this->composeSelectQuery($this->toDb($relEntity->getEntityName()), $selectPart, $joinsPart, $wherePart, $orderPart, $offset, $limit); - + $ps = $this->pdo->query($sql); if ($ps) { if (!$totalCount) { diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index f6ebb34060..4a89381d35 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -147,10 +147,10 @@ class Repository public function findRelated(Entity $entity, $relationName, array $params = array()) { - $this->handleSelectParams($params); - $dataArr = $this->mapper->selectRelated($entity, $relationName, $params); + $entityName = $entity->relations[$relationName]['entity']; + $this->handleSelectParams($params, $entityName); - $entityName = $entity->relations[$relationName]['entity']; + $dataArr = $this->mapper->selectRelated($entity, $relationName, $params); $seed = $this->entityFactory->create($entityName); $collection = new EntityCollection($dataArr, $seed, $this->entityFactory); @@ -159,7 +159,8 @@ class Repository public function countRelated(Entity $entity, $relationName, array $params = array()) { - $this->handleSelectParams($params); + $entityName = $entity->relations[$relationName]['entity']; + $this->handleSelectParams($params, $entityName); return $this->mapper->countRelated($entity, $relationName, $params); }