- 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:
Dave Mc Nicoll committed 2021-11-24 16:22:28 +00:00
1 parent dc5e088551
commit 2210853093
2 files changed
+23 -9

No files matched your search

+14 -6
View File
@@ -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