I have three entities, Block, BlockPlacement, BlockPosition:
class BlockEntity
{
private $bid;
/**
* @ORM\OneToMany(
* targetEntity="BlockPlacementEntity",
* mappedBy="block",
* cascade={"remove"})
*/
private $placements;
}
class BlockPlacementEntity
{
/**
* The id of the block postion
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="BlockPositionEntity", inversedBy="placements")
* @ORM\JoinColumn(name="pid", referencedColumnName="pid", nullable=false)
*/
private $position;
/**
* The id of the block
*
* @var BlockEntity
* @ORM\Id
* @ORM\ManyToOne(targetEntity="BlockEntity", inversedBy="placements")
* @ORM\JoinColumn(name="bid", referencedColumnName="bid", nullable=false)
*/
private $block;
private $sortorder;
}
class BlockPositionEntity
{
private $pid;
/**
* @ORM\OneToMany(
* targetEntity="BlockPlacementEntity",
* mappedBy="position",
* cascade={"remove"})
* @ORM\OrderBy({"sortorder" = "ASC"})
*/
private $placements;
}
So, you can see the relationship: Block < OneToMany > Placement < ManyToOne > Position.
Now I am trying to construct a form to create/edit a block:
$builder
->add($builder->create('placements', 'entity', [
'class' => 'Zikula\BlocksModule\Entity\BlockPositionEntity',
'choice_label' => 'name',
'multiple' => true,
'required' => false
]))
;
This gives me a good select box with multiple selections possible with a proper list of positions to choose from. But it does not show previous selections for placement (I am using existing data) e.g. marking positions as 'selected'. I have not tried creating a new Block yet, only editing existing data.
I suspect I will need to be using addModelTransformer()
or addViewTransformer()
but have tried some of this an cannot get it to work.
I've looked at the collection
form type and I don't like that solution because it isn't a multi-select box. It requires JS and isn't as intuitive as a simple select element.
This seems like such a common issue for people. I've searched and found no common answer and nothing that helps.