I have table structure like
id - slug - locale
1 - my-cool-slug - en
2 - my-cool-slug - lv
id - primary key slug_locale = unique key( like - my-cool-slug_en, and my-cool-slug_lv same slugs, but locale is different thats why there's no error)
When I try to add a new slug with Phalcon it shows error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'my-cool-slug-lv' for key 'slug_locale'
$data = EventsLang::findFirst(array(
'conditions' => 'object_id = :id: AND locale = :locale:',
'bind' => array('id' => $object_id, 'locale' => $locale)
));
if(!$data) {
$data = new EventsLang;
$data->locale= $locale;
$data->object_id = $object_id;
}
if($transaction) $data->setTransaction($transaction);
if(isset($columns['slug']) and !empty($columns['slug'])) $data->slug = $columns['slug'];
if($data->save()) {
return true;
}
if($transaction) $transaction->rollback(t("Could not save translation"));
return false;
Here's the code I'm passing transaction from before Event creating, like
$transaction = \Core::getTransaction();
try {
if($event_id) {
$events_obj = Events::findFirst($event_id);
if(!$events_obj) $events_obj = new Events();
} else {
$events_obj = new Events();
}
$events_obj->setTransaction($transaction);
if(!$events_obj->save()) {
$transaction->rollback(t("Nevarēja saglabāt pamatdatus"));
}
And I pass created transaction before
EventsLang::add($events_obj->id,$lang,$columns, $transaction);
} catch (\Phalcon\Mvc\Model\Transaction\Failed $e) {
return array(
'error' => $e->getMessage()
);
}