Reputation: 287
I'm beginner in Symfony 2.
I'm trying to display a form with a "select" where is the "options" from a query.
I put the following code in my form :
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'))
...
}
And I have the following error:
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".
I understand that the error tells me that "numero" is not in the entity Table but I question the entity Table2. I must miss something, but I do not know where ...
My entity definition looks like this : Table 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...
}
Table 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;
}
...
}
Can you help me please ?
Upvotes: 4
Views: 16238
Reputation: 2811
If you need information's table, you could create a constructor in your Form class
in your controller :
$emForm = $this->getDoctrine()->getRepository('RelacionesDoctrineBundle:Adolecente');
$asignatura = new AsignaturasType($emForm);// your form class
in your form class
class AsignaturasType extends AbstractType {
protected $repository;
function __construct($repository)
{
$this->repository = $repository;
}
}
and done! you use it:
$findAdolecente = $this->repository->findAll();
Upvotes: 1
Reputation: 519
you can use this solution. Its a simply and from documentation of symfony2. I m use this
->add('numero', 'entity', array(
'class' => 'BlocMainBundle:Table2',
'choice_label' => 'numero' // MAGIC read next paragraph, it is a private variable
))
Like is in documentation writed:
If the entity object does not have a __toString() method the choice_label option is needed.
Use this solution because its native symfony solution for this situations :)
I hope i will help you or the other people
Upvotes: 2
Reputation: 2748
If you do not have the relationship set then you need to tell the FormBuilder to not map it to a field.
->add('numero', 'entity', array(
'mapped' => false,
'class' => 'BlocMainBundle:Table2',
'property' => 'numero',
));
To accomplish the options the way that you want (using multiple fields for the option text) you need to use a choice
type and build your options list like this:
->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;
}
Upvotes: 10