1

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

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

[Semantical Error] line 0, col 62 near 't where q.id': Error: Class questionnaireBundle\Entity\questionnaire has no association named type
I no have your entity mapping, I guess that question entity contain type property that is a relation to another entity. Plz add your mapping here.
Right! I have added this in my question!
@anubis why type property is string? and you set that in form as a object?
I have a select and the user have to choose between 3 strings ("One", "two", "three") for example
|
0

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:

  1. proposed by @ghanbari to make relation from questionnaire to type
  2. make your form as a service and inject entityManager than build your choices like you want (type => type) and change the field from entity type to choice type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.