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

Updating the wrong record????

I have an application for learning by moving throug a hierarchy of units. When learning you can go through the units in the same level ("go left-right") or go "down" to a lower level.

Two tables:

learnings (id*,fk_user, fk_starting_unit, belongs_to)

learning_structure (fk_unit, fk_learnings, completed)

When the user goes "down" I add a new Learning that "belongs_to" the previous one. Then I get all units on the lower level and add them to the learning structure so I can move through them.

In the moment the user goes "down" I still have to make the current unit "completed" and then move on to lower level units.

SAMPLE STRUCTURE:

Learnings (1, 99, 100, NULL)

Learnings (2, 99, 102, 1) -> this added when I want to go "down"

Learning Structure (100, 1, 1)

Learning Structure (101, 1, 1)

Learning Structure (102, 1, 0) -> here I click the link to go "down", a new Learning added and items below added

Learning Structure (103, 2, 0)

Learning Structure (104, 2, 0)

PROBLEM:

$prev_id = 1

$uid = 102

$phql = "UPDATE LearningStructure SET completed=1 WHERE fk_learnings=$prev_lid AND fk_unit = $uid";

$result = $this->modelsManager->executeQuery($phql);

RESULT:

Learning Structure (102, 1, 0) - not updated

Learning Structure (103, 2, 1) - UPDATED???

Why???



1.6k

It seems that the problem for Phalcon is that I inserted the lower level items to the DB before updating this record.

When I var_dump the record before updating it, there is a property called _lastinitialized that has the values of the next row in the DB (103,2).

After the update this (wrong) record is updated (as shown above), instead of the one that should be updated.

????



43.9k

Hi, that's strange indeed. Could you show the full action's code ?



1.6k

I had to do the update of the record (102,1) first, then add the other rows after. That solved the problem. Still no clue why this happens. I'll post the code in next reply.