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!
12.
EntityType Checkboxes with 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
}
}
16 Comments
Can you post your controller code ?
Hey there!
All the code is available below each video, just look for a proper code block. Our code blocks are expandable, so you can easily expand the code block to see the *full* code.
Cheers!
Is there any way to customize with css the checkboxes that represent an Entity Type?
Hey @mike
Yes, that's totally doable, you just have to load your custom CSS in the template. If you want to add a CSS class into the checkbox, well, there are many ways to do it (you can check our Symfony forms tutorial), but here is one: https://symfony.com/doc/cur...
Cheers!
I can't make it work on Symphony 4.
Warning: count(): Parameter must be an array or an object that implements Countable
in vendor\symfony\symfony\src\Symfony\Component\Validator\Validator\RecursiveContextualValidator.php at line 712
Thnaks!
Hey Gaston,
What exactly code causes this error?
Cheers!
Hi Victor! it happens when trying to login at http://127.0.0.1:8000/login (after submit the user-pass). I've followed all this tutorial from the code without problem till this chapter.
Thank you!
Hey Gaston,
Ah, now I see. That was a bug with PHP 7.2 that is fixed in further versions of dependencies. You can update all your dependencies with "composer up", or do a specific update at least: "composer up symfony/symfony twig/twig" - this helped me to get rid of this error.
Cheers!
This helped. Thanks!
Hey Yuriy,
I'm glad it was helpful for you :) Thanks for letting us know!
Cheers!
Hi Ryan,
I've set up ManyToMany Relationship. But as you mentioned before in my case the selectfield is filled up with a huge amount of data (which is a performance impact) I've tried to filter the form query with a custom query but I failed ... because I have no idea to fetch only the data which is really used. It is possible to fetch only the "selected" fields? In example case only showing up the users who are Genus scientists?
For adding / removing I would later build an ajax-query, so if user is typing in a part of username doctrine is quering the matching names out of database instead of loading all the huge bunch of data.
Thanks Ryan!
Regards,
Thomas
Ok, no problem! The issue is that, even if your user will only have, for example, 3 GenusScientists selected, Symfony needs to build the entire select field so that it's displayed to the user. In other words, Symfony needs to query for ALL GenusScientist objects in order to build that select field. So, there's no simple way around that. Of course, by the time you have this problem, you probably also have a select field that is SO long, it's kind of unusable :).
So, we need to "graduate" to the next solution! Unfortunately, I wish Symfony had a little bit more in "core" to help with this next solution - but there are some great libraries that can help. Here's the general idea - the specifics will vary: instead of rendering a select field with all of the scientists, we will instead render an
<input type="hidden"field, where the value is a comma-separated list of the id's selected, e.g. value="10, 5, 11". Then, you will use some JavaScript widget to build a fancy select box. You'll configure an AJAX auto-complete with this widget that, when you type, will send an AJAX request to a new Symfony endpoint that will return all the matching scientists based on what is typed. This is a classic AJAX auto-complete situation. However, when the user does select a scientist, you'll need to configure that widget to actually add this as another CSV item to the hidden input field - so we now would have value="10, 5, 11, 6". Now, the only really tricky part is how to get this to work well with the form system. Because, when these values are submitted - 10, 5, 11, 6 - we need to transform these into the 4 related entities, before setting them on the Genus object. Honestly, we need to create a little tutorial for this - it comes up all the time, and it's a little bit tricky. I've created a gist which is loosely based off of some code we have in our repository. The code is quite old, and it's honestly a bit messy, but it might help give you the right idea: https://gist.github.com/weaverryan/bb95f063895c546c471ad609ee8bf6cd. The trickiest part is creating a new custom form field that is able to transform the CSV is ids into an array of object on submit. This field basically works a lot like the EntityType field, except that it renders as a CSV text field instead (and so doesn't have the performance problem, since it doesn't need to load all the options).I hope this helps!
Me again Ryan :) I had time to cover this, just went through the simple way. First I filled form with a hidden field with id (in my case it is only 1 id, but it doesn't matter if 1 ore more). If user now is start typing within form field live search pops up and doctrine is catching the most suitable entries. Now if entry is clicked the hidden form field is filled with the new id.
After submitting the form doctrine catch up the entity and setting it to the object.
I am not complete with all your tutorials but if I find a better solution I will keep you informed :)
Hi Ryan, thanks a lot for giving this great hint. I will check this out and come back to you with my results.
I've someone gets the error:
Type error: Argument 1 passed to AppBundle\Form\GenusFormType::AppBundle\Form\{closure}() must be an instance of AppBundle\Repository\SubFamilyRepository, instance of Doctrine\ORM\EntityRepository given.
Then they must change the following line in the repository User class
@ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
Maybe i missed it somewhere in the video's
Yo Bertin!
Ah, this is probably my fault actually :). I made a few changes *before* this tutorial, including adding some more fields to User and adding this code for the repository. So, if you're coding straight through from the previous tutorial, you'll be missing these things. You should have them download the fresh start code for this tutorial. I *usually* mention this very clearly at the beginning of the tutorial ("Hey! I made some updates, to download the fresh start code"), but I apparently forgot in this case. Sorry for the confusion!
Cheers!
"Houston: no signs of life"
Start the conversation!