Devo aggiungere un questionario di domande a scelta multipla al modulo di registrazione. Le domande e le opzioni sono in due entità:Compilare questionario con entità utente nel modulo - symfony2
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Question
*
* @ORM\Table(name="question")
* @ORM\Entity(repositoryClass="Me\UserBundle\Entity\QuestionRepository")
*/
class Question
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="questionText", type="text")
*/
private $questionText;
/**
* @var boolean $expanded
*
* @ORM\Column(name="expanded", type="boolean")
*/
private $expanded;
/**
* @var boolean $multiple
*
* @ORM\Column(name="multiple", type="boolean")
*/
private $multiple;
/**
* @var Questionnaire $questionnaire
*
* @ORM\ManyToOne(targetEntity="Questionnaire", inversedBy="questions")
* @ORM\JoinColumn(name="questionnaire", referencedColumnName="id", onDelete="cascade")
*/
private $questionnaire;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $options
*
* @ORM\OneToMany(targetEntity="Option", mappedBy="question", cascade={"all"})
*/
private $options;
public function __construct()
{
$this->expanded = false;
$this->multiple = false;
$this->options = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set questionText
*
* @param string $questionText
* @return Question
*/
public function setQuestionText($questionText)
{
$this->questionText = $questionText;
return $this;
}
/**
* Get questionText
*
* @return string
*/
public function getQuestionText()
{
return $this->questionText;
}
/**
* @param mixed $options
*/
public function setOptions($options)
{
$this->options[] = $options;
return $this;
}
/**
* @return mixed
*/
public function getOptions()
{
return $this->options;
}
function __toString()
{
return $this->getQuestionText();
}
/**
* @param boolean $expanded
*/
public function setExpanded($expanded)
{
$this->expanded = $expanded;
}
/**
* @return boolean
*/
public function getExpanded()
{
return $this->expanded;
}
/**
* @param boolean $multiple
*/
public function setMultiple($multiple)
{
$this->multiple = $multiple;
}
/**
* @return boolean
*/
public function getMultiple()
{
return $this->multiple;
}
/**
* @param \Me\UserBundle\Entity\Questionnaire $questionnaire
*/
public function setQuestionnaire($questionnaire)
{
$this->questionnaire = $questionnaire;
}
/**
* @return \Me\UserBundle\Entity\Questionnaire
*/
public function getQuestionnaire()
{
return $this->questionnaire;
}
}
e
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* QuestionOption
*
* @ORM\Table(name="option")
* @ORM\Entity(repositoryClass="Me\UserBundle\Entity\OptionRepository")
*/
class Option
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="questionId", type="integer")
*/
private $questionId;
/**
* @var string
*
* @ORM\Column(name="optionText", type="string", length=255)
*/
private $optionText;
/**
* @ORM\ManyToOne(targetEntity="Question", inversedBy="options")
* @ORM\JoinColumn(name="questionId", referencedColumnName="id", onDelete="cascade")
**/
private $question;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set optionText
*
* @param string $optionText
* @return Option
*/
public function setOptionText($optionText)
{
$this->optionText = $optionText;
return $this;
}
/**
* Get optionText
*
* @return string
*/
public function getOptionText()
{
return $this->optionText;
}
/**
* @return mixed
*/
public function getQuestion()
{
return $this->question;
}
/**
* @param mixed $question
*/
public function setQuestion($question)
{
$this->question = $question;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
function __toString()
{
return $this->getOptionText();
}
}
Ho anche un'entità questionario, anche se non credo che ho veramente bisogno, perché gli utenti non sarà la creazione di questionari, solo compilando il questionario unico durante la registrazione.
La mia impresa utilizzatrice:
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="Me\UserBundle\Entity\UserRepository")
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="firstName", type="string", length=50)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="middleInitial", type="string", length=50)
*/
private $middleInitial;
/**
* @var string
*
* @ORM\Column(name="lastName", type="string", length=50)
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="homePhoneArea", type="string", length=3)
*/
private $homePhoneArea;
/**
* @var string
*
* @ORM\Column(name="homePhoneNumber", type="string", length=7)
*/
private $homePhoneNumber;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=50)
*/
private $email;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*/
public $questions;
public function __construct()
{
$this->questions = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set middleInitial
*
* @param string $middleInitial
* @return User
*/
public function setMiddleInitial($middleInitial)
{
$this->middleInitial = $middleInitial;
return $this;
}
/**
* Get middleInitial
*
* @return string
*/
public function getMiddleInitial()
{
return $this->middleInitial;
}
/**
* Set lastName
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set homePhoneArea
*
* @param string $homePhoneArea
* @return User
*/
public function setHomePhoneArea($homePhoneArea)
{
$this->homePhoneArea = $homePhoneArea;
return $this;
}
/**
* Get homePhoneArea
*
* @return string
*/
public function getHomePhoneArea()
{
return $this->homePhoneArea;
}
/**
* Set homePhoneNumber
*
* @param string $homePhoneNumber
* @return User
*/
public function setHomePhoneNumber($homePhoneNumber)
{
$this->homePhoneNumber = $homePhoneNumber;
return $this;
}
/**
* Get homePhoneNumber
*
* @return string
*/
public function getHomePhoneNumber()
{
return $this->homePhoneNumber;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getQuestions()
{
return $this->questions;
}
}
mio controllo utente:
public function newAction()
{
$user = new User();
$em = $this->getDoctrine()->getManager();
$questions = $em->getRepository('MeUserBundle:Question')->findAll();
if (!$questions) {
throw $this->createNotFoundException('Unable to find Questions.');
}
$builder = $this->createFormBuilder();
$optionEntities = array();
foreach ($questions as $question)
{
$options = array();
foreach ($question->getOptions() as $option)
{
$options[$option->getId()] = $option->getOptionText();
$optionEntities[$option->getId()] = $option;
}
$builder->add('question_'. $question->getId(), 'choice', array(
'label' => $question->getQuestionText(),
'expanded' => $question->getExpanded(),
'multiple' => $question->getMultiple(),
'choices' => $options
));
}
$user->getQuestions()->add($questions);
$form = $this->createForm(new MyFormType(), array('User' => $user));
return $this->render('MeUserBundle:User:new.html.twig', array(
'entity' => $user,
'form' => $form->createView(),
));
}
Il tipo di modulo così com'è oggi:
<?php
namespace Me\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyFormType extends AbstractType
{
protected $questions;
public function __construct($questions)
{
$this->questions = $questions;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('questions', 'collection', array(
'type' => new QuestionType()
))
->add('firstName')
->add('middleInitial')
->add('lastName')
->add('homePhoneArea')
->add('homePhoneNumber')
->add('email')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
));
}
public function getName()
{
return 'myform_type';
}
}
Questa impostazione non funziona per ottenere il domande e le relative opzioni associate e visualizzarle nello stesso modulo di creazione utente. Ho visto istruzioni e documenti per combinare moduli, ma non creare moduli con questo tipo di configurazione. Qualsiasi guida sarebbe apprezzata.
solo per chiarire, fa uso e domanda sono collegati con qualsiasi tipo di relazione? e, puoi pubblicare il tuo modulo MyFormType? –
Dove vuoi memorizzare le risposte? –
Mostraci la tua Entità Utente e il tuo Modulo MyFormType –