4

im using Doctrine on Symfony2 and im trying to insert some values on my table, but if I try to insert a duplicate key I got an error from the system:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test-1-2-2016-10-11-13' for key 'Unique'

That's my code:

                $insert = new Alerts();
                $insert->setAlKeyword($alert_keyword);
                $insert->setAlLocation($alert_location);
                $insert->setAlDevice($alert_device);
                $insert->setAlSource($alert_source);
                $insert->setAlSubDate(new \DateTime($alert_date);
                $insert->setAlSubHour($alert_time);
                $insert->setAlTotal(+1);
                $em = $this->getDoctrine()->getManager("my_em");
                $em->persist($insert);
                $em->flush();

The problem is about the DUPLICATE KEY UPDATE statement: how can I use that statement in doctrine?

Thanks a lot

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
cressisr
  • 41
  • 1
  • 2
  • This might be useful: http://stackoverflow.com/questions/9699613/insert-ignore-on-duplicate-entries-in-doctrine2-symfony2 – Alok Patel Oct 12 '16 at 10:21

2 Answers2

-3

Make use of the Unique constraint. This will prevent you trying to save when the unique index already exists.

Apply this to the entity for the unique index you have set in your table.

If it is a composite primary key, look at this answer.

Community
  • 1
  • 1
Rooneyl
  • 7,802
  • 6
  • 52
  • 81
-3

Remove unique: true on non-unique statement from your doctrine configuration place

Stephan Yamilov
  • 605
  • 7
  • 14
  • That's not a proper solution. You'll end up with 2 rows while OP asked for a duplicate key update statement. – Quentin S. Oct 21 '17 at 21:03