簡體   English   中英

sonata_type_model中的默認數據

[英]default data in sonata_type_model

我使用symfony 2.7並使用SonataAdminBundle。

我有2個具有OneToMany關系的實體,分別稱為(Produit)和(Correspondant),一個Produit可以具有Many Correspondant。 在(產品)的創建表單中,我具有添加許多(通訊員)的通訊員,並且默認情況下,我喜歡添加所有通訊員,為此,我嘗試這樣做:

產品管理員

 $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 ),
           )

但這行不通

有人可以幫我嗎? 謝謝

您需要設置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