2014-11-14 70 views
0

我試圖實現表單集合。我有會員,每個會員可以有多個會員。 「createForm()」的給定成員對象具有一個包含從數據庫中正確提取的成員關係的數組。成員表單得到顯示正確,但似乎form.memberships是空的。它甚至不提供「測試」。Symfony2從集合沒有元素

控制器

public function memberAction($id) { 
    $request = Request::createFromGlobals(); 
    $user_provider = $this->get('webservice_user_provider'); 

    if ($id === "new") { 
     $member = new WebserviceUser(); 
     $name = "Neues Mitlgied"; 
    } else if (intval($id) != 0) { 
     $member = $user_provider->getUserById(intval($id)); 
     $member->getMemberships(); 
     $name = $member->getName() . " " . $member->getLastname(); 
    } else { 
     throw new NotFoundHttpException('No Member found!'); 
    } 

    $form = $this->createForm(new MemberType($this->get('webservice_config_provider')), $member); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     return $this->redirect($this->generateUrl('bow_exciting_fit_member', array('id' => $user_provider->persistMember($form->getViewData())))); 
    } 

    return $this->render('BOWExcitingFitBundle:Default:member.html.twig', array(     
       "member" => $member, 
       "form" => $form->createView() 
    )); 
} 

MemberType

class MemberType extends AbstractType { 

function __construct(\BOW\ExcitingFitBundle\Webservice\WebserviceConfigProvider $config) { 
    $this->config = $config; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) {  

    ... MORE CODE ... 

    $builder->add('memberships', 'collection', array(
     'type' => new MembershipType($this->config), 
     'by_reference' => false, 
     'allow_add' => true)); 

    ... MORE CODE ... 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) { 
    $resolver->setDefaults(array(
     'data_class' => 'BOW\ExcitingFitBundle\Webservice\WebserviceUser', 
    )); 
} 

public function getName() { 
    return 'member'; 
} 

} 

MembershipType

class MembershipType extends AbstractType { 

function __construct(\BOW\ExcitingFitBundle\Webservice\WebserviceConfigProvider $config) { 
    $this->config = $config; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) { 

    ... MORE CODE ... 

    $builder->add('id', 'hidden'); 
    $builder->add('user', 'hidden'); 

    ... MORE CODE ... 

} 

public function setDefaultOptions(OptionsResolverInterface $resolver) { 
    $resolver->setDefaults(array(
     'data_class' => 'BOW\ExcitingFitBundle\Webservice\WebserviceMembership', 
    )); 
} 

public function getName() { 
    return 'membership'; 
} 

} 

模板

{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} 
    <div class="ui divided list memberships" data-prototype="{{ include('BOWExcitingFitBundle:Form:membership-prototype.html.twig', { 'form': form.memberships.vars.prototype })|e }}">  
    {% for membership in form.memberships %} 
     Test 
     {{ include('BOWExcitingFitBundle:Form:membership-prototype.html.twig', { 'form': membership }) }} 
    {% endfor %} 
    </div>    
{{ form_end(form) }} 

回答

0

即使我不使用學說,我不得不使用一個ArrayCollection(),而不是爲成員的數組。