create
Creates a new record with the provided data.
This section outlines the usage and functionality of various operators available for manipulating data within the database. These operators include 'create', 'createMany', 'connect', 'connectOrCreate', 'disconnect', 'update', and 'updateMany'. Each operator serves a unique purpose, from creating new records to updating or linking existing ones, providing a robust toolkit for data management.
Creates a new record with the provided data.
Creates multiple new records in a single operation, improving performance for bulk insert operations.
Links an existing record to another by establishing a relationship between them, without altering the data itself.
Attempts to connect to an existing record. If the specified record does not exist, it creates a new record and then establishes the connection.
Removes an existing relationship between records without deleting the records themselves.
Modifies the data of an existing record, identified by a unique identifier, supporting dynamic updates for specified fields.
Updates multiple records that meet the specified criteria, allowing for bulk data modifications.
NOTE: The difference between the create
and createMany
operators is that when you pass an array of data in the create
operator, you have the ability to create a record related to the current record. However, the createMany
operator does not have this ability.
In this section, you can find usage examples for each operator, demonstrating how to apply them in different scenarios for effective data management.
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>";
create
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
'data' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => 'password123',
'post' => [
'create' => [
[
'title' => 'My First Post',
'content' => 'This is the content of my first post.',
'categories' => [
'name' => 'PHP Frameworks',
'tags' => [
'create' => [
['name' => 'PHP'],
['name' => 'Laravel']
]
]
]
],
[
'title' => 'My Second Post',
'content' => 'This is the content of my second post.',
'categories' => [
'name' => 'JavaScript Frameworks',
'tags' => [
'create' => [
['name' => 'JavaScript'],
['name' => 'React']
]
]
]
]
]
]
]
]);
echo "<pre>";
echo "New user created with profile: " . print_r($newUser, true);
echo "</pre>";
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>";
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>";
disconnect
When is Explicit Relation 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>";
disconnect
When an Implicit Relation the ID must be declared use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'new.email@example.com',
'userRole' => [
'disconnect' => ['idRole' => 2]
]
]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
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
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'updated.email@example.com',
'name' => 'John Doe',
'profile' => [
'update' => ['bio' => 'Senior Software Developer']
]
]
]);
echo "<pre>";
echo "Updated User: " . print_r($updatedUser, true);
echo "</pre>";
updateMany
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$updatedUsers = $prisma->user->update([
'where' => ['id' => 'someUserId'],
'data' => [
'email' => 'updated.email@example.com',
'name' => 'John Doe',
'post' => [
'updateMany' => [
['title' => 'My First Post', 'content' => 'This is the updated content of my first post.'],
['title' => 'My Second Post', 'content' => 'This is the updated content of my second post.']
]
]
]
]);
echo "<pre>";
echo "Updated Users: " . print_r($updatedUsers, true);
echo "</pre>";