Chapters
28 Chapters
|
2:24:59
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
13.
Saving the Inverse Side of a ManyToMany
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.
This course is built on Symfony 3, but most of the concepts apply just fine to newer versions of Symfony.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"symfony/symfony": "3.4.*", // v3.4.49
"doctrine/orm": "^2.5", // 2.7.5
"doctrine/doctrine-bundle": "^1.6", // 1.12.13
"doctrine/doctrine-cache-bundle": "^1.2", // 1.4.0
"symfony/swiftmailer-bundle": "^2.3", // v2.6.7
"symfony/monolog-bundle": "^2.8", // v2.12.1
"symfony/polyfill-apcu": "^1.0", // v1.23.0
"sensio/distribution-bundle": "^5.0", // v5.0.25
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
"incenteev/composer-parameter-handler": "^2.0", // v2.1.4
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"knplabs/knp-markdown-bundle": "^1.4", // 1.9.0
"doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
"stof/doctrine-extensions-bundle": "^1.2" // v1.3.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.7
"symfony/phpunit-bridge": "^3.0", // v3.4.47
"nelmio/alice": "^2.1", // v2.3.6
"doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
}
}
6 Comments
I am having such a hard time trying to fix this issue.
Basically I have a "User" and "Profession" entity and I have a ManyToMany relationship between the two.
The user has the ability to edit their own profile, which they can do it via the form. The form at the moment display all basic information contained in the User table.
I now want to allow users to edit their own professions too, therefore I added the following to my ProfileFormType class:
When I try now to edit the profile, I can see the "profession" field with all the options and the one already assigned to the user are selected. The problem starts when I try to add/remove additional professions. When I try to save them, it doesn't do anything and I don't know why.
Inside the "User" class I have this:
and inside the Profession class, I have this:
My Profile controller (in the edit function) I have this:
Sorry for the length of this post, but I really need some help. I have tried slack, IRC but I can't find anyone who can help me understand what is wrong with it.
Thanks,
Alessandro
Hey Alessandro!
Hmm, ok. Here are the things that I'm looking at to make sure this works correctly:
1) You added a
professionfield to your form - but the property on your entity is calledprofessions(with an "s"). This should have caused an error. If there is no error, it must mean that you have (I'm guessing?) agetProfession()andsetProfession()method in yourUserclass? Those should not be there. Basically, make sure that in your form, the field is calledprofessions(with an s).2) Assuming the name of the field in the form is
professions, I would next look to make sure that yourUserclass hasaddProfession()andremoveProfession()methods. These should have been generated for you if you created the relationship viamake:entity. You should not have asetProfessionsmethod. Basically, because the field in your form is calledprofessions, when you submit the form, Symfony will first look for asetProfessions()method. If it exists, it will call it. We actually don't want that :). If it does not find this method, it will then look foraddProfession()andremoveProfession: it will call each method one time for all the professions that were added/removed by the user in the form. We WANT this.3) Side note: in Profession, it looks like the name of the property is "user". It should be "users" for clarity: it will hold an array of User objects :).
4) Here is the key: ultimately, because the
Profession.user(which really should beProfession.userfor clarity) is the "owning" side of the relationship, we ultimately need that side of the relation to be set. If the new data were only set ontoUser.professions, Doctrine will not save the updated data in the database. This is almost definitely what's happening to you. To make that happen, if you usedmake:entityto generate the relationship, the code inside theaddProfession()andremoveProfessionmethods will already contain code to do this. That will be enough to updating theProfession.userproperty and everything will save.The easiest way to get things cleaned up might be to:
A) remove the relationship entirely from Profession and User (remove all the "user" code from Profession - the property, methods, etc and then remove all the "profession" code from User).
B) Re-generate the relationship with
make:entityso that all the properties and methods are set up correctly.Let me know if this helps!
Cheers!
Where is this in the ''finish' code folder?
Hay Dave,
What exactly can't you find in the "finish/" folder?
Cheers!
I couldn't find any of the code to save the inverse many-to-many. I understand now that the finish folder is the end of this whole course, not this chapter. At the end of this course the many-to-many is turned into a one-to-many / many-to-one so that's why it's not there. Sorry for my misunderstanding, although it would be nice to have the code after each chapter since things keep changing.
Hey Dave,
Sorry for misleading you. Actually, we have an issue about this, but it's not prioritized yet. But for now, you can use our handy expandable code block below each video. You can even expand the entire file by pressing "Show All Lines" button (double arrows in the left top corner of each code block) to get more context. Plus, keep in mind "Copy" button in the right top corner, it prevent you to select the code manually.
Cheers!
"Houston: no signs of life"
Start the conversation!