PHP Method Documentation: UpdateMany

This document elaborates on the updateMany method, which allows for the bulk updating of records in the database based on specified criteria. It covers the method's functionality for updating multiple records within a single transaction, ensuring data integrity and consistency. Additionally, this documentation discusses the method's support for dynamic data validation and handling of relational data.

Purpose

The 'updateMany' method is developed to perform bulk updates on records in the 'Users' table that meet given conditions. This facilitates efficient data manipulation operations across multiple records, ensuring atomicity and consistency through transactional control. It is especially useful for scenarios requiring simultaneous updates to a set of records, optimizing performance and reliability.

Parameters

  • array $data - An associative array with two keys: 'where', which defines the conditions for selecting records, and 'data', containing the new values for the update.

Return Value

Returns an associative array on success, including 'status', 'message', and the number of 'affectedRows'. Returns false if no record matches the update criteria.

Exception Handling

Throws an exception if essential keys are missing, if input arrays are improperly structured, or if any part of the database operation fails. Transactions are rolled back in the event of an exception to preserve database integrity.

Example Usage

Updating Multiple User Records

  use Lib\Prisma\Classes\Prisma;

    $prisma = Prisma::getInstance();
    $updatedRecords = $prisma->user->updateMany([
        'where' => ['status' => 'inactive'],
        'data' => ['status' => 'active']
    ]);

    echo "<pre>";
    echo "Records updated: " . print_r($updatedRecords, true);
    echo "</pre>";