Tuesday, September 25, 2012

Doctrine: SQLSTATE[23000]: Integrity constraint violation duplicate entry

If you are using a table where some fields are I18N and ex. the title field is sluggable, you will realize that you cannot update(!) the object with the same title. See an example:

The page is multilingual (Hungarian and English). You create a product with TEFAL XY Hungarian and TEFAL XY English tilte (the same title used in each language). You can saved the form / object, it created successfully. Now you change the Hungarian title to TEFAL X. Save. Then change the English title to TEFAL X, too. You will get an error:

Doctrine: SQLSTATE[23000]: Integrity constraint violation duplicate entry

This is a bug in doctrine 1.2.x.

Solution
  1. Copy lib / vendor / symfony / lib / plugins / sfDoctrinePlugin / lib / vendor / doctrine / Doctrine / Template / Listener / Sluggable.php inside your own lib directory.
  2. In the getUniqueSlug method, change this line:
    $whereString .= ' AND r.' . implode(' != ? AND r.', $table->getIdentifierColumnNames()) . ' != ?';
    to this:
    $whereString .= ' AND (r.' . implode(' != ? OR r.', $table->getIdentifierColumnNames()) . ' != ?)';
  3. Save. Clear cache. Be happy.

No comments:

Post a Comment