- 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
This commit is contained in:
parent
dc5e088551
commit
2210853093
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue