Chapters
29 Chapters
|
1:34:37
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
14.
More with ManyToMany: Avoiding Duplicates
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4", // v2.4.2
"doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
"doctrine/doctrine-bundle": "~1.2", // v1.2.0
"twig/extensions": "~1.0", // v1.0.1
"symfony/assetic-bundle": "~2.3", // v2.3.0
"symfony/swiftmailer-bundle": "~2.3", // v2.3.5
"symfony/monolog-bundle": "~2.4", // v2.5.0
"sensio/distribution-bundle": "~2.3", // v2.3.4
"sensio/framework-extra-bundle": "~3.0", // v3.0.0
"sensio/generator-bundle": "~2.3", // v2.3.4
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
"ircmaxell/password-compat": "~1.0.3", // 1.0.3
"phpunit/phpunit": "~4.1", // 4.1.0
"stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
}
}
4 Comments
if ($event->hasAttendee($this->getUser())) {
$event->getAttendees()->removeElement($this->getUser());
}
$em->persist($event);
$em->flush();
is it not better to do this instead? Why we should disturb our database if nothing needed?
if ($event->hasAttendee($this->getUser())) {
$event->getAttendees()->removeElement($this->getUser());
$em->persist($event);
$em->flush();
}
Hey there!
Yes, you're right! But either way, Doctrine won't make a query to the database if it doesn't need to. In other words, both ways will result in 0 queries if there is no change. But, there will be a small performance boost with your method, since Doctrine doesn't even need to check on this.
Cheers!
hasAttendee doing its job well in a Twig template.
But when a user tries to add new relationship it still could lead to error due to concurrency reasons.
Wouldn't it be better if we just try{} to add a relationship no matter what and then catch() the "duplicate key" exception?
Hey Ivan!
In that case you might preffer to create a new method for adding attendees like this:
So you can get rid of adding that try catch in every place where you need to add attendees
Have a nice day!
"Houston: no signs of life"
Start the conversation!