diff --git a/src/app/entity/controllers/EntityController.php b/src/app/entity/controllers/EntityController.php index 2158177fe7d3474b16eda406ce9cb334c2eba8fe..8a2f70556f0a21ad9fd4549060fea54fe03799d5 100755 --- a/src/app/entity/controllers/EntityController.php +++ b/src/app/entity/controllers/EntityController.php @@ -180,7 +180,7 @@ class EntityController $entity['users'] = EntityModel::getUsersById(['id' => $entity['entity_id'], 'select' => ['users.id','users.user_id', 'users.firstname', 'users.lastname', 'users.status']]); $children = EntityModel::get(['select' => [1], 'where' => ['parent_entity_id = ?'], 'data' => [$args['id']]]); - $entity['contact'] = $this->getContactLinkCount($entity['id']); + $entity['contact'] = EntityController::getContactLinkCount($entity['id']); $entity['hasChildren'] = count($children) > 0; $documents = ResModel::get(['select' => [1], 'where' => ['destination = ?'], 'data' => [$args['id']]]); $entity['documents'] = count($documents); @@ -196,12 +196,6 @@ class EntityController return $response->withJson(['entity' => $entity]); } - public function getContactLinkCount(int $id) - { - $linkCount = count(ResourceContactModel::get(['select' => ['distinct res_id'], 'where' => ['item_id = ?', 'type = ?'], 'data' => [$id, 'entity']])); - return $linkCount; - } - public function create(Request $request, Response $response) { if (!PrivilegeController::hasPrivilege(['privilegeId' => 'manage_entities', 'userId' => $GLOBALS['id']])) { @@ -540,7 +534,9 @@ class EntityController ]); //ResourceContact $dyingConnection = ResourceContactModel::get(['select' => ['id', 'res_id', 'item_id', 'mode'], 'where' => ['type = ?', 'item_id = ?'], 'data' => ['entity', $dyingEntity['id']]]); - $successorConnection = ResourceContactModel::get(['select' => ['id', 'res_id', 'item_id', 'mode'], 'where' => ['type = ?', 'item_id = ?', 'res_id in (?)'], 'data' => ['entity', $successorEntity['id'], array_uniq(array_column($dyingConnection, 'res_id'))]]); + if(!empty($dyingConnection)) { + $successorConnection = ResourceContactModel::get(['select' => ['id', 'res_id', 'item_id', 'mode'], 'where' => ['type = ?', 'item_id = ?', 'res_id in (?)'], 'data' => ['entity', $successorEntity['id'], array_uniq(array_column($dyingConnection, 'res_id'))]]); + } $dyingIds = array_column($dyingConnection, 'id'); $idsToDelete = []; foreach ($dyingConnection as $dyingConn) { @@ -550,8 +546,13 @@ class EntityController } } } - ResourceContactModel::delete(['where' => ['id in (?)'], 'data' => [$idsToDelete]]); - ResourceContactModel::update(['set' => ['item_id' => $successorEntity['id']], 'where' => ['id in (?)'], 'data' => [$dyingIds]]); + if(!empty($idsToDelete)) { + ResourceContactModel::delete(['where' => ['id in (?)'], 'data' => [$idsToDelete]]); + } + if(!empty($dyingIds)) { + ResourceContactModel::update(['set' => ['item_id' => $successorEntity['id']], 'where' => ['id in (?)'], 'data' => [$dyingIds]]); + } + EntityModel::delete(['where' => ['entity_id = ?'], 'data' => [$aArgs['id']]]); HistoryController::add([ @@ -849,4 +850,10 @@ class EntityController return $response->withHeader('Content-Type', $contentType); } + + public static function getContactLinkCount(int $id) + { + $linkCount = count(ResourceContactModel::get(['select' => ['distinct res_id'], 'where' => ['item_id = ?', 'type = ?'], 'data' => [$id, 'entity']])); + return $linkCount; + } }