Skip to content

Update builder

Use UpdateBuilder for set-based updates.

use Small\SwooleEntityManager\QueryBuilder\RelationalQueryBuilder\Enum\ConditionOperatorType;

$update = $userManager->createUpdateBuilder('user');
$update
    ->addFieldToUpdate('enabled', false)
    ->where()
    ->firstCondition(
        $update->getFieldForCondition('lastLoginAt'),
        ConditionOperatorType::inferior,
        ':threshold',
    );
$update->setParameter('threshold', new \DateTimeImmutable('-1 year')->format('Y-m-d H:i:s'));

$userManager->executeUpdate($update);

addFieldToUpdate() accepts an entity field name. Conditions and parameters use the same API as the select builder.

Lifecycle callbacks

By default, executeUpdate($builder) performs one native bulk statement and skips entity callbacks.

$userManager->executeUpdate($update, skipTriggers: false);

When trigger execution is requested and the entity implements update or persist lifecycle interfaces, the manager first selects matching entities and persists them one by one. This is slower but executes callbacks.

Unsupported operations

Update builders do not support joins, ordering, offsets or limits. Calling those operations throws LogicException. Convert the update to a select query when you need to inspect the matching rows:

$matchingUsers = $userManager->getResult($update->createQueryBuilder());

Planned Core 3.2: atomic expressions

The Core 3.2 development API adds typed expressions to bulk updates, such as setExpression('revision', Expr::add(Expr::field('revision'), Expr::value(1))). The SQL adapters validate mapped column names and parameterize all scalar operands. See typed atomic expressions for examples, supported operators, and compatibility notes. Core 3.1.0 does not have this API.

Next chapter: Delete builder