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.

model save(), create two records

such as $mt->phones = "123,456";

 $phonearr = explode(',', $mt->phones);
                foreach ($phonearr as $phone) {
                    $mtlist = new List();
                    $mtlist->return_msgid = 1;
                    $mtlist->mtid = $mt->mtid;
                                        $mtlist->phone = $phone;
                    echo "phone:".$phone."\n";
                    if (!$mtlist->save()) {
                        foreach ($mtlist->getMessages() as $value) {
                            echo $value->getMessage();
                        }
                    }
                }

ablove of it output: 123 456

in list table , should be two records, but in my list table , have four records. but above of output is two.

mean foreach execute one times

but sometimes, it have two records, sometimes it have four records. no fixed.



82.7k

Check this, maybe is related to your problem: https://github.com/phalcon/cphalcon/issues/291



11.6k
edited Oct '14

ï¼ Phalcon , this section of the project is phalcon cli.

in cliTask,

class MainTask extends \Phalcon\CLI\Task {
     public function sendAction() {
          $mt = List::findFirst(array("issend = 0"));
          $phonearr = explode(',', $mt->phones);
        foreach ($phonearr as $phone) {
          $mtlist = new List();
          $mtlist->return_msgid = 1;
          $mtlist->mtid = $mt->mtid;
                                        $mtlist->phone = $phone;
          echo "phone:".$phone."\n";
          if (!$mtlist->save()) {
            foreach ($mtlist->getMessages() as $value) {
              echo $value->getMessage();
            }
          }
        }
      }

in my shell code is :

#!/bin/bash
while true ; do 
        php -f /opt/fast_cli/console.php main send
sleep 1
done

in the whloe process, i use echo print data, don not seen twice execute



11.6k
edited Oct '14

i have found the error reason.

because table List have not primary key (yesteryday, i drop the table primary key.). so , when i update the record:

i use :

$st = List::findFirst("params xxx");
$st->return_msgid = 0;
$st->save();

now, it don't update the record, but insert a record . why?



82.7k
Accepted
answer

Phalcon requires a primary key to create the underlying SQL correctly:

UPDATE list SET return_msid = 0 WHERE primary_field = value

If you want to ensure that a create/update is made you can do:

$st = List::findFirst("params xxx");
$st->return_msgid = 0;
$st->update(); //This already needs a primary but it does not creates a record just return false


11.6k

ok, very thanks.

this error cost my whole day time. I always thought that the error is inserted