1

I have PHP:

if($post['for']){
    foreach($post['for'] as $value){
        $saveMsgObjFor->message_id = $save;
        $saveMsgObjFor->object_type_id = 4;
        $saveMsgObjFor->object_ref_id = $value;
        $saveMsgObjFor->object_email = null;
        $saveMsgObjFor->save($update);
    }
}

But it just save first loop. For twice, it show error:

Statement could not be executed (23000 - 1062 - Duplicate entry
 '33' for key 'PRIMARY'

33 is field message_object_id and it was auto increment. Help me, please..

2 Answers2

2

Based on the hint in this answer, it would appear that you need to explicitly set the message_object_id to null. First time round it defaults to null, but then after the save() it gets set to the auto increment value, so you need to explicitly reset it, for example:

foreach($post['for'] as $value){
    $saveMsgObjFor->message_object_id = null;
    $saveMsgObjFor->message_id = $save;
Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
0

I presume you should remove the following line from your code:

$saveMsgObjFor->message_id = $save;

I'm not overly familiar with the Zend framework but it looks like you're trying to assign the same value to a auto_increment, which won't work, as it's the row's identifier and must be unique. Hope this helps!

Connor Gurney
  • 372
  • 2
  • 12