mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-04-21 16:01:56 +02:00
Added batch inserts for doctrine orm populate
This commit is contained in:
parent
856a44978d
commit
c49cd54386
@ -452,6 +452,8 @@ print_r($insertedPKs);
|
||||
// )
|
||||
```
|
||||
|
||||
**Note:** Due to the fact that `Faker` returns all the primary keys inserted, the memory consumption will go up drastically when you do batch inserts due to the big list of data.
|
||||
|
||||
In the previous example, the `Book` and `Author` models share a relationship. Since `Author` entities are populated first, Faker is smart enough to relate the populated `Book` entities to one of the populated `Author` entities.
|
||||
|
||||
Lastly, if you want to execute an arbitrary function on an entity before insertion, use the fourth argument of the `addEntity()` method:
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Faker\ORM\Doctrine;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Faker\Generator;
|
||||
|
||||
/**
|
||||
* Service class for populating a database using the Doctrine ORM or ODM.
|
||||
@ -10,20 +11,35 @@ use Doctrine\Common\Persistence\ObjectManager;
|
||||
*/
|
||||
class Populator
|
||||
{
|
||||
/** @var int */
|
||||
protected $batchSize;
|
||||
|
||||
/** @var Generator */
|
||||
protected $generator;
|
||||
|
||||
/** @var ObjectManager|null */
|
||||
protected $manager;
|
||||
|
||||
/** @var array */
|
||||
protected $entities = array();
|
||||
|
||||
/** @var array */
|
||||
protected $quantities = array();
|
||||
|
||||
/** @var array */
|
||||
protected $generateId = array();
|
||||
|
||||
/**
|
||||
* @param \Faker\Generator $generator
|
||||
* Populator constructor.
|
||||
* @param Generator $generator
|
||||
* @param ObjectManager|null $manager
|
||||
* @param int $batchSize
|
||||
*/
|
||||
public function __construct(\Faker\Generator $generator, ObjectManager $manager = null)
|
||||
public function __construct(Generator $generator, ObjectManager $manager = null, $batchSize = 1000)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
$this->manager = $manager;
|
||||
$this->batchSize = $batchSize;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,6 +71,9 @@ class Populator
|
||||
/**
|
||||
* Populate the database using all the Entity classes previously added.
|
||||
*
|
||||
* Please note that large amounts of data will result in more memory usage since the the Populator will return
|
||||
* all newly created primary keys after executing.
|
||||
*
|
||||
* @param null|EntityManager $entityManager A Doctrine connection object
|
||||
*
|
||||
* @return array A list of the inserted PKs
|
||||
@ -72,9 +91,18 @@ class Populator
|
||||
foreach ($this->quantities as $class => $number) {
|
||||
$generateId = $this->generateId[$class];
|
||||
for ($i=0; $i < $number; $i++) {
|
||||
$insertedEntities[$class][]= $this->entities[$class]->execute($entityManager, $insertedEntities, $generateId);
|
||||
$insertedEntities[$class][]= $this->entities[$class]->execute(
|
||||
$entityManager,
|
||||
$insertedEntities,
|
||||
$generateId
|
||||
);
|
||||
if (count($insertedEntities) % $this->batchSize === 0) {
|
||||
$entityManager->flush();
|
||||
$entityManager->clear($class);
|
||||
}
|
||||
}
|
||||
$entityManager->flush();
|
||||
$entityManager->clear($class);
|
||||
}
|
||||
|
||||
return $insertedEntities;
|
||||
|
Loading…
x
Reference in New Issue
Block a user