Symfony 2 - 表单选择选项数据库

时间:2013-09-27 20:31:33

标签: symfony doctrine-orm

我是Symfony 2的初学者。

我正在尝试显示带有“select”的表单,其中是查询中的“选项”。

我将以下代码放在我的表单中:

use Doctrine\ORM\EntityRepository;
use Bloc\MainBundle\Entity\Table;
use Bloc\MainBundle\Entity\Table2;

public function addAction(Request $request)
{
    $table = new Table();
    $form = $this->createFormBuilder($table , array('attr' => array('role' => 'form')))
        ->add('num', 'integer', array('label' => 'Numéro', 'attr' => array('class' => 'form-control')))
        ->add('nom_emetteur', 'text', array('label' => 'Emetteur', 'attr' => array('class' => 'form-control')))
        ->add('numero', 'entity', array('class' => 'BlocMainBundle:Table2', 'property' => 'numero'))
        ...
}

我有以下错误:

Neither the property "numero" nor one of the methods "getNumero()", "isNumero()", "hasNumero()", "__get()" or "__call()" exist and have public access in class "Bloc\MainBundle\Entity\Table". 

我理解错误告诉我“numero”不在实体表中,但我质疑实体Table2。 我必须错过一些东西,但我不知道在哪里......

我的实体定义如下: 表1:

<?php...
class Table
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="num", type="integer")
     */
    private $num;

    //Getter and setter...
}

表2

<?php

namespace Bloc\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Fournisseur
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Bloc\MainBundle\Entity\Table2Repository")
 */
class Table2
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="numero", type="integer")
     */
    private $numero;

    /**
     * Set numero
     *
     * @param integer $numero
     * @return Fournisseur
     */
    public function setNumero($numero)
    {
        $this->numero = $numero;

        return $this;
    }

    /**
     * Get numero
     *
     * @return integer 
     */
    public function getNumero()
    {
        return $this->numero;
    }
    ...
}

你能帮我吗?

3 个答案:

答案 0 :(得分:10)

如果您没有设置关系,那么您需要告诉FormBuilder不要将其映射到字段。

->add('numero', 'entity', array(
    'mapped'   => false, 
    'class'    => 'BlocMainBundle:Table2',
    'property' => 'numero',
));

要以您想要的方式完成选项(使用选项文本的多个字段),您需要使用choice类型并构建您的选项列表,如下所示:

->add('numero', 'choice', array(
    'mapped'  => false,
    'choices' => $this->buildChoices()
));

protected function buildChoices() {
    $choices          = [];
    $table2Repository = $this->getDoctrine()->getRepository('BlocMainBundle:Table2');
    $table2Objects    = $table2Repository->findAll();

    foreach ($table2Objects as $table2Obj) {
        $choices[$table2Obj->getId()] = $table2Obj->getNumero() . ' - ' . $table2Obj->getName();
    }

    return $choices;
}

答案 1 :(得分:2)

您可以使用此解决方案。它简单而来自documentation的symfony2。我用这个

->add('numero', 'entity', array(
            'class' => 'BlocMainBundle:Table2',
            'choice_label' => 'numero' // MAGIC read next paragraph, it is a private variable
        ))

就像在文件中写的那样:

  

如果实体对象没有__toString()方法,则需要choice_label选项。

使用此解决方案,因为它的本机symfony解决方案适用于这种情况:)

我希望我会帮助你或其他人

答案 2 :(得分:1)

如果您需要信息表,可以在Form class

中创建构造函数

在您的控制器中:

$emForm = $this->getDoctrine()->getRepository('RelacionesDoctrineBundle:Adolecente');

$asignatura = new AsignaturasType($emForm);// your form class

在您的表单类

class AsignaturasType extends AbstractType {

      protected $repository;

          function __construct($repository)
             {
               $this->repository = $repository;
             }
}

完成了!你用它:

  $findAdolecente    = $this->repository->findAll();