Method Documentation: Connect Operator Overview

Overview

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.

Operators

create

Creates a new record with the provided data.

createMany

Creates multiple new records in a single operation, improving performance for bulk insert operations.

connect

Links an existing record to another by establishing a relationship between them, without altering the data itself.

connectOrCreate

Attempts to connect to an existing record. If the specified record does not exist, it creates a new record and then establishes the connection.

disconnect

Removes an existing relationship between records without deleting the records themselves.

update

Modifies the data of an existing record, identified by a unique identifier, supporting dynamic updates for specified fields.

updateMany

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.

Usage Examples

In this section, you can find usage examples for each operator, demonstrating how to apply them in different scenarios for effective data management.

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 Related Post and Tags 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',
                '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>";

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 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>";

Update User using the 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>";

Update User using the 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>";

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 using the 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>";

Update User using the 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>";