diff --git a/CHANGELOG.md b/CHANGELOG.md index f1ca4fe6e431c4dbf4f191bc58b6c35544bae6a4..c9c4db114c57f8a4ea0faea7dc046af77a01c9ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ------------------ - [Fix] Pour Oracle, la recherche des séquences des tables fonctionne pour les tables & séquences de plus de 30 caractères +- Ajout de la méthode Table->getLastInsertId permettant de retourner la dernière valeur de séquence utilisée pour peupler un ID lors d'un INSERT +- Nouvelle option "return-insert-data" pour Table->merge, permettant de retourner dans "insert-data" les données insérées pour pouvoir récupérer les ID peuplés à partir des séquences 0.9.7 (19/07/2024) diff --git a/src/Table.php b/src/Table.php index 36cc9e0d81555f7ab68d42e893c9002f7cf370c1..9b339a563da11ca8e80a2cd381a8972b1bb13e73 100644 --- a/src/Table.php +++ b/src/Table.php @@ -25,6 +25,8 @@ class Table */ private $transformCache = []; + private ?int $lastInsertId = null; + /** @@ -295,6 +297,13 @@ class Table + public function getLastInsertId(): ?int + { + return $this->lastInsertId; + } + + + /** * @param array $data * @param array $options @@ -309,7 +318,8 @@ class Table $bdd = $this->getBdd(); if (!isset($data['ID']) && $this->hasId() && $this->hasSequence()) { - $data['ID'] = $this->getBdd()->sequence()->nextVal($this->ddl['sequence']); + $this->lastInsertId = $this->getBdd()->sequence()->nextVal($this->ddl['sequence']); + $data['ID'] = $this->lastInsertId; } $histoUserId = (int)$bdd->getOption('histo-user-id'); @@ -424,12 +434,17 @@ class Table 'soft-delete' => false, 'insert' => true, 'update' => true, + 'return-insert-data' => false, 'update-cols' => [], 'update-ignore-cols' => [], 'update-only-null' => [], ]; $options = array_merge($defaultOptions, $options); + if ($options['return-insert-data']){ + $result['insert-data'] = []; + } + $ddl = $this->getDdl(); $bdd = $this->getBdd(); @@ -519,6 +534,9 @@ class Table if ($options['insert']) { $this->insert($n); $result['insert']++; + if ($options['return-insert-data']){ + $result['insert-data'][] = $n; + } } }