Search code examples
symfony1doctrine

Copy an object with relations in which there is a Sortable object throws an Duplicate Entry error


In an old projet using symfony 1.4 There are an object Product linked to other objects such as Translation ProductPlatform ...

What I want to do is copy the Product object with all its relations.

Pretty simple with $product->copy(true) BUT

It doesn't copy the relations, so i need to do :

  $this->loadReference('Translation');
  $this->loadReference('ProductPlatforms');

  foreach ($this->ProductPlatforms as $platform) {
    $platform->loadReference('Translation');
  }

  $newProduct = $this->copy(true);
  $newProduct->save();

  return $newProduct;

The throwed error is this :

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-131' for key 'product_platform_position_sortable_idx'

What I don't understand is the following requests :

SELECT p.id AS p__id, p.position AS p__position FROM product_platform p 
WHERE (p.product_id = 'EndNote (copie)') ORDER BY p.position desc LIMIT 1;
----
INSERT INTO product_platform (publish_configuration, product_id, platform_id, position) 
VALUES ('1', '131', '1', '1')

And then, it reinsert, for another object, the same position :

INSERT INTO product_platform (publish_configuration, product_id, platform_id, position) 
VALUES ('1', '131', '3', '1');

Why there is, instead of an ID, the Name of my object in the Where clause WHERE (p.product_id = 'EndNote (copie)'). I think this is the reason it tries to insert the same value.


Solution

  • Ok, it's a bug with the Sortable behaviour, we sent a PR :

    https://github.com/bshaffer/csDoctrineActAsSortablePlugin/pull/25