PHP code example of 6dreams / doctrine-bulk

1. Go to this page and download the library: Download 6dreams/doctrine-bulk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

6dreams / doctrine-bulk example snippets



declare(strict_types = 1);

use \Doctrine\ORM\EntityManagerInterface;
use \SixDreams\Bulk\BulkInsert;
use \SixDreams\Bulk\BulkUpdate;

/**
 * Class DbWrite
 */
class DbWrite {
    /** @var EntityManagerInterface */
    private $manager;
    
    /**
     * Creates two users in one query.
     *
     * @return int
     */
    public function createTwoUsers(): int
    {
        $firstInsertedId = (int) (new BulkInsert($this->manager, User::class))
            ->addEntity(new User('user 1', 'password'))
            ->addEntity(new User('user 2', 'password'))
            ->execute();
        
        return $firstInsertedId;
    }
    
    /**
     * Updates two users in database.
     */
    public function updateTwoUsers(): void
    {
        (new BulkUpdate($this->manager, User::class))
            ->addEntity(new User(1, 'user 1', 'new_user1_password'))
            ->addEntity(new User(2, 'user 1', 'new_user2_password'), ['password'])
            ->execute();
    }
}