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.

insert ignore

want to add to database record with a request 'INSERT IGNORE ...' (since there are two fields associated UNIQUE).

then, after a request to receive the inserted record id (primary key autoincrement).

how to do it ??? I'm all worn out already = (

edited Mar '15
    $sql = 'INSERT IGNORE INTO invoices VALUES (null, :emailId, :userId, :offerType)';
    $sth = $this->db->prepare($sql);
    $sth->bindParam(':emailId',   $arrInvoice['emailId'], PDO::PARAM_INT);
    $sth->bindParam(':userId',    $arrInvoice['userId'], PDO::PARAM_INT);
    $sth->bindParam(':offerType', $arrInvoice['offerType'], PDO::PARAM_INT);


tried so, but $sth not return inserted id

    $invoice = new invoices();
    $php->emailId = $arrInvoice['emailId'];
    $php->userId = $arrInvoice['userId'];
    $php->offerType = $arrInvoice['offerType'];
    $php->save ();

this code gives an error, because the related field is not unique


table invoices have: UNIQUE uniqueEmailIdUserId ( emailId , userId )


There is no way to INSERT IGNORE by Phalcon Model. But you can use RAW query by using DB adapter that added to your app DI.

BUT!!! You could use $model->save() or $model->create()exception to handle INSER IGNORE yourself. something like this:

try {
} catch (Exception $e) {
    if ($e->getCode() == 1062 //DUPLICATE ERROR CODE) {
        // do nothing :) infact it handled duplicate entry by giving no error
    } else {
        throw $e; // or anything you want...