191 lines
6.7 KiB
PHP
191 lines
6.7 KiB
PHP
<?php
|
||
/**
|
||
* Test für Many-to-Many-Beziehung mit additionalColumns
|
||
* Testet die Beziehung zwischen CDokumente und CAICollections mit syncId
|
||
*/
|
||
|
||
require_once 'bootstrap.php';
|
||
|
||
$app = new \Espo\Core\Application();
|
||
$container = $app->getContainer();
|
||
$entityManager = $container->getByClass(\Espo\ORM\EntityManager::class);
|
||
$recordServiceContainer = $container->getByClass(\Espo\Core\Record\ServiceContainer::class);
|
||
|
||
echo "\n" . str_repeat("=", 80) . "\n";
|
||
echo str_repeat(" ", 20) . "Many-to-Many Junction Test (Internal API)\n";
|
||
echo str_repeat("=", 80) . "\n\n";
|
||
|
||
$docId = null;
|
||
$collectionId = null;
|
||
$testSyncId = "TEST-SYNC-" . date('Ymd-His');
|
||
|
||
try {
|
||
// 1. CDokumente erstellen (ohne das required dokument-Feld - nur für Test)
|
||
echo "1️⃣ Erstelle Test-Dokument...\n";
|
||
|
||
// Direktes ORM-Entity erstellen (umgeht Validierung)
|
||
$doc = $entityManager->createEntity('CDokumente', [
|
||
'name' => 'Test-Dokument für Junction-Test ' . date('Y-m-d H:i:s'),
|
||
'description' => 'Test-Dokument für Many-to-Many mit syncId',
|
||
]);
|
||
|
||
if (!$doc) {
|
||
throw new Exception("Fehler beim Erstellen des Dokuments");
|
||
}
|
||
|
||
$docId = $doc->getId();
|
||
echo "✓ Dokument erstellt: {$docId}\n\n";
|
||
|
||
// 2. CAICollections erstellen
|
||
echo "2️⃣ Erstelle Test-Collection...\n";
|
||
$collection = $entityManager->createEntity('CAICollections', [
|
||
'name' => 'Test-Collection für Junction-Test ' . date('Y-m-d H:i:s'),
|
||
'description' => 'Test-Collection für Many-to-Many mit syncId',
|
||
]);
|
||
|
||
if (!$collection) {
|
||
throw new Exception("Fehler beim Erstellen der Collection");
|
||
}
|
||
|
||
$collectionId = $collection->getId();
|
||
echo "✓ Collection erstellt: {$collectionId}\n\n";
|
||
|
||
// 3. Beziehung erstellen mit syncId
|
||
echo "3️⃣ Verknüpfe Dokument mit Collection (mit syncId)...\n";
|
||
echo " syncId: {$testSyncId}\n";
|
||
|
||
$repository = $entityManager->getRDBRepository('CDokumente');
|
||
$relation = $repository->getRelation($doc, 'cAICollections');
|
||
|
||
// Verbinde mit additionalColumns
|
||
$relation->relateById($collectionId, [
|
||
'syncId' => $testSyncId
|
||
]);
|
||
|
||
echo "✓ Verknüpfung erstellt\n\n";
|
||
|
||
// 4. Beziehung wieder laden und prüfen
|
||
echo "4️⃣ Lade Beziehung und prüfe syncId...\n";
|
||
|
||
// Reload Dokument
|
||
$doc = $entityManager->getEntity('CDokumente', $docId);
|
||
|
||
// Lade die verknüpften Collections
|
||
$repository = $entityManager->getRDBRepository('CDokumente');
|
||
$relation = $repository->getRelation($doc, 'cAICollections');
|
||
|
||
// Hole alle verknüpften Collections mit Columns
|
||
$collections = $relation->find();
|
||
|
||
echo " Anzahl verknüpfter Collections: " . count($collections) . "\n";
|
||
|
||
$found = false;
|
||
foreach ($collections as $col) {
|
||
if ($col->getId() === $collectionId) {
|
||
echo " Collection gefunden: {$col->getId()}\n";
|
||
|
||
// Hole die Middle-Columns
|
||
$relationData = $relation->getColumnAttributes($col, ['syncId']);
|
||
|
||
echo "\n Relation-Daten:\n";
|
||
echo " " . str_repeat("-", 76) . "\n";
|
||
echo " " . json_encode($relationData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
|
||
echo " " . str_repeat("-", 76) . "\n\n";
|
||
|
||
if (isset($relationData['syncId'])) {
|
||
$returnedSyncId = $relationData['syncId'];
|
||
if ($returnedSyncId === $testSyncId) {
|
||
echo " ✅ syncId korrekt geladen: {$returnedSyncId}\n";
|
||
$found = true;
|
||
} else {
|
||
echo " ⚠️ syncId geladen, aber Wert stimmt nicht:\n";
|
||
echo " Erwartet: {$testSyncId}\n";
|
||
echo " Erhalten: {$returnedSyncId}\n";
|
||
}
|
||
} else {
|
||
echo " ❌ syncId ist NICHT in den Relation-Daten vorhanden\n";
|
||
echo " Verfügbare Felder: " . implode(', ', array_keys($relationData)) . "\n";
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!$found && count($collections) > 0) {
|
||
echo " ⚠️ Collection verknüpft, aber syncId-Prüfung fehlgeschlagen\n";
|
||
} elseif (!$found) {
|
||
echo " ❌ Keine verknüpfte Collection gefunden\n";
|
||
}
|
||
echo "\n";
|
||
|
||
// 5. Direkter Datenbank-Check
|
||
echo "5️⃣ Prüfe Datenbank direkt...\n";
|
||
$pdo = $entityManager->getPDO();
|
||
$stmt = $pdo->prepare(
|
||
"SELECT * FROM c_a_i_collection_c_dokumente
|
||
WHERE c_dokumente_id = :docId AND deleted = 0"
|
||
);
|
||
$stmt->execute(['docId' => $docId]);
|
||
$dbRows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
|
||
echo " Datenbank-Zeilen gefunden: " . count($dbRows) . "\n";
|
||
if (count($dbRows) > 0) {
|
||
echo "\n Datenbank-Inhalt:\n";
|
||
echo " " . str_repeat("-", 76) . "\n";
|
||
foreach ($dbRows as $row) {
|
||
echo " " . json_encode($row, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
|
||
}
|
||
echo " " . str_repeat("-", 76) . "\n";
|
||
|
||
foreach ($dbRows as $row) {
|
||
if ($row['c_a_i_collections_id'] === $collectionId) {
|
||
if (isset($row['sync_id']) && $row['sync_id'] === $testSyncId) {
|
||
echo " ✅ syncId in Datenbank korrekt: {$row['sync_id']}\n";
|
||
} else {
|
||
echo " ❌ syncId in Datenbank falsch oder fehlend\n";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
echo "\n";
|
||
|
||
$success = $found;
|
||
|
||
} catch (Exception $e) {
|
||
echo "\n❌ Fehler: " . $e->getMessage() . "\n";
|
||
echo " Stack Trace:\n";
|
||
echo " " . $e->getTraceAsString() . "\n\n";
|
||
$success = false;
|
||
} finally {
|
||
// Cleanup
|
||
echo "6️⃣ Räume Test-Daten auf...\n";
|
||
|
||
if ($docId) {
|
||
try {
|
||
$entityManager->removeEntity($entityManager->getEntity('CDokumente', $docId));
|
||
echo "✓ Dokument gelöscht: {$docId}\n";
|
||
} catch (Exception $e) {
|
||
echo "⚠️ Fehler beim Löschen des Dokuments: {$e->getMessage()}\n";
|
||
}
|
||
}
|
||
|
||
if ($collectionId) {
|
||
try {
|
||
$entityManager->removeEntity($entityManager->getEntity('CAICollections', $collectionId));
|
||
echo "✓ Collection gelöscht: {$collectionId}\n";
|
||
} catch (Exception $e) {
|
||
echo "⚠️ Fehler beim Löschen der Collection: {$e->getMessage()}\n";
|
||
}
|
||
}
|
||
|
||
echo "\n";
|
||
}
|
||
|
||
echo str_repeat("=", 80) . "\n";
|
||
if ($success) {
|
||
echo "✅ TEST ERFOLGREICH - Many-to-Many mit additionalColumns funktioniert!\n";
|
||
} else {
|
||
echo "❌ TEST FEHLGESCHLAGEN - syncId nicht korrekt verfügbar\n";
|
||
}
|
||
echo str_repeat("=", 80) . "\n\n";
|
||
|
||
exit($success ? 0 : 1);
|