Method Documentation: delete
Purpose
This method facilitates the deletion of a User record from the database based on specific criteria outlined in an associative array. It verifies the existence of the User prior to deletion and can return the data of the deleted User. The method is designed for precise deletion operations by requiring conditions that uniquely identify the User, thus maintaining data integrity and consistency within the database.
Parameters
array $criteria
- An associative array containing the filter criteria to locate the User record for deletion. The array must include awhere
key that uniquely identifies the User. Optionalselect
orinclude
keys can be provided to specify data returned upon deletion.string $format
(optional) - Specifies the return data format, eitherarray
for an associative array orobject
for a standard PHP object. Default isarray
.
Return Value
On successful deletion, it returns the deleted User's data in the specified $format
. The return type is mixed, based on $format
, either an array or an object. If deletion fails due to non-existence or non-unique criteria, it returns an array with 'modelName'
and 'cause'
keys, indicating the failure reason.
Error Handling
Throws an Exception if the where
key is missing or not an associative array in $criteria
. Also, throws an Exception if both include
and select
keys are present, as they cannot be used simultaneously. Additionally, throws an Exception if there's an error during the deletion process or if the transaction fails, indicating the error nature for debugging.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";
Example Using include
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
'include' => ['posts' => true]
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";
Example Using select
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$deletedUser = $prisma->user->delete([
'where' => ['id' => 'someUserId'],
'select' => ['name' => true, 'email' => true, 'posts' => true]
]);
echo "<pre>";
echo "Deleted User: " . print_r($deletedUser, true);
echo "</pre>";