4

Has anyone ever implemented sonata_type_model_reference form type?

I need to chain state and country relations, and I read on an slideshare that that is possible with sonata_type_model_reference, but I can't find any documentation of it.

How to implement it? Or what other option is there to relate/chain two or more fields with database/model data?

Cœur
  • 37,241
  • 25
  • 195
  • 267
edrian
  • 4,501
  • 5
  • 31
  • 35
  • What does you mean by "chain" two relations ? Do you want update options of a field corresponding to a related field value ? Example : We have one listbox with categories, a second listbox with subcategories, and a oneToMany from the first to the second. On choose a category, we update the available options of the subcategories listbox, using only entries where has category = chosen. Is it what you want ? – chalasr Jan 13 '16 at 15:11
  • @chalasr Yes, that was what I wanted, but it was long time ago and indeed we discontinued that project. But if you have a solution feel free to contribute with your answer! – edrian Jan 13 '16 at 18:08

1 Answers1

0

Before today, I used custom AJAX to achieve this.

Client :

// 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 come back here for edits.

chalasr
  • 12,971
  • 4
  • 40
  • 82