Reputation: 646
I have 2 entities:
1. Question
/**
* @ORM\Table()
* @ORM\Entity
*/
class Question
{
/**
* @ORM\Column(type="smallint", nullable=false)
*/
private $type;
/**
* @ORM\Column(type="string", length=255)
*/
private $test_field;
/**
* @ORM\OneToMany(targetEntity="Option", mappedBy="question")
*/
private $options;
}
2. Option
/**
* @ORM\Table()
* @ORM\Entity
*/
class Option
{
/**
* @ORM\ManyToOne(targetEntity="Question", inversedBy="options")
* @ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
private $question;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $field_for_question_type_x;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $field_for_question_type_y;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $field_for_question_type_z;
}
Based on the Question
type there needs to be different formType used which contains only some of the Option
fields.
For example:
Question
with type = X
should have form with fields $field_for_question_type_x
Question
with type = Y
should have form with fields $field_for_question_type_y
etc.
I also have those different formTypes created so the question is how could I tell Sonata Admin $formMapper
to add collection of such forms(based on Question entity type)?
Currently it looks like this:
protected function configureFormFields(FormMapper $formMapper)
{
$Question = $this->getSubject();
$formMapper->add('test_field');
if ($Question->getId()) {
$formMapper->with('Options');
switch ($Question->getType()) {
case 'X':
// Here I would need to add collection of \My\Bundle\Form\OptionXType()
$formMapper->add('options', ????????);
break;
case 'Y':
// Here I would need to add collection of \My\Bundle\Form\OptionYType()
$formMapper->add('options', ????????);
break;
}
}
}
What should be added to provide such functionality?
Upvotes: 1
Views: 2535
Reputation: 646
Answer was quite simple:
->add('options', 'sonata_type_collection',
array(),
array(
'edit' => 'inline',
'inline' => 'table',
'admin_code' => $type_based_admin_code # each has custom form configuration
)
)
Upvotes: 1