jla*_*gue 4 symfony doctrine-orm
我是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'))
...
}
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
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".
Run Code Online (Sandbox Code Playgroud)
我明白错误告诉我"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...
}
Run Code Online (Sandbox Code Playgroud)
表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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
你能帮我吗 ?
Ken*_*nel 10
如果您没有设置关系,那么您需要告诉FormBuilder不将其映射到字段.
->add('numero', 'entity', array(
'mapped' => false,
'class' => 'BlocMainBundle:Table2',
'property' => 'numero',
));
Run Code Online (Sandbox Code Playgroud)
要以您想要的方式完成选项(使用选项文本的多个字段),您需要使用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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14750 次 |
最近记录: |