Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Form select options database

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 ?

like image 288
jlafforgue Avatar asked Dec 11 '22 11:12

jlafforgue


1 Answers

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;
}
like image 164
Ken Hannel Avatar answered Dec 20 '22 13:12

Ken Hannel