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!
13.
Auto-complete Association Field & Controlling the Query
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
}
}
22 Comments
Resolved: answer below
I have a question about CollectionField.
Use case: some related items should be displayed, others not, based on a field of the related entity (hidden or not).
I tried to add a query to define what related items should be displayed.
However, ->setQueryBuilder is not available for CollectionField.
Thanks.
Update
with CollectionField->setFormTypeOption('getter', fn () => ... I was able to only show selection of related entities, however after saving the main entity, the system removed the related entities that were not displayed, what is normal behaviour.
So I need to find a solution to prevent deletion of items that are not displayed by the getter. Maybe with the setter?
Update
Found a solution, adjust the removeRelatedEntity function in the main Entity to prevent removal of the related entity (and find another way to delete the hidden ones).
Hey @fvquicke
Thank you for sharing your problem and solution!
Hello, I have two entities, category and sub-category which are linked together, and which themselves are linked to a product entity. What I would like is that in easy admin, when I create a product, when I select a category, the sub-category field is automatically updated with the data related to this category. Do you have any idea how I can set this up? I know that with Symfony, we can use Events Form, but no idea how to use it in Easy Admin... Thank you :)
Hey @Naim!
This is... always trickier than it should be! Yes, the first part of the answer is using form events. But the second answer is "how do I make an Ajax call to "submit" the
categorydata and re-render the "sub-category" field. As far as I know, EasyAdmin doesn't supply anything for that second part - https://github.com/EasyCorp/EasyAdminBundle/issues/4716Personally (though I have not done this yet), I would solve this by:
A) First setting up the form events as needed to change the sub-category options/query based on the
categoryfield. Form events are tricky, but this is well-documented. You can test that this is working by selecting thecategoryfield and submitting your form. When the page re-renders (with validation errors), if you've done your job correctly, the sub-category field will have the correct options.B) For the 2nd part (the Ajax call), I would LOVE to use LiveComponents for this... it would be super easy... except I think it will be difficult, currently, to build the identical "form" inside of your component class that you have in your crud controller. It'd be great to make that easier in the future. Without that option, I think you'll need to write a little bit of custom JavaScript that, on change of your category field, submits the form via Ajax. When you get back the HTML response, you would find the sub-category field in that new HTML and replace the sub-category on the page with it. It's some pretty boring work to make it happen, but it shouldn't be too bad.
Let me know how it goes - it'd be great to make this easier for everyone using EasyAdmin in the future - the solution I'm giving you doesn't excite me :).
Cheers!
Hi!
Is where any way to set custom url for autocomplete request for Association Field?
Hey Dmitry,
Well, that's not configurable, unfortunately, but you can override that autocomplete method in the CRUD controller to which it sends AJAX requests and do there whatever you want.
I hope this helps!
Cheers!
I've a many to many relation solved by a third entity because i needed some extra fields.
Now I want to display these extra fields in the edit section of the CRUD Controller wich the entity is related on.
Some facts, the entity is called
runSheetsand the many to many relation entity is calledrunSheetStationsthat combinesrunSheetsandstations. TherunSheetsStationshas two extra fieldsdone(boolean) andmemo(text).How can I create fields that will represent those fields and how could I save changes.
Thank you very much für your help.
Hey @Klaus-M!
Hmm. Ok, so if I understand correctly, when editing a single
runSheetsentity, you would like to show a "collection" of sub-forms for each relatedrunSheetStationswhere each sub-form shows thedoneandmemofield. Is that correct?If so, the solution should be to use the
CollectionField- https://symfony.com/doc/4.x/EasyAdminBundle/fields/CollectionField.html - for therunSheetStationsproperty onrunSheets(I'm assuming that yourrunSheetsentity has arunSheetsStationsproperty which is aOneToManyto therunSheetStationsentity). That... should be all you need to do (though, this stuff is complex enough that I would NOT be surprised if I'm forgetting a detail). TheCollectionFieldstuff can get really complex... but that's mostly if you need to be able to dynamically add new relations... but it doesn't sound like you even have that case: you just want the user to be able to modify some fields on the existing, relatedrunSheetStations.Let me know if that helps :). Cheers!
Hello,
What does the difference of `static function` and `function` of anonymous-function used on this chapter?
Hey suabahasa,
Good question! Though it's not that much important fairly speaking, but I'll try to explain you some details. Just functions are bind to the instance context, i.e. you can use $this inside those functions, i.e. they are bind to the *objects* of classes where you use them. When static functions are bind to the class, i.e. they do not have access to the $this variable inside of them. And so, they are mostly like static and regular methods on its behaviour, and also has similar performance. If you're not going to use $this inside those functions - it's recommend to make them static that will have a better performance, but that is minor and difficult to be noticed anyway, but depends on your project code complexity. So, it's mostly like a "hipster" question, use static when you could to be more hipster :p ;)
I hope it's clearer for you now.
Cheers!
Hi Victor,
thank you for detailed answer.
Before, I curious if there is some special use case/best-practice of using the "static" for this chapter, but now I got the answer.
Cheers!
Hey Suabahasa,
Awesome, glad to help! :)
Cheers!
I have an employee entity that has a self-refencing field supervisor (which is a ManyToOne Association).
In EmployeeCrudController, I have in ConfigureFields -- yield AssociationField::new('supervisor'). This works fine when I am creating new Employee.
Employee:Peter <== Supervisor:John
But when I want to edit the above for employee:Peter, the Association Field Supervisor will include the employee Peter itself as one of the choices. I thought of using the querybuilder but I can't think of how to get the current record to be edited so I can filter it out.
Hey Peter,
Ah, I see your problem. Yeah, that's a tricky case. You're on the correct way, you would need to use a custom query builder to filter that out. How to get the current Employee ID? Take it from the URL. If you look closer at the URL - you will notice that the current employee ID is written there as "entityId". So, inject RequestStack into your controller, get the current Request object from it and get the "entityId" from the query. Then, just use it in the query builder to filter it out.
Or, probably even better, you can leverage EasyAdmin getContext() to get the current Request object, i.e. call "$this->getContext()->getRequest()". Btw, you can even try to get the current entity object as "$this->getContext()->getEntity()->getInstance()", but it might be overkill in case you just need its ID - you just can fetch it from the URL query parameter.
I hope this helps!
Cheers!
nice..Thanks
Hello,
It is possible to use methods from Entity's Repository (ie findBy...()) instead of specifying it on QueryBuilder?
Thanks for reply
Hey Tomáš S.!
Excellent question! It is not, unfortunately. The technical reason is that, in addition to querying for all the items to list them, iirc, the QueryBuilder is also used when an item is submitted to figure out if the submitted item is a "valid" item. For example, it would take your QueryBuilder (which may already have some WHERE on it) and (again, I'm going off memory here, but I believe this is how it works) add a
AND id = 12where 12 is the id of the submitted item. If the system allowed you to only pass it the findBy(), then even for just checking the validity of the submitted item, it would need to use your method to query for ALL of the items... just to check if the submitted one is in there.Anyways, the "best answer" I can give you is this: if you already have an existing, custom findByABC() method in your repository, and you'd like to re-use that logic, then isolate your custom QueryBuilder logic into some
createFindByABCQueryBuilder(QueryBuilder $qb = null): QueryBuildermethod. Then, call this from your custom findByABC() method and when working with this field.I hope that helps :).
Cheers!
Thank you.
I am absolutely beginner of Symfony and EasyAdminBundle. It would be nice to reuse the code, but it is not important at the moment for me.
Best regards
How can i perform dependent select
Hey Debasish N.
Could you elaborate a bit more? I don't quite understand what you mean by "dependent select"
Cheers!
Thanks, @Diego Aguiar
I have Category and Subcategory entities. Now on the product creation
page, I need to select category and subcategory. When I select a
category then I want to show the subcategories which belong to the
selected category. How can I do that in easy admin?
Hey Debasish,
Unfortunately, that's not available our of the box. I suppose you can create a custom EasyAdmin filter in this case, and with some AJAX requests it's doable. I'm not sure if you can reuse the exist EasyAdmin filter for the root categories, it still might be tricky, so you may need to implement your custom filter for both root categories and subcategories. Unfortunately, we do. not cover such a complex case in this tutorial, but I think it's possible to do in theory with some custom JS code.
I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!