4

我是 Symfony 2 的初学者。

我正在尝试显示一个带有“选择”的表单,查询中的“选项”在哪里。

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

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;
    }
    ...
}

你能帮我吗 ?

4

3 回答 3

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;
}
于 2013-09-27T21:15:40.527 回答
2

你可以使用这个解决方案。它是一个简单的 symfony2文档。我用这个

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

就像在文档中一样:

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

使用这个解决方案,因为它在这种情况下的原生 symfony 解决方案 :)

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

于 2015-08-24T20:09:17.997 回答
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();
于 2017-01-08T00:53:07.483 回答