PHP Method Documentation: Create
This document provides comprehensive details on the create
method, which is designed for inserting new records into the database. It includes support for handling related records, dynamic data validation, transactions to ensure data integrity, and the flexibility to shape the response with 'select', 'include', and a 'format' parameter.
Purpose
The purpose of the 'create' method is to insert a new record into the 'Users' table, offering enhanced capabilities for including related entities, applying data validation according to field types, and optionally returning the record in various formats. This method guarantees data integrity through transactions and supports complex operations such as connecting or creating related records.
Parameters
array $data
- An associative array containing the data for the new User record. This must include a 'data' key with an associative array as its value. The array may also contain 'select' and 'include' keys for response shaping, and a 'format' key to specify the return type ('array' or 'object').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
The method returns the created User record, either as an associative array or an object, based on the specified 'format'. If 'select' or 'include' parameters are used, the response is shaped accordingly. Related entities can be included if specified.
Exception Handling
An exception is thrown if the 'data' key is not provided or is not an associative array, if both 'include' and 'select' keys are used simultaneously, or if any error occurs during the database operation. Transactions are rolled back to maintain data consistency.
Example Usage
Creating a New User Record
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123'
]
]);
echo "<pre>";
echo "New user created: " . print_r($newUser, true);
echo "</pre>";
Creating a User with Related Profile using the create
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'profile' => [
'create' => [
'bio' => 'Software Developer',
]
]
]
]);
echo "<pre>";
echo "New user created with profile: " . print_r($newUser, true);
echo "</pre>";
Creating a User with Multiple Related Posts using the createMany
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'posts' => [
'createMany' => [
['title' => 'My First Post', 'content' => 'This is the content of my first post.'],
['title' => 'My Second Post', 'content' => 'This is the content of my second post.']
]
]
]
]);
echo "<pre>";
echo "New user created with posts: " . print_r($newUser, true);
echo "</pre>";
Creating a User with Roles using connectOrCreate
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'roles' => [
'connectOrCreate' => [
'where' => ['name' => 'Admin'],
'create' => ['name' => 'Admin'],
],
],
]
]);
echo "<pre>";
echo "New user created with roles: " . print_r($newUser, true);
echo "</pre>";
Creating a User with Existing Roles using connect
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'roles' => [
'connect' => [
'name' => 'Admin'
],
],
]
]);
echo "<pre>";
echo "New user created: " . print_r($newUser, true);
echo "</pre>";
Shaping the Response with include
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'profile' => [
'create' => [
'bio' => 'Software Developer',
]
],
'roles' => [
'connectOrCreate' => [
'where' => ['name' => 'Admin'],
'create' => ['name' => 'Admin'],
],
],
],
'include' => [
'profile' => true,
'roles' => true
]
]);
echo "<pre>";
echo "New user created: " . print_r($newUser, true);
echo "</pre>";
Shaping the Response with select
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'profile' => [
'create' => [
'bio' => 'Software Developer',
]
],
'roles' => [
'connectOrCreate' => [
'where' => ['name' => 'Admin'],
'create' => ['name' => 'Admin'],
],
],
'post' => [
'createMany' => [
[
'title' => 'Hello World',
'content' => 'This is my first post!',
],
[
'title' => 'Second Post',
'content' => 'This is my second post!',
],
],
],
],
'select' => [
'id' => true,
'name' => true,
'email' => true,
'roles' => [
'select' => [
'id' => true,
'name' => true,
],
],
'profile' => [
'select' => [
'id' => true,
'bio' => true,
],
],
'post' => [
'select' => [
'id' => true,
'title' => true,
'content' => true,
],
],
]
]);
echo "<pre>";
echo "New user created: " . print_r($newUser, true);
echo "</pre>";