6dreams / doctrine-bulk
Simple classes that allows bulk operations on doctrine entities.
Installs: 34 881
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 4
Forks: 9
Open Issues: 6
Requires
- php: >=8.0
- doctrine/orm: ^2.5
Requires (Dev)
- phpunit/phpunit: 8.5.8
README
Adds ability to multiple insert of entities or array to database using doctrine schema.
Notes
- Designed for MySQL (also works with PostgreSQL)
- Works with custom id generators (need few tweaks)
- Without custom id generator, works only with MySQL AI
- Allows retrive first inserted id \ total updated
- As bonus this package includes
HashedIdGenerator
that can be used for generate char(25) ids from entity data - Please note that UPDATE queries with HashedIdGenerator currently didn't support changing Id
Samples
Default usage
<?php 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(); } }