You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
-66Lines changed: 0 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -155,72 +155,6 @@ for ($i = 0; $i < 3; $i++) {
155
155
156
156
You can check available Faker locales in the source code, [under the `Provider` directory](https://github.com/FakerPHP/Faker/tree/main/src/Faker/Provider). The localization of Faker is an ongoing process, for which we need your help. Don't hesitate to create localized providers to your own locale and submit a PR!
157
157
158
-
## Populating Entities Using an ORM or an ODM
159
-
160
-
Faker provides adapters for Object-Relational and Object-Document Mappers (currently, [Propel](http://www.propelorm.org), [Doctrine2](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/), [CakePHP](http://cakephp.org), [Spot2](https://github.com/vlucas/spot2), [Mandango](https://github.com/mandango/mandango) and [Eloquent](https://laravel.com/docs/master/eloquent) are supported). These adapters ease the population of databases through the Entity classes provided by an ORM library (or the population of document stores using Document classes provided by an ODM library).
161
-
162
-
To populate entities, create a new populator class (using a generator instance as parameter), then list the class and number of all the entities that must be generated. To launch the actual data population, call the `execute()` method.
163
-
164
-
Note that some of the `populators` could require additional parameters. As example the `doctrine` populator has an option to specify
165
-
its batchSize on how often it will flush the UnitOfWork to the database.
166
-
167
-
Here is an example showing how to populate 5 `Author` and 10 `Book` objects:
168
-
169
-
```php
170
-
$generator = \Faker\Factory::create();
171
-
$populator = new \Faker\ORM\Propel\Populator($generator);
172
-
$populator->addEntity('Author', 5);
173
-
$populator->addEntity('Book', 10);
174
-
$insertedPKs = $populator->execute();
175
-
```
176
-
177
-
The populator uses name and column type guessers to populate each column with relevant data. For instance, Faker populates a column named `first_name` using the `firstName` formatter, and a column with a `TIMESTAMP` type using the `dateTime` formatter. The resulting entities are therefore coherent. If Faker misinterprets a column name, you can still specify a custom closure to be used for populating a particular column, using the third argument to `addEntity()`:
178
-
179
-
```php
180
-
$populator->addEntity('Book', 5, [
181
-
'ISBN' => function() use ($generator) { return $generator->ean13(); }
182
-
]);
183
-
```
184
-
185
-
In this example, Faker will guess a formatter for all columns except `ISBN`, for which the given anonymous function will be used.
186
-
187
-
???+ tip
188
-
189
-
To ignore some columns, specify `null` for the column names in the third argument of `addEntity()`. This is usually necessary for columns added by a behavior:
190
-
191
-
```php
192
-
$populator->addEntity('Book', 5, [
193
-
'CreatedAt' => null,
194
-
'UpdatedAt' => null,
195
-
]);
196
-
```
197
-
198
-
Of course, Faker does not populate auto-incremented primary keys. In addition, `Faker\ORM\Propel\Populator::execute()` returns the list of inserted PKs, indexed by class:
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.
211
-
212
-
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.
213
-
214
-
Lastly, if you want to execute an arbitrary function on an entity before insertion, use the fourth argument of the `addEntity()` method:
215
-
216
-
```php
217
-
$populator->addEntity('Book', 5, [], [
218
-
function($book) {
219
-
$book->publish();
220
-
},
221
-
]);
222
-
```
223
-
224
158
## Seeding the Generator
225
159
226
160
You may want to always get the same generated data - for instance when using Faker for unit testing purposes. The generator offers a `seed()` method, which seeds the random number generator. Calling the same script twice with the same seed produces the same results.
Faker provides adapters for Object-Relational and Object-Document Mappers (currently, [Propel](http://www.propelorm.org), [Doctrine2](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/), [CakePHP](http://cakephp.org), [Spot2](https://github.com/vlucas/spot2), [Mandango](https://github.com/mandango/mandango) and [Eloquent](https://laravel.com/docs/master/eloquent) are supported). These adapters ease the population of databases through the Entity classes provided by an ORM library (or the population of document stores using Document classes provided by an ODM library).
4
+
5
+
To populate entities, create a new populator class (using a generator instance as parameter), then list the class and number of all the entities that must be generated. To launch the actual data population, call the `execute()` method.
6
+
7
+
Note that some of the `populators` could require additional parameters. As example the `doctrine` populator has an option to specify
8
+
its batchSize on how often it will flush the UnitOfWork to the database.
9
+
10
+
Here is an example showing how to populate 5 `Author` and 10 `Book` objects:
11
+
12
+
```php
13
+
$generator = \Faker\Factory::create();
14
+
$populator = new \Faker\ORM\Propel\Populator($generator);
15
+
$populator->addEntity('Author', 5);
16
+
$populator->addEntity('Book', 10);
17
+
$insertedPKs = $populator->execute();
18
+
```
19
+
20
+
The populator uses name and column type guessers to populate each column with relevant data. For instance, Faker populates a column named `first_name` using the `firstName` formatter, and a column with a `TIMESTAMP` type using the `dateTime` formatter. The resulting entities are therefore coherent. If Faker misinterprets a column name, you can still specify a custom closure to be used for populating a particular column, using the third argument to `addEntity()`:
21
+
22
+
```php
23
+
$populator->addEntity('Book', 5, [
24
+
'ISBN' => function() use ($generator) { return $generator->ean13(); }
25
+
]);
26
+
```
27
+
28
+
In this example, Faker will guess a formatter for all columns except `ISBN`, for which the given anonymous function will be used.
29
+
30
+
???+ tip
31
+
32
+
To ignore some columns, specify `null` for the column names in the third argument of `addEntity()`. This is usually necessary for columns added by a behavior:
33
+
34
+
```php
35
+
$populator->addEntity('Book', 5, [
36
+
'CreatedAt' => null,
37
+
'UpdatedAt' => null,
38
+
]);
39
+
```
40
+
41
+
Of course, Faker does not populate auto-incremented primary keys. In addition, `Faker\ORM\Propel\Populator::execute()` returns the list of inserted PKs, indexed by class:
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.
54
+
55
+
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.
56
+
57
+
Lastly, if you want to execute an arbitrary function on an entity before insertion, use the fourth argument of the `addEntity()` method:
0 commit comments