Skip to content

Commit 6e4b2cd

Browse files
author
Greg Bell
committed
Comment and grammar fixes
1 parent 5cbcd6a commit 6e4b2cd

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

docs/hydrator.md

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -273,37 +273,37 @@ class BlogPost
273273
}
274274
```
275275

276-
There are two use cases that can arise when using OneToOne association: the toOne entity (in the case, the user) may
277-
already exist (which will often be the case with a User and BlogPost example), or it can be created too. The
276+
There are two use cases that can arise when using OneToOne association: the toOne entity (in this case, the User) may
277+
already exist (which will often be the case with a User and BlogPost example), or it can be created. The
278278
DoctrineHydrator natively supports both cases.
279279

280280
##### Existing entity in the association
281281

282-
When the association's entity already exists, what you need to do is simply giving the identifier of the association:
282+
When the association's entity already exists, all you need to do is simply give the identifier of the association:
283283

284284
```php
285285
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
286286

287287
$hydrator = new DoctrineHydrator($entityManager);
288288
$blogPost = new BlogPost();
289289
$data = [
290-
'title' => 'The best blog post in the world !',
290+
'title' => 'The best blog post in the world!',
291291
'user' => [
292292
'id' => 2, // Written by user 2
293293
],
294294
];
295295

296296
$blogPost = $hydrator->hydrate($data, $blogPost);
297297

298-
echo $blogPost->getTitle(); // prints "The best blog post in the world !"
298+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
299299
echo $blogPost->getUser()->getId(); // prints 2
300300
```
301301

302302
**NOTE** : when using association whose primary key is not compound, you can rewrite the following more succinctly:
303303

304304
```php
305305
$data = [
306-
'title' => 'The best blog post in the world !',
306+
'title' => 'The best blog post in the world!',
307307
'user' => [
308308
'id' => 2, // Written by user 2
309309
],
@@ -314,15 +314,15 @@ to:
314314

315315
```php
316316
$data = [
317-
'title' => 'The best blog post in the world !',
317+
'title' => 'The best blog post in the world!',
318318
'user' => 2,
319319
];
320320
```
321321

322322

323323
##### Non-existing entity in the association
324324

325-
If the association's entity does not exist, you just need to give the given object:
325+
If the association's entity does not exist, you just need to give the object:
326326

327327
```php
328328
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
@@ -334,13 +334,13 @@ $user->setUsername('bakura');
334334
$user->setPassword('p@$$w0rd');
335335

336336
$data = [
337-
'title' => 'The best blog post in the world !',
337+
'title' => 'The best blog post in the world!',
338338
'user' => $user,
339339
];
340340

341341
$blogPost = $hydrator->hydrate($data, $blogPost);
342342

343-
echo $blogPost->getTitle(); // prints "The best blog post in the world !"
343+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
344344
echo $blogPost->getUser()->getId(); // prints 2
345345
```
346346

@@ -402,8 +402,8 @@ echo $blogPost->getUser()->getPassword(); // prints 2BorN0t2B
402402
DoctrineModule hydrator also handles OneToMany relationships (when use `Zend\Form\Element\Collection` element). Please
403403
refer to the official [Zend Framework 2 documentation](http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html) to learn more about Collection.
404404

405-
> Note: internally, for a given collection, if an array contains identifiers, the hydrator automatically fetch the
406-
objects through the Doctrine `find` function. However, this may cause problems if one of the value of the collection
405+
> Note: internally, for a given collection, if an array contains identifiers, the hydrator automatically fetches the
406+
objects through the Doctrine `find` function. However, this may cause problems if one of the values of the collection
407407
is the empty string '' (as the ``find`` will most likely fail). In order to solve this problem, empty string identifiers
408408
are simply ignored during the hydration phase. Therefore, if your database contains an empty string value as primary
409409
key, the hydrator could not work correctly (the simplest way to avoid that is simply to not have an empty string primary
@@ -436,7 +436,7 @@ class BlogPost
436436
protected $tags;
437437

438438
/**
439-
* Never forget to initialize all your collections !
439+
* Never forget to initialize your collections!
440440
*/
441441
public function __construct()
442442
{
@@ -530,7 +530,7 @@ class Tag
530530
}
531531
```
532532

533-
Please note interesting things in BlogPost entity. We have defined two functions: addTags and removeTags. Those
533+
Please note some interesting things in BlogPost entity. We have defined two functions: addTags and removeTags. Those
534534
functions must be always defined and are called automatically by Doctrine hydrator when dealing with collections.
535535
You may think this is overkill, and ask why you cannot just define a `setTags` function to replace the old collection
536536
by the new one:
@@ -543,21 +543,21 @@ public function setTags(Collection $tags)
543543
```
544544

545545
But this is very bad, because Doctrine collections should not be swapped, mostly because collections are managed by
546-
an ObjectManager, thus it must not be replaced by a new instance.
546+
an ObjectManager, thus they must not be replaced by a new instance.
547547

548-
Once again, two cases may arise: the tags already exist or they does not.
548+
Once again, two cases may arise: the tags already exist or they do not.
549549

550550
##### Existing entity in the association
551551

552-
When the association's entity already exists, what you need to do is simply giving the identifiers of the entities:
552+
When the association's entity already exists, what you need to do is simply give the identifiers of the entities:
553553

554554
```php
555555
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
556556

557557
$hydrator = new DoctrineHydrator($entityManager);
558558
$blogPost = new BlogPost();
559559
$data = [
560-
'title' => 'The best blog post in the world !',
560+
'title' => 'The best blog post in the world!',
561561
'tags' => [
562562
['id' => 3], // add tag whose id is 3
563563
['id' => 8], // also add tag whose id is 8
@@ -566,15 +566,15 @@ $data = [
566566

567567
$blogPost = $hydrator->hydrate($data, $blogPost);
568568

569-
echo $blogPost->getTitle(); // prints "The best blog post in the world !"
569+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
570570
echo count($blogPost->getTags()); // prints 2
571571
```
572572

573573
**NOTE** : once again, this:
574574

575575
```php
576576
$data = [
577-
'title' => 'The best blog post in the world !',
577+
'title' => 'The best blog post in the world!',
578578
'tags' => [
579579
['id' => 3], // add tag whose id is 3
580580
['id' => 8], // also add tag whose id is 8
@@ -586,14 +586,14 @@ can be written:
586586

587587
```php
588588
$data = [
589-
'title' => 'The best blog post in the world !',
589+
'title' => 'The best blog post in the world!',
590590
'tags' => [3, 8],
591591
];
592592
```
593593

594594
##### Non-existing entity in the association
595595

596-
If the association's entity does not exist, you just need to give the given object:
596+
If the association's entity does not exist, you just need to give the object:
597597

598598
```php
599599
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
@@ -612,13 +612,13 @@ $tag2->setName('STL');
612612
$tags[] = $tag2;
613613

614614
$data = [
615-
'title' => 'The best blog post in the world !',
615+
'title' => 'The best blog post in the world!',
616616
'tags' => $tags, // Note that you can mix integers and entities without any problem
617617
];
618618

619619
$blogPost = $hydrator->hydrate($data, $blogPost);
620620

621-
echo $blogPost->getTitle(); // prints "The best blog post in the world !"
621+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
622622
echo count($blogPost->getTags()); // prints 2
623623
```
624624

@@ -656,7 +656,7 @@ $data = [
656656
];
657657
```
658658

659-
The hydrator will check whether the setCity() method on the Entity allows null values and acts accordingly, the following describes the process that happens when a null value is received:
659+
The hydrator will check whether the setCity() method on the Entity allows null values and act accordingly. The following describes the process that happens when a null value is received:
660660

661661
1. If the setCity() method DOES NOT allow null values i.e. `function setCity(City $city)`, the null is silently ignored and will not be hydrated.
662662
2. If the setCity() method DOES allow null values i.e. `function setCity(City $city = null)`, the null value will be hydrated.
@@ -667,7 +667,7 @@ By default, every collections association has a special strategy attached to it
667667
and extracting phase. All those strategies extend from the class
668668
`DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy`.
669669

670-
DoctrineModule provides two strategies out of the box:
670+
DoctrineModule provides four strategies out of the box:
671671

672672
1. `DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByValue`: this is the default strategy, it removes old elements that are not in the new collection.
673673
2. `DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByReference`: this is the default strategy (if set to byReference), it removes old elements that are not in the new collection.
@@ -678,15 +678,15 @@ As a consequence, when using `AllowRemove*`, you need to define both adder (eg.
678678
On the other hand, when using the `DisallowRemove*` strategy, you must always define at least the adder, but the remover
679679
is optional (because elements are never removed).
680680

681-
The following table illustrate the difference between the two strategies
681+
The following table illustrates the difference between the two strategies
682682

683683
| Strategy | Initial collection | Submitted collection | Result |
684684
| -------- | ------------------ | -------------------- | ------ |
685685
| AllowRemove* | A, B | B, C | B, C
686686
| DisallowRemove* | A, B | B, C | A, B, C
687687

688-
The difference between ByValue and ByReference is that when using strategies that end by ByReference, it won't use
689-
the public API of your entity (adder and remover) - you don't even need to define them -. It will directly add and
688+
The difference between ByValue and ByReference is that when using strategies that end with ByReference, it won't use
689+
the public API of your entity (adder and remover) - you don't even need to define them - it will directly add and
690690
remove elements directly from the collection.
691691

692692

@@ -781,7 +781,7 @@ It now only prints "bar", which shows clearly that the getter has not been calle
781781

782782
### A complete example using Zend\Form
783783

784-
Now that we understand how the hydrator works, let's see how it integrates into the Zend Framework 2's Form component.
784+
Now that we understand how the hydrator works, let's see how it integrates into the Zend Framework's Form component.
785785
We are going to use a simple example with, once again, a BlogPost and a Tag entities. We will see how we can create the
786786
blog post, and being able to edit it.
787787

@@ -814,7 +814,7 @@ class BlogPost
814814
protected $tags;
815815

816816
/**
817-
* Never forget to initialize all your collections !
817+
* Never forget to initialize your collections!
818818
*/
819819
public function __construct()
820820
{
@@ -937,13 +937,13 @@ class Tag
937937

938938
#### The fieldsets
939939

940-
We now need to create two fieldsets that will map those entities. With Zend Framework 2, it's a good practice to create
940+
We now need to create two fieldsets that will map those entities. With Zend Framework, it's a good practice to create
941941
one fieldset per entity in order to reuse them across many forms.
942942

943943
Here is the fieldset for the Tag. Notice that in this example, I added a hidden input whose name is "id". This is
944-
needed for editing. Most of the time, when you create the Blog Post for the first time, the tags does not exist.
945-
Therefore, the id will be empty. However, when you edit the blog post, all the tags already exists in database (they
946-
have been persisted and have an id), and hence the hidden "id" input will have a value. This allow you to modify a tag
944+
needed for editing. Most of the time, when you create the Blog Post for the first time, the tags do not exist.
945+
Therefore, the id will be empty. However, when you edit the blog post, all the tags already exist in database (they
946+
have been persisted and have an id), and hence the hidden "id" input will have a value. This allows you to modify a tag
947947
name by modifying an existing Tag entity without creating a new tag (and removing the old one).
948948

949949
```php
@@ -1039,13 +1039,13 @@ class BlogPostFieldset extends Fieldset implements InputFilterProviderInterface
10391039
}
10401040
```
10411041

1042-
Plain and easy. The blog post is just a simple fieldset with an element type of type ``Zend\Form\Element\Collection``
1042+
Plain and easy. The blog post is just a simple fieldset with an element type of ``Zend\Form\Element\Collection``
10431043
that represents the ManyToOne association.
10441044

10451045
#### The form
10461046

10471047
Now that we have created our fieldset, we will create two forms: one form for creation and one form for updating.
1048-
The form task is to make the glue between the fieldsets. In this simple example, both forms are exactly the same,
1048+
The form's purpose is to be the glue between the fieldsets. In this simple example, both forms are exactly the same,
10491049
but in a real application, you may want to change this behaviour by changing the validation group (for instance, you
10501050
may want to disallow the user to modify the title of the blog post when updating).
10511051

@@ -1067,7 +1067,7 @@ class CreateBlogPostForm extends Form
10671067
// The form will hydrate an object of type "BlogPost"
10681068
$this->setHydrator(new DoctrineHydrator($objectManager));
10691069

1070-
// Add the user fieldset, and set it as the base fieldset
1070+
// Add the BlogPost fieldset, and set it as the base fieldset
10711071
$blogPostFieldset = new BlogPostFieldset($objectManager);
10721072
$blogPostFieldset->setUseAsBaseFieldset(true);
10731073
$this->add($blogPostFieldset);
@@ -1097,7 +1097,7 @@ class UpdateBlogPostForm extends Form
10971097
// The form will hydrate an object of type "BlogPost"
10981098
$this->setHydrator(new DoctrineHydrator($objectManager));
10991099

1100-
// Add the user fieldset, and set it as the base fieldset
1100+
// Add the BlogPost fieldset, and set it as the base fieldset
11011101
$blogPostFieldset = new BlogPostFieldset($objectManager);
11021102
$blogPostFieldset->setUseAsBaseFieldset(true);
11031103
$this->add($blogPostFieldset);
@@ -1157,7 +1157,8 @@ public function editAction()
11571157
// Create the form and inject the ObjectManager
11581158
$form = new UpdateBlogPostForm($objectManager);
11591159

1160-
// Create a new, empty entity and bind it to the form
1160+
// Fetch the existing BlogPost from storage and bind it to the form.
1161+
// This will pre-fill form field values
11611162
$blogPost = $this->userService->get($this->params('blogPost_id'));
11621163
$form->bind($blogPost);
11631164

@@ -1178,9 +1179,9 @@ public function editAction()
11781179

11791180
Although using the hydrator is like magical as it abstracts most of the tedious task, you have to be aware that it can
11801181
leads to performance issues in some situations. Please carefully read the following paragraphs in order to know how
1181-
to solve (and avoid !) them.
1182+
to solve (and avoid!) them.
11821183

1183-
#### Unwanting side-effect
1184+
#### Unwanted side-effects
11841185

11851186
You have to be very careful when you are using DoctrineModule hydrator with complex entities that contain a lot of
11861187
associations, as a lot of unnecessary calls to database can be made if you are not perfectly aware of what happen
@@ -1219,7 +1220,7 @@ class User
12191220
}
12201221
```
12211222

1222-
This simple entity contains an id, a string property, and a OneToOne relationship. If you are using Zend Framework 2
1223+
This simple entity contains an id, a string property, and a OneToOne relationship. If you are using Zend Framework
12231224
forms the correct way, you will likely have a fieldset for every entity, so that you have a perfect mapping between
12241225
entities and fieldsets. Here are fieldsets for User and and City entities.
12251226

@@ -1402,14 +1403,14 @@ public function editNameAction()
14021403
}
14031404
```
14041405

1405-
This looks good, isn't it ? However, if we check the queries that are made (for instance using the awesome
1406-
[ZendDeveloperTools module](https://github.com/zendframework/ZendDeveloperTools)), we will see that a request is
1406+
This looks good, doesn't it? However, if we check the queries that are made (for instance using the awesome
1407+
[ZendDeveloperTools module](https://github.com/zendframework/zend-developer-tools), we will see that a request is
14071408
made to fetch data for the City relationship of the user, and we hence have a completely useless database call,
14081409
as this information is not rendered by the form.
14091410

1410-
You could ask, why ? Yes, we set the validation group, BUT the problem happens during the extracting phase. Here is
1411+
You could ask, "why?" Yes, we set the validation group, BUT the problem happens during the extracting phase. Here is
14111412
how it works : when an object is bound to the form, this latter iterates through all its fields, and tries to extract
1412-
the data from the object that is bound. In our example, here is how it work :
1413+
the data from the object that is bound. In our example, here is how it works:
14131414

14141415
1. It first arrives to the UserFieldset. The input are "name" (which is string field), and a "city" which is another fieldset (in our User entity, this is a OneToOne relationship to another entity). The hydrator will extract both the name and the city (which will be a Doctrine 2 Proxy object).
14151416
2. Because the UserFieldset contains a reference to another Fieldset (in our case, a CityFieldset), it will, in turn, tries to extract the values of the City to populate the values of the CityFieldset. And here is the problem : City is a Proxy, and hence because the hydrator tries to extract its values (the name and postcode field), Doctrine will automatically fetch the object from the database in order to please the hydrator.
@@ -1442,7 +1443,7 @@ class EditNameForm extends Form
14421443
$userFieldset->setName('user');
14431444
$userFieldset->setUseAsBaseFieldset(true);
14441445

1445-
// We don't want City relationship, so remove it !!
1446+
// We don't want City relationship, so remove it!!
14461447
$userFieldset->remove('city');
14471448

14481449
$this->add($userFieldset);
@@ -1455,6 +1456,6 @@ class EditNameForm extends Form
14551456
}
14561457
```
14571458

1458-
And boom ! As the UserFieldset does not contain the CityFieldset relation anymore, it won't be extracted !
1459+
And boom! As the UserFieldset does not contain the CityFieldset relation anymore, it won't be extracted!
14591460

14601461
As a rule of thumb, try to remove any unnecessary fieldset relationship, and always look at which database calls are made.

0 commit comments

Comments
 (0)