DDC-3354: Replacing indexed item on association with indexBy cannot comply with unicity constraint
Jira issue originally created by user cblegare:
Using a bidirectional one-to-many relation having an 'indexBy' clause on the owning side and a unique constraint over the indexed field and the 'mappedBy' field of the inverse side, replaced indexed fields being inserted before removed violated the unique constraint.
As stated in the code examples below, if done without the unique constraint, when replacing an indexed field, the row is replaced in the database (new one created, old one deleted).
Here is an example:
Person:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
oneToMany:
emailAddresses:
targetEntity: Email
indexBy: name
mappedBy: owner
cascade: [ all ]
orphanRemoval: true
Email:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
nullable: false
address:
type: string
nullable: false
uniqueConstraints:
unique*named_person*email:
columns: [ owner_id, name ]
manyToOne:
owner:
targetEntity: Person
inversedBy: emailAddresses
joinColumn:
name: owner_id
referencedColumnName: idclass Person {
protected $id;
/****
* @var Collection|Email[] $emailAddresses
*/
protected $emailAddresses;
/****
* I would expect this to work.
*/
public function setEmailAddress_expected($name, $emailAddress)
{
/****
* Expected to work but throws UniqueConstraintViolationException
* If done without the 'unique*named_person*email' constraint', it
* works properly creating a new Email and deleting the old one.
*/
$this->emailAddresses->set(
(string) $name,
new Email($this, (string) $name, (string) $emailAddress)
);
}
/****
* I would expect this to work.
*/
public function setEmailAddress*expected*too($name, $emailAddress)
{
/****
* Expected to work but throws UniqueConstraintViolationException
* If done without the 'unique*named_person*email' constraint, it
* works properly creating a new Email and deleting the old one.
*/
$this->emailAddresses->remove((string) $name);
$this->emailAddresses->set(
(string) $name,
new Email($this, (string) $name, (string) $emailAddress)
);
}
/****
* Works
*/
public function setEmailAddress_works($name, $emailAddress)
{
$existing = $this->emailAddresses->get((string) $name);
if ($existing) {
$existing->setAddress((string) $emailAddress);
} else {
$this->emailAddresses->set(
(string) $name,
new Email($this, (string) $name, (string) $emailAddress)
);
}
}
}
class Email {
protected $id;
protected $owner;
protected $name;
protected $address;
public function **construct($owner, $name, $address)
{
$this->owner = $owner;
$this->name = $name;
$this->address = $address;
}
/****
* I am forced to create this method.
*/
public function setAddress($address)
{
$this->address = $address;
}
}IMO, Using 'indexBy' on a one-to-many relation should automatically generate a unique constraint for the combination of the indexed field and the 'mappedBy' field.
Documentation states that {quote} Fields that are used for the index by feature HAVE to be unique in the database. The behavior for multiple entities with the same index-by field value is undefined. {quote} which is unclear (especially the use of the word 'database'). I wonder why indexes used for association should be unique for a whole table.
Source: doctrine/orm