gajus/moa

How should afterUpdate method be triggered when saving object does not result in MySQL query?

gajus opened this issue · 3 comments

Suppose that you construct an object, e.g.

<?php
$string = new \Sandbox\Model\String($this->db);
$string['name'] = 'foo';
$string->save(); // INSERTs object to the database and triggers "afterInsert" method
$string['name'] = 'bar';
$string->save(); // UPDATEs object's instance in the database and triggers "afterUpdate" method
$string->save(); // Does not not execute MySQL query because object state has not changed.

Should the 3rd call to save:

  1. Issue MySQL query? (even though it is assumed that it will not affect the record)
  2. If query is not executed, should the afterUpdate method be triggered?

The current implementation will not execute MySQL statement but will trigger the afterUpdate method.

qris commented

Triggers may be defined on UPDATE that have side effects in the database, and the application may rely on those. Also, the timestamp of a record in the database would be updated by an UPDATE statement even if no values were changed.

Since MySQL does not allow an UPDATE statement with no SET clauses (i.e. it requires at least one column to be modified) you'd have to set a column to itself (e.g. ID = ID) which is odd. But I can't see any way to execute such a statement with MOA in application code, so there should be a way to force MOA to perform a null update.

Well, that's the thing. I also thought that timestamp would be updated even if no values change. But a quick test will show you that this is not the case. Therefore, it left me in doubt a bit. Though, after implementing MOA in practise on several applications, I realised that "afterUpdate" trigger must go-off regardless of MySQL behaviour.