[英]symfony2 many-to-many sonata admin model_list
I am using latest Symfony2 and Sonata Admin to maintain my site and this is my problem: 我使用最新的Symfony2和Sonata Admin来维护我的网站,这是我的问题:
I have two entities: Shop and Discount. 我有两个实体:商店和折扣。 One shop can have many discounts and one discount can be assigned to many shops. 一家商店可以享受很多折扣,并且可以为许多商店分配一个折扣。 Therefore it should be Many to Many relation. 因此它应该是多对多的关系。
I would like to use sonatata`s type_model_list in ShopAdmin so I can choose from a popup window these discounts and select multiple. 我想在ShopAdmin中使用sonatata的type_model_list,这样我就可以从弹出窗口中选择这些折扣并选择多个。 Is this possible? 这可能吗?
This is part of my Shop Entity: 这是我的商店实体的一部分:
use Doctrine\Common\Collections\ArrayCollection;
...
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\ManyToMany(targetEntity="ShoppingFever\ShoppingFeverBundle\Entity\Discount", fetch="EAGER")
* @ORM\JoinColumn(name="discountId", referencedColumnName="id")
*/
private $discountId;
And this is the relative ShopAdmin of function configureFormFields: 这是函数configureFormFields的相对ShopAdmin:
$formMapper
->add('shopName',null, array('label' => 'Název obchodu'))
->add('brandName',null, array('label' => 'Název brandu'))
->add('discountId', 'sonata_type_model_list', array(
'btn_add' => 'Add discount', //Specify a custom label
'btn_list' => 'button.list', //which will be translated
'btn_delete' => false, //or hide the button.
'btn_catalogue' => 'SonataNewsBundle' //Custom translation domain for buttons
), array(
'placeholder' => 'Nothing selected',
'expanded' => true, 'multiple' => true, 'by_reference' => false
))
->add('street',null, array('label' => 'Ulice'))
->add('city',null, array('label' => 'Město'))
->add('zip',null, array('label' => 'PSČ'))
->add('gps',null, array('label' => 'GPS'))
->add('openingHours','textarea', array('label' => 'Otevírací doba'))
->add('eventId',null, array('required'=>false,'label' => 'Event'));
If I had One To Many relation (one discount to any shop), the admin works. 如果我有一对多关系(任何商店一折),管理员工作。 Symfony generated the reference table for discount and shop ids to work for many to many . Symfony生成折扣和商店ID的参考表,可以为多对多工作。
This is the One To Many output and I would like this to work for Many To Many, so where now it says Swarovski, there would be several discounts. 这是一对多的输出,我希望这适用于多对多,所以现在它说施华洛世奇,会有几个折扣。
Is it possible to do it with many to many as well? 是否有可能与许多人一起做?
You can't use sonata_type_model_list
with a many-to-many relation. 您不能将sonata_type_model_list
与多对多关系一起使用。
What you can do is to use sonata_type_model
instead and set the option multiple
to true
: 你可以做的是改用sonata_type_model
并将选项multiple
设置为true
:
->add('discountId', 'sonata_type_model', array(
'multiple' => true,
// other options
))
For the current version of SonataAdminBundle (as of today 2019-02-18 it is 3.x) the example should look like: 对于当前版本的SonataAdminBundle (截至今日2019-02-18它是3.x),示例应如下所示:
// import ModelType
use Sonata\AdminBundle\Form\Type\ModelType;
// code example
->add('discountId', ModelType::class, [
'multiple' => true,
// other options
])
This will create a dropdown list to select multiple related objects and Add new button (or Add discount if you set that trough btn_add
) to create a new related object. 这将创建一个下拉列表来选择多个相关对象和添加新按钮(如果您设置了低谷btn_add
,则添加折扣 )以创建新的相关对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.