- Added the replace keyword

This commit is contained in:
Dave Mc Nicoll committed 2021-08-02 15:13:17 +00:00
1 parent 7fb591e1f5
commit 0453e9cbc5
4 files changed
+45 -17

No files matched your search

+32 -9
View File
@@ -125,8 +125,12 @@ class Repository
}
}
public function save(object $entity, ? array $fieldsAndValue = null) : bool
public function save(/*object|array*/ $entity, ? array $fieldsAndValue = null, bool $replace = false) : bool
{
if ( is_array($entity) ) {
$entity = ( new $this->entityClass() )->fromArray($entity);
}
if ( ! $this->matchEntity($entity) ) {
throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
}
@@ -136,7 +140,7 @@ class Repository
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
if ( ! $entity->isLoaded() ) {
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset)->runQuery();
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runQuery();
if ( ( 0 !== $statement->lastInsertId ) &&
( null !== $primaryKeyDefinition )) {
@@ -170,6 +174,11 @@ class Repository
return false;
}
public function replace(/*object|array*/ $entity, ? array $fieldsAndValue = null) : bool
{
return $this->save($entity, $fieldsAndValue, true);
}
public function saveAll(EntityCollection $collection) : void
{
foreach($collection as $entity) {
@@ -177,6 +186,13 @@ class Repository
}
}
public function replaceAll(EntityCollection $collection) : void
{
foreach($collection as $entity) {
$this->replace($entity);
}
}
public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self
{
$schema = $schema ?: $this->entityResolver->schemaName();
@@ -195,9 +211,13 @@ class Repository
return $this->createSqlQuery()->runQuery();
}
public function generateDatasetDiff(object $entity) : array
public function generateDatasetDiff(object $entity, bool $oldValues = false) : array
{
return array_diff_assoc( array_change_key_case($entity->toArray()), array_change_key_case($entity->entityGetDataset(false, true)) );
$array = array_change_key_case($entity->toArray());
$dataset = array_change_key_case($entity->entityGetDataset(false, true));
return array_diff_assoc($oldValues ? $dataset : $array , $oldValues ? $array : $dataset );
}
public function yield() : \Generator
@@ -261,9 +281,9 @@ class Repository
return $this;
}
public function insert(array $fieldlist, string $table, string $alias, ? string $schema) : self
public function insert(array $fieldlist, string $table, string $alias, ? string $schema, bool $replace = false) : self
{
$this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema);
$this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema, $replace);
return $this;
}
@@ -604,10 +624,13 @@ class Repository
return $this;
}
protected function insertSqlQuery(array $dataset) : self
protected function insertSqlQuery(array $dataset, bool $replace = false) : self
{
if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) {
$this->insert(array_map([ $this, 'escapeField' ] , array_keys($dataset)), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
if ( null === $insert = $this->queryBuilder->getFragment(Query\Insert::class) ) {
$this->insert(array_map([ $this, 'escapeField' ] , array_keys($dataset)), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName(), $replace);
}
else {
$insert->replace = $replace;
}
$this->values($dataset);