有没有人实现过sonata_type_model_reference表单类型?
我需要链接州和国家的关系,我在一个幻灯片中读到了sonata_type_model_reference可以实现的,但我找不到它的任何文档。
怎么实现呢?或者还有哪些其他选项可以将两个或多个字段与数据库/模型数据相关联/链接?
在今天之前,我使用自定义AJAX来实现这一目标。
客户:
// 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 %}
服务器:
// 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);
}
//...
}
和
// 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));
}
// ...
}
我将尽快实现sonata_type_model引用并返回此处进行编辑。