Symfony2 From Collection没有元素

时间:2014-11-14 10:09:52

标签: php symfony

我正在尝试实施表单集合。我有会员,每个会员可以有多个会员资格。 " createForm()"的给定成员对象有一个数组,其成员资格可以从数据库中正确获取。成员表单获取显示正确,但似乎form.memberships为空。它甚至不会渲染"测试"。

控制器

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) }}

1 个答案:

答案 0 :(得分:0)

即使我不使用Doctrine,我也不得不使用arrayCollection()而不是数组作为成员资格。