I have 3 entities:
[Member] ----OneToMany----> [MemberCategory] ---ManyToOne---> [Category]
This works well as far as fetching results from the database, but I can't get the Form Builder to build a correct form controls.
I want a list of all categories with checkboxes that are checked for those categories that are used by the member. Eventually I want to add the priority field.
Member
class Member
{
protected $id;
@ORM\OneToMany(targetEntity="MemberCategory", mappedBy="member")
protected $categories;
}
Category
class Category
{
protected $id;
@ORM\Column(name="category_name", type="string", length=50, nullable=false, unique=true)
private $categoryName;
}
MemberCategory
class MemberCategory
{
@ORM\Id
@ORM\ManyToOne(targetEntity="Member")
@ORM\JoinColumns({
@ORM\JoinColumn(name="member_id", referencedColumnName="id", onDelete="CASCADE")
private $member;
@ORM\Id
@ORM\ManyToOne(targetEntity="Category")
@ORM\JoinColumns({
@ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE")
private $category;
@ORM\Column(name="priority", type="integer", nullable=true)
protected $priority;
}
Obvious attempts with the form builder that don't work.
If I use:
$builder->add('categories', 'entity', array(
'class' => 'SMWMemberBundle:Category',
'property' => 'categoryName',
'multiple' => true,
'expanded' => true,
'required' => false
));
I get a select with all the categories, but none of those selected in MemberCategory for this member.
If I use:
$builder->add('categories', 'entity', array(
'class' => 'SMWMemberBundle:MemberCategory',
'property' => 'category.categoryName',
'multiple' => true,
'expanded' => true,
'required' => false
));
I get all selected categories for all users.
Does anyone know how to get this to work, this is an obvious common pattern in relational data and would be easy using SQL and PHP.
Is there a straight forward solution in Symfony 2.3 and Doctrine?