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

Snapshot data not updated following save() / update(). Bug, limitation, user error?

Dynamic updates appear to work, but only correctly once. I'm wondering why and welcome suggestions for an optimal fix.

To illustrate, consider a model that uses dynamic updates with schema:

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| strcol1 | varchar(255) | YES  |     | NULL    |                |
| intcol2 | int(11)      | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

initial model creation works fine with query:

INSERT INTOtest(strcol1,intcol2) VALUES (DEFAULT, DEFAULT)

However, the following code on an instance of a new Test model:

$test->strcol1 = 'foo';

$test->save();

$test->intcol2 = 123;

$test->save();

gives SQL:

              UPDATE `test` SET `strcol1` = 'foo' WHERE `id` = '4'
              UPDATE `test` SET `strcol1` = 'foo', `intcol2` = 123 WHERE `id` = '4'

Why would strcol1 be set again when it has not changed since the first save? While not especially an issue in this case, were the value for a column set as rawvalue now() for example, it could produce unintended results.

This is with 2.0.10. Any suggested workaround/fix at this point?

Well... first in my mind is to clone $test object after u find the element :))) But... maybe is something in documentation. I will search about it....



3.8k

Thanks Boris. That would work for some cases but is a kludge for sure :) It's quite conceivable that a model instance gets passed through a path where updates are indepedently made in several places, and while it could be possible to defer updating until a final update, that might not be the cleanest design and the possibility of such updates may not be something that should be exposed. I'll log it as a defect.