Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon does not catch MySQL error on duplicate entry

I have table structure like

id -         slug -       locale 
1 -     my-cool-slug - en
2 -     my-cool-slug - lv

id - primary key sluglocale = unique key( like - my-cool-slugen, 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%5Flocale'

        $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()
            );
        }
edited Aug '14

You say your table columns are id, slug and locale but your query uses object_id

    $data = EventsLang::findFirst(array(
        'conditions' => 'object_id = :id: AND locale = :locale:',
        'bind' => array('id' => $object_id, 'locale' => $locale)
    ));

And:

if(isset($columns['slug']) and !empty($columns['slug'])) $data->slug = $columns['slug'];

What is $columns?

edited Aug '14

There's nothing with that, i just interpreted to you to understand it easier

id === object_id

$columns is this, there's no point with that, just passing slug column to EventLang models

    $columns = array(
        'slug' => seo_string($data['title'][$lang]),
    );

There's more fields on table, but I showed only what is used to!



3.3k
Accepted
answer

Yeah, thank you, but its little messy by this, coz other table related errors Phalcon can handle! :)