Skip to content

EspoCRM – Open Source CRM Application

ORM: Mapper relate return false if no row affected

Yuri Kuznetsovdbafd6ba4d13
diff --git a/application/Espo/ORM/Mapper/BaseMapper.php b/application/Espo/ORM/Mapper/BaseMapper.php
index 813e2f1b1b..661abc2414 100644
--- a/application/Espo/ORM/Mapper/BaseMapper.php
+++ b/application/Espo/ORM/Mapper/BaseMapper.php
@@ -996,7 +996,11 @@ class BaseMapper implements RDBMapper
->set($set)
->build();
- $this->queryExecutor->execute($query);
+ $sth = $this->queryExecutor->execute($query);
+
+ if ($sth->rowCount() === 0) {
+ return false;
+ }
return true;
@@ -1047,7 +1051,7 @@ class BaseMapper implements RDBMapper
// @todo Leave one INSERT for better performance.
- if ($sth->rowCount() == 0) {
+ if ($sth->rowCount() === 0) {
$values = $where;
$columns = array_keys($values);
@@ -1066,7 +1070,7 @@ class BaseMapper implements RDBMapper
->updateSet($update)
->build();
- $this->queryExecutor->execute($insertQuery);
+ $sth = $this->queryExecutor->execute($insertQuery);
return true;
}
@@ -1083,7 +1087,11 @@ class BaseMapper implements RDBMapper
->set($update)
->build();
- $this->queryExecutor->execute($updateQuery);
+ $sth = $this->queryExecutor->execute($updateQuery);
+
+ if ($sth->rowCount() === 0) {
+ return false;
+ }
return true;
}
diff --git a/application/Espo/ORM/Mapper/RDBMapper.php b/application/Espo/ORM/Mapper/RDBMapper.php
index 2e0f373a78..69665f386a 100644
--- a/application/Espo/ORM/Mapper/RDBMapper.php
+++ b/application/Espo/ORM/Mapper/RDBMapper.php
@@ -38,24 +38,39 @@ interface RDBMapper extends Mapper
/**
* Relate an entity with another entity.
*
- * @param array<string, mixed>|null $columnData
+ * @param Entity $entity An entity.
+ * @param string $relationName A relation name.
+ * @param Entity $foreignEntity A foreign entity.
+ * @param array<string, mixed>|null $columnData Column values.
+ * @return bool True if the row was affected.
*/
public function relate(Entity $entity, string $relationName, Entity $foreignEntity, ?array $columnData): bool;
/**
* Unrelate an entity from another entity.
+ *
+ * @param Entity $entity An entity.
+ * @param string $relationName A relation name.
+ * @param Entity $foreignEntity A foreign entity.
*/
public function unrelate(Entity $entity, string $relationName, Entity $foreignEntity): void;
/**
- * Unrelate an entity from another entity by a given ID.
+ * Relate an entity from another entity by a given ID.
*
- * @param array<string, mixed>|null $columnData
+ * @param Entity $entity An entity.
+ * @param string $relationName A relation name.
+ * @param string $id A foreign ID.
+ * @param array<string, mixed>|null $columnData Column values.
*/
public function relateById(Entity $entity, string $relationName, string $id, ?array $columnData = null): bool;
/**
* Unrelate an entity from another entity by a given ID.
+ *
+ * @param Entity $entity An entity.
+ * @param string $relationName A relation name.
+ * @param string $id A foreign ID.
*/
public function unrelateById(Entity $entity, string $relationName, string $id): void;
diff --git a/application/Espo/Tools/Stream/Service.php b/application/Espo/Tools/Stream/Service.php
index 3e488e2f35..79d215bc2f 100644
--- a/application/Espo/Tools/Stream/Service.php
+++ b/application/Espo/Tools/Stream/Service.php
@@ -768,24 +768,6 @@ class Service
*/
public function noteRelate(Entity $entity, Entity $parent, array $options = []): void
{
- $entityType = $entity->getEntityType();
-
- $existing = $this->entityManager
- ->getRDBRepository(Note::ENTITY_TYPE)
- ->select([Attribute::ID])
- ->where([
- 'type' => Note::TYPE_RELATE,
- 'parentId' => $parent->getId(),
- 'parentType' => $parent->getEntityType(),
- 'relatedId' => $entity->getId(),
- 'relatedType' => $entityType,
- ])
- ->findOne();
-
- if ($existing) {
- return;
- }
-
$note = $this->getNewNote();
$note->setType(Note::TYPE_RELATE);
@@ -814,24 +796,6 @@ class Service
*/
public function noteUnrelate(Entity $entity, Entity $parent, array $options = []): void
{
- $entityType = $entity->getEntityType();
-
- $existing = $this->entityManager
- ->getRDBRepository(Note::ENTITY_TYPE)
- ->select([Attribute::ID])
- ->where([
- 'type' => Note::TYPE_UNRELATE,
- 'parentId' => $parent->getId(),
- 'parentType' => $parent->getEntityType(),
- 'relatedId' => $entity->getId(),
- 'relatedType' => $entityType,
- ])
- ->findOne();
-
- if ($existing) {
- return;
- }
-
$note = $this->getNewNote();
$note->setType(Note::TYPE_UNRELATE);