Let's start with the background.
I need a user profile that will have basic information such as name, email address, phone, etc. And for this I have a user.
I also need to save the answers from the questionnaire. I thought to store them in the database as json in a text box. These questions may change in the future, there are currently ~ 30 questions, so I do not want to store it as an entity. So, currently in my User object, I have the following:
/** * @var array * * @ORM\Column(name="questionnaire", type="json_array", nullable=true) */ private $questionnaire;
I understand that Symfony will take care of json_encode / json_decode. So this is great.
But now I have a little problem with creating a form with symfony creator.
At first I thought I could try something like this:
$builder->add('questionnaire[source]');
What does not work. For symfony masters, this is obvious, I know; o)
So, currently I see my options: CollectionType or Data Transformers.
From what I see, CollectionType will not work, since it only works with numeric arrays, where we have a field with some JS βAdd another lineβ or something like that. http://symfony.com/doc/current/reference/forms/types/collection.html#adding-and-removing-items But if I am wrong about this and I have to go with CollectionType, there is some magic way , please, say Me. I can not find much about it.
So, I was thinking about Data Transformers or just creating an array to send without this Transformer thing. Create all the fields in the Questionnaire using "mapped => false", and then set these values ββas an associative array in $ questionnaire. It "feels" good, but I'm not sure how to process it later in the "Edit" form (from the documents I think with http://symfony.com/doc/current/reference/forms/types/form.html #data ).
In the questionnaire itself there will be many ChoiceType, CollectionType fields with "Add more rows", etc., and he will have many questions. So it will be a little difficult. I would like to avoid creating an object for this with every question as a property (not sure if it is the right choice, but considering everything that I consider to be the best,).
This is my first date with symfony, so any help / advice is appreciated.