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
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
343
+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
344
344
echo $blogPost->getUser()->getId(); // prints 2
345
345
```
346
346
@@ -402,8 +402,8 @@ echo $blogPost->getUser()->getPassword(); // prints 2BorN0t2B
402
402
DoctrineModule hydrator also handles OneToMany relationships (when use `Zend\Form\Element\Collection` element). Please
403
403
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.
404
404
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
407
407
is the empty string '' (as the ``find`` will most likely fail). In order to solve this problem, empty string identifiers
408
408
are simply ignored during the hydration phase. Therefore, if your database contains an empty string value as primary
409
409
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
436
436
protected $tags;
437
437
438
438
/**
439
-
* Never forget to initialize all your collections!
439
+
* Never forget to initialize your collections!
440
440
*/
441
441
public function __construct()
442
442
{
@@ -530,7 +530,7 @@ class Tag
530
530
}
531
531
```
532
532
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
534
534
functions must be always defined and are called automatically by Doctrine hydrator when dealing with collections.
535
535
You may think this is overkill, and ask why you cannot just define a `setTags` function to replace the old collection
536
536
by the new one:
@@ -543,21 +543,21 @@ public function setTags(Collection $tags)
543
543
```
544
544
545
545
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.
547
547
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.
549
549
550
550
##### Existing entity in the association
551
551
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:
553
553
554
554
```php
555
555
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
556
556
557
557
$hydrator = new DoctrineHydrator($entityManager);
558
558
$blogPost = new BlogPost();
559
559
$data = [
560
-
'title' => 'The best blog post in the world!',
560
+
'title' => 'The best blog post in the world!',
561
561
'tags' => [
562
562
['id' => 3], // add tag whose id is 3
563
563
['id' => 8], // also add tag whose id is 8
@@ -566,15 +566,15 @@ $data = [
566
566
567
567
$blogPost = $hydrator->hydrate($data, $blogPost);
568
568
569
-
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
569
+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
570
570
echo count($blogPost->getTags()); // prints 2
571
571
```
572
572
573
573
**NOTE** : once again, this:
574
574
575
575
```php
576
576
$data = [
577
-
'title' => 'The best blog post in the world!',
577
+
'title' => 'The best blog post in the world!',
578
578
'tags' => [
579
579
['id' => 3], // add tag whose id is 3
580
580
['id' => 8], // also add tag whose id is 8
@@ -586,14 +586,14 @@ can be written:
586
586
587
587
```php
588
588
$data = [
589
-
'title' => 'The best blog post in the world!',
589
+
'title' => 'The best blog post in the world!',
590
590
'tags' => [3, 8],
591
591
];
592
592
```
593
593
594
594
##### Non-existing entity in the association
595
595
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:
597
597
598
598
```php
599
599
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
@@ -612,13 +612,13 @@ $tag2->setName('STL');
612
612
$tags[] = $tag2;
613
613
614
614
$data = [
615
-
'title' => 'The best blog post in the world!',
615
+
'title' => 'The best blog post in the world!',
616
616
'tags' => $tags, // Note that you can mix integers and entities without any problem
617
617
];
618
618
619
619
$blogPost = $hydrator->hydrate($data, $blogPost);
620
620
621
-
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
621
+
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
622
622
echo count($blogPost->getTags()); // prints 2
623
623
```
624
624
@@ -656,7 +656,7 @@ $data = [
656
656
];
657
657
```
658
658
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:
660
660
661
661
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.
662
662
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
667
667
and extracting phase. All those strategies extend from the class
DoctrineModule provides two strategies out of the box:
670
+
DoctrineModule provides four strategies out of the box:
671
671
672
672
1.`DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByValue`: this is the default strategy, it removes old elements that are not in the new collection.
673
673
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.
678
678
On the other hand, when using the `DisallowRemove*` strategy, you must always define at least the adder, but the remover
679
679
is optional (because elements are never removed).
680
680
681
-
The following table illustrate the difference between the two strategies
681
+
The following table illustrates the difference between the two strategies
@@ -1178,9 +1179,9 @@ public function editAction()
1178
1179
1179
1180
Although using the hydrator is like magical as it abstracts most of the tedious task, you have to be aware that it can
1180
1181
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.
1182
1183
1183
-
#### Unwanting side-effect
1184
+
#### Unwanted side-effects
1184
1185
1185
1186
You have to be very careful when you are using DoctrineModule hydrator with complex entities that contain a lot of
1186
1187
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
1219
1220
}
1220
1221
```
1221
1222
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
1223
1224
forms the correct way, you will likely have a fieldset for every entity, so that you have a perfect mapping between
1224
1225
entities and fieldsets. Here are fieldsets for User and and City entities.
1225
1226
@@ -1402,14 +1403,14 @@ public function editNameAction()
1402
1403
}
1403
1404
```
1404
1405
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
1407
1408
made to fetch data for the City relationship of the user, and we hence have a completely useless database call,
1408
1409
as this information is not rendered by the form.
1409
1410
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
1411
1412
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:
1413
1414
1414
1415
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).
1415
1416
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
1442
1443
$userFieldset->setName('user');
1443
1444
$userFieldset->setUseAsBaseFieldset(true);
1444
1445
1445
-
// We don't want City relationship, so remove it!!
1446
+
// We don't want City relationship, so remove it!!
1446
1447
$userFieldset->remove('city');
1447
1448
1448
1449
$this->add($userFieldset);
@@ -1455,6 +1456,6 @@ class EditNameForm extends Form
1455
1456
}
1456
1457
```
1457
1458
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!
1459
1460
1460
1461
As a rule of thumb, try to remove any unnecessary fieldset relationship, and always look at which database calls are made.
0 commit comments