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?
The post of cheesemacfly is not so bad, but it is call embeded form and can be complicated to manage. In fact, you jus need to create a "Custom Repositories" http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
It will add a new methods for "grabbing" your ORM object with Doctrine2 like find() or findBy();
1) Create a new repository in a Repository Folder of your Bundle
}
2) Attached your Cutom repository to your Entity
3) add a constructor to your Form class and pass de Entity Manager and the variable required to your query :
4) In your controller, you create your form like that :
Feel free to ask me questions, I'm hope this is what you are looking for ;)
You will need to use the querybuilder inside your form
See documentation for proper use. see symfony2 form querybuilder with parameters for an exmple
This is how I resolved this problem in the past (applied to your example) but this is what I figured out so it might not be 100% right for your case.
First create a
MemberCategory
form type matching your needs:Then add this form type inside your
Member
type form:Then you can follow the documentation to add as many categories as you want to your members and assigning a priority every time.