Skip to content

Commit e20e3c7

Browse files
author
Bram Ceulemans
authored
Removed ORM documentation from index and made a separate page (FakerPHP#38)
1 parent 6d9b785 commit e20e3c7

File tree

3 files changed

+66
-66
lines changed

3 files changed

+66
-66
lines changed

docs/index.md

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -155,72 +155,6 @@ for ($i = 0; $i < 3; $i++) {
155155

156156
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!
157157

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:
199-
200-
```php
201-
print_r($insertedPKs);
202-
// [
203-
// 'Author' => [34, 35, 36, 37, 38],
204-
// 'Book' => [456, 457, 458, 459, 470, 471, 472, 473, 474, 475],
205-
// ]
206-
```
207-
208-
???+ note
209-
210-
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-
224158
## Seeding the Generator
225159

226160
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.

docs/orm.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ORM Integration
2+
3+
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:
42+
43+
```php
44+
print_r($insertedPKs);
45+
// [
46+
// 'Author' => [34, 35, 36, 37, 38],
47+
// 'Book' => [456, 457, 458, 459, 470, 471, 472, 473, 474, 475],
48+
// ]
49+
```
50+
51+
???+ note
52+
53+
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:
58+
59+
```php
60+
$populator->addEntity('Book', 5, [], [
61+
function($book) {
62+
$book->publish();
63+
},
64+
]);
65+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ nav:
119119
- formatters/biased.md
120120
- formatters/html-lorem.md
121121
- 'Locales': *locales
122+
- orm.md
122123
- third-party.md
123124

124125
markdown_extensions:

0 commit comments

Comments
 (0)