Symfony2: Sonata Admin: Chain selectors, sonata_type_model_reference

Has anyone ever implemented a sonata_type_model_reference form type?

I need to connect the relationship of state and country, and I read on the slide show that this is possible with sonata_type_model_reference, but I can not find its documentation.

Do you know how to implement it? Or what other option exists for linking / chaining two or more fields to database / model data?

+4
source share
1 answer

Until today, I have used custom AJAX to achieve this.

Customer:

// Override of admin-bundle/Resources/Views/CRUD/base_edit.html.twig {% block javascripts %} {{ parent() }} <script type="text/javascript"> $(document).ready(function() { $('#{{ admin.uniqId }}_parent').change(function() { var parent = $(this); var child = $('#{{ admin.uniqId }}_child'); $.get('/admin/child/get_choices/' + parent.val(), function(data) { child.empty().append(data); }, 'text') .then(function() { var childFirstOption = child.find('option:first'); var childDisplayText = $("#field_widget_{{ admin.uniqId }}_child .select2-chosen"); childFirstOption.attr("selected", true); childDisplayText.text(childFirstOption.text()); }); }); }); </script> {% endblock %} 

Server:

 // src/App/AdminBundle/Controller/ChildAdminController.php class ChildAdminController extends Controller { //... public function getChoicesAction($parent) { $html = ""; $parent = $this->getDoctrine() ->getRepository('AppAdminBundle:Parent') ->find($parent) ; $choices = $parent->getChilds(); foreach($choices as $choice) { $html .= '<option value="' . $choice->getId() . '" >' . $choice->getLabel() . '</option>'; } return new Response($html); } //... } 

and

 // src/App/AdminBundle/Admin/ChildAdmin.php //... use Sonata\AdminBundle\Route\RouteCollection; class ChildAdmin extends Admin { //... protected function configureRoutes(RouteCollection $collection) { $collection->add('get_choices', 'get_choices/{parent}', array(), array(), array('expose' => true)); } // ... } 

I will try to implement sonata_type_model_reference soon and will return here for editing.

0
source

Source: https://habr.com/ru/post/1439178/


All Articles