We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon won't update record anyway

I'm using Model and PHQL to update a record but mysql wont accept any changes. select and insert works properly.

sample update with PHQL:

UPDATE \Modules\Shared\Models\Airline SET [name] = 'test' WHERE id = '8'

Could you provide sample code and db structure if that's not a problem?

Simpled code i've used using model:

            $object = Airline::findFirst($id);

            $post = $this->request->getPost();

            $object->name = $post['name'];
            $object->lata = $post['lata'];
            $object->language = $post['language'];
            $object->parent_id = $post['parent_id'] ?: NULL;

            $object->update();
            foreach ($object->getMessages() as $m) {
                echo $m. '<br>';
            }

Model and db structure:

class Airline extends \Phalcon\Mvc\Model
{

  /**
  * @var integer
  */
  public $id;

  /**
  * @var string
  */
  public $name;

  /**
  * @var string
  */
  public $lata;

  /**
  * @var string
  */
  public $language;

  /**
  * @var integer
  */
  public $parent_id;

  public function initialize()
  {
      $this->setSource('sh_airlines');
 }

Could you provide sample code and db structure if that's not a problem?



43.9k

Hi,

are you sure that you've got a valid id for querying $object = Airline::findFirst($id);

edited Oct '15

Hi

Yes! I'm sure.

also post data was checked and are valid.

Hi,

are you sure that you've got a valid id for querying $object = Airline::findFirst($id);



43.9k

ok,

does $object = Airline::findFirst($id); return a valid model object ?

Also try with

$object->save();

Yes. $object rerturns a vaild object. save() has a same result..

ok,

does $object = Airline::findFirst($id); return a valid model object ?

Also try with

$object->save();



43.9k

that's really strange.

Try to debug your sql save statement to see what happens during $object->save() (or update())

(https://docs.phalcon.io/en/latest/reference/models.html#logging-low-level-sql-statements)

edited Oct '15

oOOPS !

result:

[Mon, 26 Oct 15 17:00:52 +0330][INFO] UPDATE sh_airlines SET id = ?, lata = ?, language = ?, parent_id = ? WHERE name = ?

is it normal?!


edit:

logging of Insert also have question mark instead of values

[Mon, 26 Oct 15 21:07:37 +0330][INFO] INSERT INTO sh_airlines (name, lata, language, parent_id) VALUES (?, ?, ?, ?)

It is normal, this is params binding in PDO.

Can you try something like:

var_dump($object->update());
foreach ($object->getMessages() as $m) {
    echo $m. '<br>';
}
exit;

This is from the first code box in your comment https://forum.phalcon.io/discussion/9131/phalcon-wont-update-record-anyway#C25077

note the exit after foreach loop;