From 2210853093303b01de382bcb0ef7b7a07fd746ff Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 24 Nov 2021 16:22:28 +0000 Subject: [PATCH] - Added a missing return from newly created items in save() ; also added a count in saveAll() method allowing to know how many items were saved --- src/Common/PdoObject.php | 12 +++++++++--- src/Repository.php | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Common/PdoObject.php b/src/Common/PdoObject.php index 462c1a8..78ce731 100644 --- a/src/Common/PdoObject.php +++ b/src/Common/PdoObject.php @@ -9,6 +9,8 @@ class PdoObject extends PDO { public static ? string $dump = null; + public bool $executionStatus; + public function select(string $sql, array $parameters = []): PDOStatement { static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]); @@ -29,7 +31,7 @@ class PdoObject extends PDO { public function runQuery(string $sql, array $parameters = []): ? PDOStatement { static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]); - # \debogueur([ $sql, $parameters ]); + try { if (false !== ( $statement = $this->prepare($sql) )) { return $this->execute($statement, $parameters, true); @@ -59,12 +61,16 @@ class PdoObject extends PDO { public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true): ? PDOStatement { + $this->executionStatus = false; + try { if ( ! $this->inTransaction() ) { $this->beginTransaction(); } - if (empty($parameters) ? $statement->execute() : $statement->execute($parameters)) { + $this->executionStatus = empty($parameters) ? $statement->execute() : $statement->execute($parameters); + + if ( $this->executionStatus ) { $statement->lastInsertId = $this->lastInsertId(); if ( $commit ) { @@ -79,7 +85,7 @@ class PdoObject extends PDO { } catch (\PDOException $e) { $this->rollback(); - + throw $e; } catch (\Throwable $e) { diff --git a/src/Repository.php b/src/Repository.php index 5c6067f..1f6a655 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -56,14 +56,14 @@ class Repository return $this->collectionFromQuery(); } - public function loadAllFromField($field, $value) : EntityCollection + public function loadAllFromField($field, $value, $operator= Query\Where::OPERATOR_EQUAL) : EntityCollection { - return $this->loadFromField($field, $value); + return $this->loadFromField($field, $value, $operator); } - public function loadFromField($field, $value) : EntityCollection + public function loadFromField($field, $value, $operator= Query\Where::OPERATOR_EQUAL) : EntityCollection { - return $this->where($field, $value)->collectionFromQuery(); + return $this->where($field, $value, $operator)->collectionFromQuery(); } public function count() : int @@ -145,6 +145,8 @@ class Repository $primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); if ( ! $entity->isLoaded() ) { + # $dataset = array_filter($dataset, fn($item, $field) => ! ($this->entityResolver->searchFieldAnnotation($field, new Field, false)->readonly ?? false), \ARRAY_FILTER_USE_BOTH); + $statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runInsertQuery(); if ( ( 0 !== $statement->lastInsertId ) && @@ -155,6 +157,8 @@ class Repository } $entity->entityFillFromDataset($dataset, true); + + return (bool) $statement->lastInsertId; } else { if ( $primaryKeyDefinition === null ) { @@ -184,11 +188,15 @@ class Repository return $this->save($entity, $fieldsAndValue, true); } - public function saveAll(EntityCollection $collection) : void + public function saveAll(EntityCollection $collection) : int { + $changed = 0; + foreach($collection as $entity) { - $this->save($entity); + $this->save($entity) && $changed++; } + + return $changed; } public function replaceAll(EntityCollection $collection) : void