2015-01-13 4 views
3

데이터베이스의 데이터를 기반으로 체크 박스를 기본적으로 선택하도록하는 방법은 무엇입니까?
지금 내 양식은 다음과 같습니다Symfony 2.6이 기본적으로 선택 함 체크 박스

 ... 
     ->add(
     "role", "entity", [ 
      "class" => "AppDefaultBundle:OptionRole", 
      "required" => false, 
      "label" => "Roles for user: ", 
      "property" => "name", 
      "expanded" => true, 
      "multiple" => true 
     ] 
    ) 
     ... 

그리고 다른 테이블의 데이터를 기반으로이 확인란에 대한 기본값을 선택합니다.

+0

내가의 리스너를 사용하는 것입니다 알고 폼 타입 내부에서 개체에 액세스하기위한 유일한 아파트형 방법

[ 'label' => 'Select Modules', 'class' => 'Foo\BarBundle\Entity\Module', 'choices' => $this->availableModules(), 'property' => 'name', 'multiple' => true, 'expanded' => true ] 

... : HTTP 여기

한 예이다 : //symfony.com/doc/current/cookbook/form/dynamic_form_modification.html – Cerad

답변

2

당신은 아마 선택 속성을 추가해야합니다 : (? 사용자) 당신이에 관련된 모든 OptionRoles 배열을해야한다 귀하의 경우에는 http://symfony.com/doc/current/reference/forms/types/choice.html#choices

을 실체는 (사람은 당신이 양식을 작성)에 노력하고 있습니다.

doctrine 사용자 모델이 OptionRoles (대개 ManyToMany 연관)임을 알고 있다고 가정하면 양식은 자동으로 사용자 OptionRoles의 확인란을 선택해야합니다.

public function availableModules() 
{ 
    return $this->get('doctrine') 
    ->getManager() 
    ->getRepository('Foo\BarBundle\Entity\Module') 
    ->findAll(); 
} 
관련 문제