updateManyAndReturn

Documentation for the updateManyAndReturn method, which updates multiple records matching the specified criteria and returns the updated entries using optional filters.

Purpose

The updateManyAndReturn method allows updating multiple records that match a given filter. After the update, it returns the updated records, filtered through optional select, include, or omit fields.

Parameters

  • 'where' - An associative array of conditions to filter the records to be updated (required).
  • 'data' - An associative array of field-value pairs to update (required).
  • 'select' - An array specifying which fields to return (optional).
  • 'include' - An array of related models to include in the result (optional).
  • 'omit' - An array of field names to exclude from the result (optional).

Return Value

Returns an array of updated model objects matching the where conditions. The result can be customized using select, include, or omit. If no records match the criteria, an empty array is returned.

Exception Handling

  • Throws an Exception if 'where' or 'data' is missing or not an array.
  • Throws a LogicException if both 'include' and 'select' are provided simultaneously.
  • Throws an InvalidArgumentException if invalid enum values are passed.
  • Throws an Exception if no valid scalar fields are present in the 'data' array.

Features

  • Performs bulk update operations based on complex where conditions.
  • Returns updated records using dynamic field selection and relation inclusion.
  • Automatically sets updated timestamps for fields marked with isUpdatedAt.
  • Supports enum validation and data coercion per field type.
  • Transaction-safe operation: changes are rolled back on failure.

Example Usage

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();

$users = $prisma->user->updateManyAndReturn([
    'where' => [
        'role' => ['equals' => 'User']
    ],
    'data' => [
        'status' => 'active'
    ],
    'select' => [
        'id' => true,
        'name' => true,
        'status' => true
    ]
]);

echo "<pre>"; print_r($users); echo "</pre>";

Behavior When No Records Match

If no records match the where criteria, the method commits immediately and returns an empty array:

$users = $prisma->user->updateManyAndReturn([
    'where' => [
        'status' => ['equals' => 'non-existent']
    ],
    'data' => [
        'status' => 'archived'
    ]
]);

// Output: []
print_r($users);