PHP code example of taxaos / doctrine-bulk

1. Go to this page and download the library: Download taxaos/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/ */

    

taxaos / doctrine-bulk example snippets



declare(strict_types = 1);

use Doctrine\ORM\EntityManagerInterface;
use DoctrineBulk\Bulk\BulkUpsert;

/**
 * Class DbWrite
 */
class DbWrite {
    private EntityManagerInterface $manager;

    /**
     * Creates two users in one query.
     *
     * @return int
     */
    public function updateExistingUsersAndCreateTwoUsers(): int
    {
        $dbUsers = []; // imagine some loaded users from DB and some changed data from your code 

        $bulkUpsert = new BulkUpsert($this->manager, User::class);

        foreach ($dbUsers as $dbUser) {
            $bulkUpsert->addEntity($dbUser, detach: true);
        }

        // now 2 new users
        $bulkUpsert->addEntity(new User('user 1', 'password'), detach: true);
        $bulkUpsert->addEntity(new User('user 2', 'password'), detach: true);

        $firstInsertedId = (int) $bulkUpsert->execute(maxRows: 1000);

        return $firstInsertedId;
    }
}