Doctrine2, uniqueConstraint on JoinColumn for ZF2?

I have a data feed that has duplicated content (no idea why, it's an external feed), however we need to insert all items with a constraint on the title and type, i.e.

These can exist:

  • Name_A, Type_A
  • Name_A, Type_B

But only one of these should exist:

  • Name_A, Type_A
  • Name_A, Type_A

Here's the Entity code I'm using:

 * Restauration
 * @ORM\Table(name="restauration", uniqueConstraints={@ORM\UniqueConstraint(name="name_unique", columns={"name_1", "restauration_type"})})
 * @ORM\Entity(repositoryClass="iMotionTools\Repository\RestaurationRepository")
class Restauration
     * @var integer
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
    private $id;

     * @var string
     * @ORM\Column(name="name_1", type="string", length=128, nullable=true)
    private $name1;

     * @var string
     * @ORM\ManyToOne(targetEntity="RestaurationType", cascade={"persist"})
     * @ORM\JoinColumn(name="restauration_type", referencedColumnName="id")
    private $type;

But I get this error when parsing and inserting the data:

SQLSTATE[23000]: Integrity constraint violation: 19 columns name_1, restauration_type are not unique:91:C:\coding\currate\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php

I'm wondering whether the easy way is to just ignore the thrown exception? Looks like it's a driverExceptionDuringQuery that gets thrown during my call to $em->persist(); but I'm not sure how I would ignore if the call contains the above error?


If you want to ignore that, stop using constraint integrity.

{@ORM\UniqueConstraint(name="name_unique", columns={"name_1" //etc...

Your data has name_1 not unique this is why you have this error, integrity constraint check this, you can't ignore that, without remove the unique constraint parameters.

Edit :

You have to then, before persist data, check with your actual data in the table, if there is a duplicate entry for both name_1 AND Type, and do not persist them.

for check both you can use :

@UniqueEntity({"name", "type"})

found here : validation multiple constraint columns

Even if it's for SF2, it's the same concept

I've removed the UniqueConstraint attribute from the table and added a function to check the objects list (which later get $entity->persist()-ed), using an array so that I can easily use it for different entity types, and it seems to work well now.

        $key = $hashList ? '' : $page['id'];
        foreach ($hashList as $method) {
            $val = $object->{$method}();

            if(is_object($val)) {
                $val = $val->getId();

            $key .= $val;

        $key = md5($key);

        $objects[$key] = $object;

Where $hashList = array('getName', 'getType') - and getType returns an object (since it's another entity), but which always has the getId() function... probably not the best solution but it works for my situation...

Need Your Help

APEX: Charts in Tabs Container not visible

oracle oracle-apex oracle-apex-5 anychart

I have in APEX a 'Static content' region. This has the template 'Tabs container'. As subregions (or 'tabs' as it is called within this template) I have multiple charts. The problem is that just the...

SSIS How to get part of a string by separator

sql-server ssis expression etl

I need an SSIS expression to get the left part of a string before the separator, and then put the new string in a new column. I checked in derived column, it seems no such expressions. Substring co...