Reputation: 1505
I'm trying to create a for with an entity.
My controller:
$questionnaire = $em->getRepository('questionnaireBundle:questionnaire')->findOneBy(array('id' => $id));
$form = $this->createForm(new questionnaireType(), $questionnaire);
QuestionnaireType:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('nom', 'text', array('label' => 'Nom:'));
$builder->add('nbreQuestions', 'text', array('label' => 'Nombre de questions:'));
$builder->add('type', 'entity', array('class' => 'questionnaireBundle:type', 'property' => 'type'));
$builder->add('envoyer', 'submit', array('label' => 'Enregistrer', 'attr' => array('class' => 'btn btn-success col-md-12')));
}
The entity: questionnaire
namespace questionnaireBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* questionnaire
*
* @ORM\Table()
* @ORM\Entity
*/
class questionnaire {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var integer
*
* @ORM\Column(name="nbreQuestions", type="integer")
*
* @Assert\NotBlank(message="Veuillez entrer un nombre de questions.")
* @Assert\Type(type="integer")
*/
private $nbreQuestions;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return questionnaire
*/
public function setNom($nom) {
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom() {
return $this->nom;
}
/**
* Set type
*
* @param string $type
* @return type
*/
public function setType($type) {
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType() {
return $this->type;
}
/**
* Set nbreQuestions
*
* @param integer $nbreQuestions
* @return questionnaire
*/
public function setNbreQuestions($nbreQuestions) {
$this->nbreQuestions = $nbreQuestions;
return $this;
}
/**
* Get nbreQuestions
*
* @return integer
*/
public function getNbreQuestions() {
return $this->nbreQuestions;
}
public function __toString() {
return (string) $this->getNom();
}
}
The field nom, nbreQuestions will automatically be fully with the entity questionnaire, but type is not update!
Anybody knows why?
Thanks Best regards
Upvotes: 1
Views: 214
Reputation: 803
The reason of not selecting the proper type in your for is that entity type builds choices as
entity_id => property_value
So it is comparing your values from questionnaire type to the ids from your type entity and it will not find a match. Moreover if you update your questionnaire object the type will be changed to the id of type entity.
I see here two solutions:
Upvotes: 0
Reputation: 1629
that must do a second query behind the sense, but you can increase performance by dql & join:
use this code:
$questionnaire = $em->createQuery('select q from questionnaireBundle:questionnaire q join q.type t where q.id = :id')->setParameter('id', $id)->getSingleResult();
$form = $this->createForm(new questionnaireType(), $questionnaire);
Upvotes: 1