Add QBMapper::insertOrUpdate()
This allows elegant upserts where the entity ID is provided (e.g. by an external system) and when that data is fed into our database multiple times. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
parent
7526971c95
commit
40fdff5b80
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCP\AppFramework\Db;
|
||||
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
|
@ -123,7 +124,23 @@ abstract class QBMapper {
|
|||
return $entity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tries to creates a new entry in the db from an entity and
|
||||
* updates an existing entry if duplicate keys are detected
|
||||
* by the database
|
||||
*
|
||||
* @param Entity $entity the entity that should be created/updated
|
||||
* @return Entity the saved entity with the (new) id
|
||||
* @since 15.0.0
|
||||
* @suppress SqlInjectionChecker
|
||||
*/
|
||||
public function insertOrUpdate(Entity $entity): Entity {
|
||||
try {
|
||||
return $this->insert($entity);
|
||||
} catch (UniqueConstraintViolationException $ex) {
|
||||
return $this->update($entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an entry in the db from an entity
|
||||
|
|
Loading…
Reference in New Issue