E
E
Elkaz2014-01-15 10:26:32
PHP
Elkaz, 2014-01-15 10:26:32

Phalcon phql handling duplicate key

Hello.
Such a question, could not find the answer in the documentation and on the community forum.
There is a fairly simple table (MySQL). It has a title field with a UNIQUE index. Accordingly, something like this is done in the controller:

$Pages = new Pages();
                   

                    $Pages->title = !empty ($streamPage['title']) ? $streamPage['title'] : 'Untitled';
                    $Pages->active = 1;


                    if ($Pages->save() == false){
                        foreach ($Pages->getMessages() as $errorMessage){
                            error_log ($errorMessage);
                        }
                        continue;
                    }

However, if we execute the same query 2 times, we will get a logical error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '...' for key 'title'
Can we somehow catch the duplicate entry and make an update? That is, through the ORM, recreate the
INSERT ... ON DUPLICATE KEY UPDATE ... query

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kompi, 2014-01-15
@Elkaz

Offhand solution. In the model, in beforeSave, do a search for an existing title. Accordingly, if found - update, otherwise - create.

A
Anatoly, 2014-01-15
@taliban

And try and catch doesn't catch this error?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question