Chapters
40 Chapters
|
4:19:16
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
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.
The AssociationField
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 tutorial is built on Symfony 6 but works great on Symfony 7!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2.0",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"doctrine/doctrine-bundle": "^2.1", // 2.5.5
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.2.1
"doctrine/orm": "^2.7", // 2.10.4
"easycorp/easyadmin-bundle": "^4.0", // v4.0.2
"handcraftedinthealps/goodby-csv": "^1.4", // 1.4.0
"knplabs/knp-markdown-bundle": "dev-symfony6", // dev-symfony6
"knplabs/knp-time-bundle": "^1.11", // 1.17.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.5
"stof/doctrine-extensions-bundle": "^1.4", // v1.7.0
"symfony/asset": "6.0.*", // v6.0.1
"symfony/console": "6.0.*", // v6.0.2
"symfony/dotenv": "6.0.*", // v6.0.2
"symfony/flex": "^2.0.0", // v2.4.5
"symfony/framework-bundle": "6.0.*", // v6.0.2
"symfony/mime": "6.0.*", // v6.0.2
"symfony/monolog-bundle": "^3.0", // v3.7.1
"symfony/runtime": "6.0.*", // v6.0.0
"symfony/security-bundle": "6.0.*", // v6.0.2
"symfony/stopwatch": "6.0.*", // v6.0.0
"symfony/twig-bundle": "6.0.*", // v6.0.1
"symfony/ux-chartjs": "^2.0", // v2.0.1
"symfony/webpack-encore-bundle": "^1.7", // v1.13.2
"symfony/yaml": "6.0.*", // v6.0.2
"twig/extra-bundle": "^2.12|^3.0", // v3.3.7
"twig/twig": "^2.12|^3.0" // v3.3.7
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.1
"symfony/debug-bundle": "6.0.*", // v6.0.2
"symfony/maker-bundle": "^1.15", // v1.36.4
"symfony/var-dumper": "6.0.*", // v6.0.2
"symfony/web-profiler-bundle": "6.0.*", // v6.0.2
"zenstruck/foundry": "^1.1" // v1.16.0
}
}
10 Comments
This is more a nice-to-have than a question about the lecture:
I wanted to have it so when the user clicks on the "name" of the question in the CRUD index page, the user is redirected to the edit page (an alternative to having to click twice to edit: one to open the right menu and another to select "edit".
This is the code I ended up with:
This works as intended but I'm wondering if there's a better way to do it. Thanks!
Hey Pedro,
Nice, thank you for sharing your solution with others! This is indeed an interesting idea :)
Cheers!
What are the other ways to fix this problem?
Found the answer in Chapter 14 (choice_label option)
Hey Dmitriy,
Good catch! I'll leave a link to that chapter here: https://symfonycasts.com/sc...
Cheers!
He Team,
I have 3 entities. Templates, Blocks and TemplateBlocks. TemplateBlocks has a ManyToOne relationship to Templates and Blocks. Also Templates and Blocks has a OneToMany relationship with TemplateBlocks. The goal here is that a Template can have many blocks and I want to configure this in the TemplateCrudController. If i was to use ManyToMany the AssociationField would automatically show all Blocks. I cannot use ManyToMany because there's a extra column on TemplateBlocks called orderBy. How do you show all available blocks in this situation and make it sortable?
Hey Scott S.!
Ah yes, I'm familiar with this setup! But to get this setup in EasyAdmin - with a nice user experience - you're going to need to do some work. EasyAdmin's form system works via Symfony's form system... and there is no built-in "form type" that can handle what you want to do. If you want this to all happen on one screen, hmm. This is a tough one... the tools that I can think of just aren't built well to "bend" to this use-case, even if it's not that crazy of a use-case.
I actually started writing a long reply to this... but is just got SO complex. So, here is what I'd probably do in the real-world:
A) Do not add a
templateBlocksin TemplateCrudController. Instead, add some faketemplateBlocksEditorfield. Set a custom form type to "mapped false", which means that this data won't actually be set back onto the Template object. Make it, idk, a TextType.B) Immediately override the template for this field. The point is not actually to render a form field at all. Rather, we're just using this as a way to "put some custom HTML into the middle of the form".
C) In this template, add some HTML - and write some JavaScript in a JS file - that builds a 100% custom "template block editor". Work entirely outside of EasyAdmin to make things flexible. I'd make an Ajax call to fetch all of the current TemplateBlocks for the current Template (create this controller by hand: it doesn't need to live inside of EasyAmin in any way) and another Ajax call to fetch all of the available "Blocks". When the user selects a Block, make a POST Ajax call to another custom endpoint that takes in the Template id, that Block id, and saves a new TemplateBlock object. Also add whatever sorting JS you want to this interface. When the user stops sorting, send another Ajax request up with a list of the ordered ids. In that custom controller, update the orderBy on all of the TemplateBlocks.
So basically I'm saying: the interface you likely need is so custom that you should work entirely outside of EasyAdmin and build it yourself. Btw, the easiest way to do the above is to only allow this "template block editor" to load once a Template has been saved. So, for a new Template, you might render "Save Template to edit blocks" first.
Sorry I can't give you a quicker answer, but I hope this helps!
Cheers!
He weaverryan
I was digging into the different form types and thought it was my lack of knowledge why i couldn't find any way to do this. As i will be using EasyAdmin as a base for all my projects i will have to come up with a custom way of handeling this. Thanks for all your suggestions!
Hello Symfonycasts Team.
Does the autocomplete also have a corresponding rights check or can this Ajax endpoint be used by anyone who searches a bit through the source code of the website?
Hey Michael,
Good question! I've never thought about it before, most probably it does not have any ability to configure permissions check, at least I don't remember I see how it can be configured this way but I might be wrong. Try to play around to figure it out. Well, if you're asking if your users (not admin users) can leverage this feature to leak the data from admin - they answer is "no". Because as you might already know, EasyAdmin handles all its requests thought a single route, and if you already configured permissions, i.e. allows only admins to access your /admin - then you're safe and users won't be able to get any data leveraging that autocomplete feature because they don't have access to the /admin route. But if you're asking about configuration additional permissions between admin users - I'm not sure here, try to play around with the code a bit yourself :)
Cheers!
"Houston: no signs of life"
Start the conversation!