2014-10-30 11 views
1

두 개의 엔티티 인 이벤트와 도시가 있습니다. 그리고 이벤트 양식 만들기를 구현하고 싶습니다. 필드 중 하나는 데이터베이스 (도시 엔티티)의 값이있는 드롭 다운 목록이어야합니다. 내가 오류가symfony2 doctrine 드롭 다운 메뉴 관련된 엔티티의 선택 항목을 선택하십시오.

$city = $this->getDoctrine() 
    ->getRepository('AtotrukisMainBundle:City') 
    ->findBy(
     array(), 
     array('priority' => 'ASC', 'name' => 'ASC') 
    ); 


$event = new Event(); 

$form = $this->createFormBuilder($event) 
    ->add('name', 'text') 
    ->add('description', 'textarea') 
    ->add('startDate', 'datetime') 
    ->add('endDate', 'datetime') 
    ->add('map', 'text') 
    ->add('city', 'choice', array(
     'choice_list' => new ChoiceList($city->getId(), $city->getName()) 
    )) 
    ->add('save', 'submit', array('label' => 'Sukurti')) 
    ->getForm(); 

$form->handleRequest($request); 

하지만 그와 함께 : 어쨌든 Error: Call to a member function getId() on array in /var/www/src/Atotrukis/MainBundle/Controller/EventController.php line 31

+0

? 왜냐하면 내가 보았 듯이 이것은 교리 관리자가 반환 한 일련의 결과입니다. – Nevertheless

답변

1

이 솔루션이 될 수 :

현재 나는 내 컨트롤러에 있음이

foreach($city as $value) { 
    $id_set[] = $value->getId(); 
    $name_set[] = $value->getName(); 
} 

//... 

->add('city', 'choice', array(
    'choice_list' => new ChoiceList($id_set, $name_set) 
)) 

가 ChoiceList가 배열을 기대하고 있기 때문에 인수로. 배열에 메서드를 사용하려고합니다.

1

컨트롤러에서 벗어난 상태로 유지하는 것이 좋습니다.

->add('approvers', 'entity', array(
    'class' => 'YourBundle:Entity', 
    'query_builder' => function(EntityRepository $er) { 
     return $er->orderBy('name', 'ASC'); // Here you can make some custome query 
    }, 
    'label' => 'label', 
)) 
0

이를 확인하시기 바랍니다 :

당신은`$의 city`를 덤프 할 때 표시되는 내용
->add('usr_role', EntityType::class, array('label' => 'Role : ', 
    'class' => \Ibw\UserBundle\Entity\UserRole::class, 
    'expanded' => false, 'placeholder' => 'Select Role', 
    'multiple' => false))