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!
07.
OneToMany: The Inverse Side of a Relationship
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
}
}
5 Comments
Hey!
Why exactly do I need to add the inversedBy attribute? It's kind of strange that it works without it if there are no annotations at the inverse side and you have to add it if you add the annotations... Thanks as always!
Hey Max,
You need the "inversedBy" only when you configure "Bidirectional" relationships, otherwise you will get only "Unidirectional" one. OneToMany Bidirectional relationship means that you can get a collection of related entities from a mapped side and a mapped entity from an inversed side (i.e. get a mapped entity from every entity in a collection).
Cheers!
Is it possible to get the objects from $user->getEvents() back in a paginated format?
Hey!
It's a good question - I can see what you're thining! But basically, the answer is no. I think you *technically* could if you made a clever query that joined from User to events and selected only *some* events, but I'm not sure - I've never tried (http://knpuniversity.com/sc.... Doctrine really wants your User object to always be the same - it would be a little weird if sometimes I called $user->getEvents() and got all the events and other times called $user->getEvents() and only got back *some* events.
So, the best way is to create a custom query for all of the events *for* the User. And actually, most(all) of the pagination libraries (like PagerfantaBundle) want you to pass them a QueryBuilder object, not a final array of Event objects.
Cheers!
Thanks for "Owning Versus Inverse Side" excellent explanation, Ryan! It was hard to me to use it correctly. For OneToMany and ManyToOne relations Symfony2 plugin with `app/console doctrine:schema:validate` command helps me fine, but for ManyToMany it was still too difficult. But now it has become clearer!
"Houston: no signs of life"
Start the conversation!