Method Documentation: update
Purpose
The update
method is designed to modify a specific record in the 'Users' table, identified by a unique identifier. It supports dynamic updates for specified fields within a transactional context, ensuring data integrity and consistency.
Parameters
array $data
- An associative array containing the update criteria and data. This includes keys for 'where' (filter criteria to identify the User to update), 'data' (the data to update in the User record), 'select' (optionally, specifies a subset of fields to return), and 'include' (optionally, specifies related models to include in the result).bool $format = false
- Optional. Specifies the format of the returned data ('false' or 'true') by default is array, but can also be set to 'true' for object format.
Return Value
Returns the updated User record as an associative array or object, depending on the 'format' parameter. If relations are specified, they are processed according to the provided instructions (connect, disconnect, etc.).
Error Handling
Throws an exception if both 'include' and 'select' are used simultaneously, or in case of any error during the update process, ensuring robust error management within applications.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => ['email' => 'new.email@example.com']
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
Update User using the disconnect
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'new.email@example.com',
'userRole' => [
'disconnect' => true // Or 'disconnect' => ['name' => 'Admin']
]
]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
Update User using the connectOrCreate
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'userRole' => [
'connectOrCreate' => [
'where' => ['name' => 'Admin'],
'create' => ['name' => 'Admin'
]
]
]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
Update User: Connecting Roles, Updating Products & Posts
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'new.email@example.com',
'userRole' => [
'connect' => ['name' => 'Admin']
],
'product' => [
'update' => [
'name' => 'New Product',
'description' => 'New Product Description',
'quantity' => 10,
'price' => 100,
'store' => 10
]
],
'post' => [
'updateMany' => [
'where' => ['authorId' => 'zbDD5936Ox5z2hwCo5-lU'],
'data' => [
'title' => 'New Post Title 3',
'content' => 'New Post Content 3',
'published' => true
]
]
],
]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
Shaping the Response with include
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'new.email@example.com',
'userRole' => [
'connect' => [
'name' => 'Admin'
]
]
],
'include' => ['userRole' => true]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
Shaping the Response with select
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'new.email@example.com',
'userRole' => [
'connectOrCreate' => [
'where' => ['name' => 'Admin'],
'create' => ['name' => 'Admin']
]
]
],
'select' => ['id' => true, 'name' => true, 'email' => true, 'userRole' => true]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";