I´m trying to create a form to manage relations on 2 entities related on a many-to-many way.
These entities are PHONE and GROUP.
Both entities have a composite primary key, ID and USER for GROUP entity and PHONE and USER for PHONE entity, being:
- ID: Numeric autoincremental unique
- PHONE: Numeric
- USER: User ID, linked to another entity not shown.
I'm using composite keys to cluster my indexes on the DB server.
My 2 entities are defined as shown: Phone entity
class Phone
{
/**
* @var integer $phoneNo
*
* @ORM\Column(name="phone_no", type="bigint", nullable=false)
* @ORM\Id
*
*/
private $phoneNo;
/**
* @var Group
*
* @ORM\ManyToMany(targetEntity="Group", inversedBy="phone")
* @ORM\JoinTable(name="phone_has_group",
* joinColumns={
* @ORM\JoinColumn(name="user", referencedColumnName="user"),
* @ORM\JoinColumn(name="phone_no", referencedColumnName="phone_no")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="group_contact", referencedColumnName="id")
* }
* )
*/
private $group;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user", referencedColumnName="id")
* })
* @ORM\Id
*/
private $user;
// more properties, getters and setters...
}
Group entity
class Group
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @var Phone
*
* @ORM\ManyToMany(targetEntity="Phone", mappedBy="group")
*/
private $phone;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user", referencedColumnName="id")
* })
* @ORM\Id
*/
private $user;
// more properties, getters and setters...
}
The relationships on this entities are working just fine. The problem arises when I try to create a Form to manage the relationships between this 2 entities.
My code:
class GroupType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name', 'text',
array('label' => 'Nombre'))
->add('phone', 'entity',
array(
'label' => 'Contactos en este grupo',
'class' => 'SMSBundle:Phone',
'expanded' => false,
'required' => false,
'multiple' => true,
))
;
}
Generates this:
<select id="e_keyword_phone" name="e_keyword[phone][]" multiple="multiple">
<option value="0">4490000000</option>
<option value="1">4490000001</option>
<option value="2">4490000002</option>
<option value="3">4490000003</option>
<option value="4">4490000004</option>
<option value="5">4490000005</option>
</select>
Normally, value attribute on each option tag represents the ID of the entity being chosen... but I'm not using a single-column ID but a 2 column composite key!!!
Here, an arbitrary incremental number is being generated, causing the selected values not being linked to the proper entities, and not being persisted on the database.
How could I make this work without getting rid of my composite keys?