| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\ORM\Doctrine;
|
|
|
4 |
|
|
|
5 |
use Doctrine\Common\Persistence\ObjectManager;
|
|
|
6 |
use Faker\Generator;
|
|
|
7 |
|
|
|
8 |
require_once 'backward-compatibility.php';
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Service class for populating a database using the Doctrine ORM or ODM.
|
|
|
12 |
* A Populator can populate several tables using ActiveRecord classes.
|
|
|
13 |
*/
|
|
|
14 |
class Populator
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* @var int
|
|
|
18 |
*/
|
|
|
19 |
protected $batchSize;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* @var Generator
|
|
|
23 |
*/
|
|
|
24 |
protected $generator;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @var ObjectManager|null
|
|
|
28 |
*/
|
|
|
29 |
protected $manager;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* @var array
|
|
|
33 |
*/
|
|
|
34 |
protected $entities = [];
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @var array
|
|
|
38 |
*/
|
|
|
39 |
protected $quantities = [];
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @var array
|
|
|
43 |
*/
|
|
|
44 |
protected $generateId = [];
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Populator constructor.
|
|
|
48 |
*
|
|
|
49 |
* @param int $batchSize
|
|
|
50 |
*/
|
|
|
51 |
public function __construct(Generator $generator, ObjectManager $manager = null, $batchSize = 1000)
|
|
|
52 |
{
|
|
|
53 |
$this->generator = $generator;
|
|
|
54 |
$this->manager = $manager;
|
|
|
55 |
$this->batchSize = $batchSize;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Add an order for the generation of $number records for $entity.
|
|
|
60 |
*
|
|
|
61 |
* @param mixed $entity A Doctrine classname, or a \Faker\ORM\Doctrine\EntityPopulator instance
|
|
|
62 |
* @param int $number The number of entities to populate
|
|
|
63 |
*/
|
|
|
64 |
public function addEntity($entity, $number, $customColumnFormatters = [], $customModifiers = [], $generateId = false)
|
|
|
65 |
{
|
|
|
66 |
if (!$entity instanceof \Faker\ORM\Doctrine\EntityPopulator) {
|
|
|
67 |
if (null === $this->manager) {
|
|
|
68 |
throw new \InvalidArgumentException('No entity manager passed to Doctrine Populator.');
|
|
|
69 |
}
|
|
|
70 |
$entity = new \Faker\ORM\Doctrine\EntityPopulator($this->manager->getClassMetadata($entity));
|
|
|
71 |
}
|
|
|
72 |
$entity->setColumnFormatters($entity->guessColumnFormatters($this->generator));
|
|
|
73 |
|
|
|
74 |
if ($customColumnFormatters) {
|
|
|
75 |
$entity->mergeColumnFormattersWith($customColumnFormatters);
|
|
|
76 |
}
|
|
|
77 |
$entity->mergeModifiersWith($customModifiers);
|
|
|
78 |
$this->generateId[$entity->getClass()] = $generateId;
|
|
|
79 |
|
|
|
80 |
$class = $entity->getClass();
|
|
|
81 |
$this->entities[$class] = $entity;
|
|
|
82 |
$this->quantities[$class] = $number;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Populate the database using all the Entity classes previously added.
|
|
|
87 |
*
|
|
|
88 |
* Please note that large amounts of data will result in more memory usage since the the Populator will return
|
|
|
89 |
* all newly created primary keys after executing.
|
|
|
90 |
*
|
|
|
91 |
* @param ObjectManager|null $entityManager A Doctrine connection object
|
|
|
92 |
*
|
|
|
93 |
* @return array A list of the inserted PKs
|
|
|
94 |
*/
|
|
|
95 |
public function execute($entityManager = null)
|
|
|
96 |
{
|
|
|
97 |
if (null === $entityManager) {
|
|
|
98 |
$entityManager = $this->manager;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if (null === $entityManager) {
|
|
|
102 |
throw new \InvalidArgumentException('No entity manager passed to Doctrine Populator.');
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
$insertedEntities = [];
|
|
|
106 |
|
|
|
107 |
foreach ($this->quantities as $class => $number) {
|
|
|
108 |
$generateId = $this->generateId[$class];
|
|
|
109 |
|
|
|
110 |
for ($i = 0; $i < $number; ++$i) {
|
|
|
111 |
$insertedEntities[$class][] = $this->entities[$class]->execute(
|
|
|
112 |
$entityManager,
|
|
|
113 |
$insertedEntities,
|
|
|
114 |
$generateId,
|
|
|
115 |
);
|
|
|
116 |
|
|
|
117 |
if (count($insertedEntities) % $this->batchSize === 0) {
|
|
|
118 |
$entityManager->flush();
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
$entityManager->flush();
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return $insertedEntities;
|
|
|
125 |
}
|
|
|
126 |
}
|