SaveTrait
Tags
Table of Contents
Methods
- findRelatedIds() : array<int, int>|null
- Returns ids of target entities linked to the parent through the relation collection `$property`.
- save() : Result
- updateAllRelations() : Result
- Updates all relations of parent entity with given scalar values.
- updateColumns() : Result
- Updates columns of the given rows and fully replaces to-many relations, all in one transaction.
- updateRelations() : Result
- Updates existing relation entities in a join/relation table.
- doUpdateRelations() : Result
- insertRelations() : Result
- Inserts relation entities into a join/relation table.
- rollBackOwnedLevel() : Throwable|null
- Rolls back the transaction levels opened by this mapper (level above the entry level), never foreign ones.
- updateColumnsUnguarded() : Result
Methods
findRelatedIds()
Returns ids of target entities linked to the parent through the relation collection `$property`.
public
findRelatedIds(int $parentId, string $property, string $targetAssociation) : array<int, int>|null
For example ('assetRels', 'asset') returns the asset ids of a record gallery. Table and
columns come from Doctrine metadata (same as self::updateAllRelations()).
Parameters
- $parentId : int
- $property : string
- $targetAssociation : string
Return values
array<int, int>|null —null when metadata or the database failed
save()
public
save(TEntity $entity[, array<int, string> $skipFields = [] ]) : Result
Parameters
- $entity : TEntity
- $skipFields : array<int, string> = []
Return values
ResultupdateAllRelations()
Updates all relations of parent entity with given scalar values.
public
updateAllRelations(array<string|int, mixed> $parentEntityIds, string $property, array<string, mixed> $values) : Result
Sets values on every row matching the parent FK.
Parameters
- $parentEntityIds : array<string|int, mixed>
-
Parent entity IDs (scalar or composite-id arrays)
- $property : string
- $values : array<string, mixed>
-
relation property name => value
Return values
ResultupdateColumns()
Updates columns of the given rows and fully replaces to-many relations, all in one transaction.
public
updateColumns(array<string|int, mixed> $entityIds, array<string, mixed> $values) : Result
A collection (one-to-many) is written as a full replace: delete of all parent rows plus
per-row inserts. A failure halfway would leave the parent with part of its history or none
(price snapshots of product sets), so a failed Result or an exception rolls everything
back. A nested call inside a foreign transaction is handled by DBAL with a savepoint.
IMPORTANT: the mapper owns only the transaction level it opened itself. DBAL lowers the
level in finally even after a failed commit or savepoint release, so a blind rollback in
catch would throw NoActiveTransaction and hide the original error, or inside a foreign
transaction it would roll back the caller's level. Rollback therefore runs only while the
level is above the entry level; its own failure is logged and never replaces the primary
error or Result (a failed Result with a failed rollback becomes DB_ERROR). An
ambiguous commit is not retried.
Parameters
- $entityIds : array<string|int, mixed>
-
ids or composite-id arrays (
[prop => value]) - $values : array<string, mixed>
-
property name => new value
Return values
ResultupdateRelations()
Updates existing relation entities in a join/relation table.
public
updateRelations(array<string|int, mixed> $parentEntityIds, string $property, array<string|int, object> $relationEntities) : Result
Parameters
- $parentEntityIds : array<string|int, mixed>
-
Parent entity IDs (scalar or composite-id arrays)
- $property : string
-
Association property name on parent entity
- $relationEntities : array<string|int, object>
-
Relation entities with updated values
Return values
ResultdoUpdateRelations()
private
doUpdateRelations(array<string|int, mixed> $parentEntityIds, AssociationMapping $mapping, array<string|int, object> $relationEntities) : Result
Parameters
- $parentEntityIds : array<string|int, mixed>
-
Parent entity IDs (scalar or composite-id arrays)
- $mapping : AssociationMapping
-
OneToMany/ManyToMany association mapping
- $relationEntities : array<string|int, object>
-
Relation entities with updated values
Return values
ResultinsertRelations()
Inserts relation entities into a join/relation table.
private
insertRelations(array<string|int, mixed> $parentEntityIds, AssociationMapping $mapping, array<int, object> $relationEntities) : Result
Parameters
- $parentEntityIds : array<string|int, mixed>
-
Parent entity IDs (scalar or composite-id arrays)
- $mapping : AssociationMapping
-
OneToMany/ManyToMany association mapping
- $relationEntities : array<int, object>
-
relation entities (validated in
updateColumnsUnguarded())
Return values
ResultrollBackOwnedLevel()
Rolls back the transaction levels opened by this mapper (level above the entry level), never foreign ones.
private
rollBackOwnedLevel(Connection $connection, int $entryLevel, Throwable|null $primary) : Throwable|null
Returns the rollback exception (logged together with $primary), or null on success.
Parameters
- $connection : Connection
- $entryLevel : int
- $primary : Throwable|null
Return values
Throwable|nullupdateColumnsUnguarded()
private
updateColumnsUnguarded(array<string|int, mixed> $entityIds, array<string, mixed> $values) : Result
Parameters
- $entityIds : array<string|int, mixed>
-
ids or composite-id arrays (
[prop => value]) - $values : array<string, mixed>
-
property name => new value