Added EntityManager interaction into examples
This commit is contained in:
parent
f309190486
commit
99533afd11
@ -297,12 +297,23 @@ relations of the `User`:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Interaction would then look like the following code parts, `$em` here is an instance of the EntityManager:
|
||||||
|
|
||||||
|
$user = $em->find('User', $userId);
|
||||||
|
|
||||||
// unidirectional many to many
|
// unidirectional many to many
|
||||||
|
$comment = $em->find('Comment', $readCommentId);
|
||||||
$user->getReadComments()->add($comment);
|
$user->getReadComments()->add($comment);
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
// unidirectional many to one
|
// unidirectional many to one
|
||||||
|
$myFirstComment = new Comment();
|
||||||
$user->setFirstComment($myFirstComment);
|
$user->setFirstComment($myFirstComment);
|
||||||
|
|
||||||
|
$em->persist($myFirstComment);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
In the case of bi-directional associations you have to update the fields on both sides:
|
In the case of bi-directional associations you have to update the fields on both sides:
|
||||||
|
|
||||||
[php]
|
[php]
|
||||||
@ -335,11 +346,17 @@ In the case of bi-directional associations you have to update the fields on both
|
|||||||
// Many-to-Many
|
// Many-to-Many
|
||||||
$user->getFavorites()->add($favoriteComment);
|
$user->getFavorites()->add($favoriteComment);
|
||||||
$favoriteComment->getUserFavorites()->add($user);
|
$favoriteComment->getUserFavorites()->add($user);
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
// Many-To-One / One-To-Many Bidirectional
|
// Many-To-One / One-To-Many Bidirectional
|
||||||
|
$newComment = new Comment();
|
||||||
$user->getAuthoredComments()->add($newComment);
|
$user->getAuthoredComments()->add($newComment);
|
||||||
$newComment->setAuthor($user);
|
$newComment->setAuthor($user);
|
||||||
|
|
||||||
|
$em->persist();
|
||||||
|
$m->flush();
|
||||||
|
|
||||||
|
|
||||||
Notice how always both sides of the bidirectional association are updated. The previous unidirectional associations were simpler to handle.
|
Notice how always both sides of the bidirectional association are updated. The previous unidirectional associations were simpler to handle.
|
||||||
|
|
||||||
@ -360,6 +377,8 @@ to do so, by key and by element. Here are some examples:
|
|||||||
$user->getComments()->removeElement($ithComment);
|
$user->getComments()->removeElement($ithComment);
|
||||||
$comment->setAuthor(null);
|
$comment->setAuthor(null);
|
||||||
|
|
||||||
|
You need to call `$em->flush()` to make persist these changes in the database permanently.
|
||||||
|
|
||||||
Notice how both sides of the bidirectional association are always updated. Unidirectional associations are consequently
|
Notice how both sides of the bidirectional association are always updated. Unidirectional associations are consequently
|
||||||
simpler to handle. Also note that if you type-hint your methods, i.e. `setAddress(Address $address)`, then PHP does only
|
simpler to handle. Also note that if you type-hint your methods, i.e. `setAddress(Address $address)`, then PHP does only
|
||||||
allows null values if `null` is set as default value. Otherwise setAddress(null) will fail for removing the association.
|
allows null values if `null` is set as default value. Otherwise setAddress(null) will fail for removing the association.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user