简体   繁体   English

sonata_type_model中的默认数据

[英]default data in sonata_type_model

I work with symfony 2.7 , and I use SonataAdminBundle. 我使用symfony 2.7并使用SonataAdminBundle。

I have 2 entities called (Produit) and (Correspondant) with OneToMany relation, One Produit can have Many Correspondant. 我有2个具有OneToMany关系的实体,分别称为(Produit)和(Correspondant),一个Produit可以具有Many Correspondant。 in the create form for the (Produit) I have correspondants to add Many (Correspondant), and I like by default add all the Correspondants, for that I tried to do this : 在(产品)的创建表单中,我具有添加许多(通讯员)的通讯员,并且默认情况下,我喜欢添加所有通讯员,为此,我尝试这样做:

ProduitAdmin 产品管理员

 $query = $this->modelManager->getEntityManager(new User())->createQuery("SELECT s FROM UserBundle\Entity\User s WHERE s.type LIKE 'Correspondant'" );
 $formMapper 
      ->add('correspondants','sonata_type_model', array(
             'class'=>'Devagnos\UserBundle\Entity\User',
             'multiple'=> true,
             'by_reference' => false,
            'label'=>'Correspondants associés',

            'data' =>  function() {
                $data = new ArrayCollection();
                $r= $query->getResult();

                foreach($r as $result) {

                   $data->add($result->getId());
              }
                return $data;
            },
              'query' => $query ),
           )

But this does not work, 但这行不通

Someone can help me please ? 有人可以帮我吗? thanks 谢谢

You need to set data attribute and put the entities you want to be selected as default. 您需要设置data属性,并将要选择的实体作为默认值。

$selected = ... //fetch entities, e.g. from repository
$formMapper
    ->add('field', 'sonata_type_model', array(
        'your_settings' => '...',
        'query' => '...',
        'data' => $selected
        )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM