getRouteParam('akteId'); $documentId = $request->getRouteParam('documentId'); $data = $request->getParsedBody(); if (!$akteId || !$documentId) { throw new BadRequest('Akte ID and Document ID are required'); } // Verify entities exist $akte = $this->entityManager->getEntityById('CAdvowareAkten', $akteId); if (!$akte) { throw new NotFound('Akte not found'); } $document = $this->entityManager->getEntityById('CDokumente', $documentId); if (!$document) { throw new NotFound('Document not found'); } // Check if already linked if ($this->checkIfLinked($akteId, $documentId)) { // Already linked, just update junction columns return $this->updateExisting($akteId, $documentId, $data); } // Create relationship via ORM (triggers hooks) $this->entityManager ->getRDBRepository('CAdvowareAkten') ->getRelation($akte, 'dokumentes') ->relate($document); // Then update junction columns via SQL (no hooks) return $this->updateExisting($akteId, $documentId, $data); } private function checkIfLinked(string $akteId, string $documentId): bool { $pdo = $this->entityManager->getPDO(); $sql = " SELECT COUNT(*) as count FROM c_advoware_akten_dokumente WHERE c_advoware_akten_id = :akteId AND c_dokumente_id = :documentId AND deleted = 0 "; $sth = $pdo->prepare($sql); $sth->execute([ 'akteId' => $akteId, 'documentId' => $documentId ]); $result = $sth->fetch(\PDO::FETCH_ASSOC); return $result['count'] > 0; } private function updateExisting(string $akteId, string $documentId, object $data): Response { $pdo = $this->entityManager->getPDO(); // Build dynamic UPDATE $setClauses = []; $params = [ 'akteId' => $akteId, 'documentId' => $documentId ]; if (isset($data->hnr)) { if (!is_numeric($data->hnr)) { throw new BadRequest('hnr must be a number'); } $setClauses[] = "hnr = :hnr"; $params['hnr'] = (int)$data->hnr; } if (isset($data->syncstatus)) { $allowedStatuses = ['new', 'unclean', 'synced', 'failed', 'unsupported']; if (!in_array($data->syncstatus, $allowedStatuses)) { throw new BadRequest('Invalid syncstatus. Allowed: ' . implode(', ', $allowedStatuses)); } $setClauses[] = "syncstatus = :syncstatus"; $params['syncstatus'] = $data->syncstatus; } if (isset($data->lastSync)) { $setClauses[] = "last_sync = :lastSync"; $params['lastSync'] = $data->lastSync; } elseif (isset($data->updateLastSync) && $data->updateLastSync === true) { $setClauses[] = "last_sync = NOW()"; } if (!empty($setClauses)) { $sql = " UPDATE c_advoware_akten_dokumente SET " . implode(', ', $setClauses) . " WHERE c_advoware_akten_id = :akteId AND c_dokumente_id = :documentId AND deleted = 0 "; $sth = $pdo->prepare($sql); $sth->execute($params); } // Return final junction entry $sql = " SELECT id as junctionId, c_advoware_akten_id as cAdvowareAktenId, c_dokumente_id as cDokumenteId, hnr, syncstatus, last_sync as lastSync FROM c_advoware_akten_dokumente WHERE c_advoware_akten_id = :akteId AND c_dokumente_id = :documentId AND deleted = 0 "; $sth = $pdo->prepare($sql); $sth->execute([ 'akteId' => $akteId, 'documentId' => $documentId ]); $result = $sth->fetch(\PDO::FETCH_ASSOC); if (!$result) { throw new NotFound('Junction entry not found after creation'); } return ResponseComposer::json($result); } }