Symfony 2 - 没有父实体的一个实体的表单集合

时间:2014-11-26 13:41:27

标签: forms symfony

我有表单收集的问题。我想显示一个表单,其中包含来自一个实体的所有值,我希望能够在一个页面上添加或删除此实体的某些记录(行)。我有以下解决方案,没问题。

CurrencyController

class CurrencyController extends Controller {
/**
 * @Template()
 */
public function testAction() {
    $em = $this->getDoctrine()->getManager();

    $currencies = $em->getRepository('MyWebBundle:Currency')->findAll();

    $arr = array('currencies' => $currencies);
    $form = $this->createFormBuilder($arr)
    ->add('currencies', 'collection', array(
    'type' => new CurrencyType(),
                'allow_add'    => true,
                'allow_delete'    => true,
                'by_reference' => false,
    ))
    ->add('submit', 'button', array('label' => 'Odeslat'))
    ->getForm();

    return array(
        'form' => $form->createView(),
    );
}
}

CurrencyType

class CurrencyType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('abbreviation', 'text')
            ->add('rate', 'number')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'My\WebBundle\Entity\Currency'
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'currency';
}
}

枝条

{% extends '::base.html.twig' %}

{% block body -%}
<h1>Test</h1>

{{ form(form) }}

{% endblock %}

如果我使用CurrenciesType的表单类,则Symfony抛出异常

Notice: Object of class My\WebBundle\Entity\Currency could not be converted to int in             
....\web\vendor\symfony\symfony\src\Symfony\Component\
Form\Extension\Core\ChoiceList\ChoiceList.php line 462

此代码如下。

CurrencyController

class CurrencyController extends Controller {
/**
 * @Template()
 */
public function testAction() {
    $em = $this->getDoctrine()->getManager();

    $currencies = $em->getRepository('MyWebBundle:Currency')->findAll();

    $arr = array('currencies' => $currencies);
    $form = $this->createForm(new CurrenciesType(), $arr);

    return array(
        'form' => $form->createView(),
    );
}
}

CurrenciesType

class CurrenciesType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('currencies', 'collection', array(
                'type' => new CurrencyType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
            ))
            ->add('submit', 'button', array('label' => 'Send'))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => null
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'my_webbundle_currencies';
}
}

CurrencyType Twig 与上述相同。

我找到了解决方案这些solution #1 solution #2,但我的symfony仍然如上所述抛出异常,我在解决方案和这些解决方案中看不出异议。请帮助解决这个问题。谢谢大家:))

0 个答案:

没有答案