j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

2 updates in a transaction clobber one another #284

Closed okachobi closed 8 years ago

okachobi commented 8 years ago

I have some code where I begin a transaction, then do the equivalent of:

ORM::getDb()->beginTransaction();
$val1 = ORM::forTable('attribute')->where( array( 'id' => '1', 
            'name' => 'type1' ) )->findOne();
$val1->value = "x";
$val1->save();
$val2 = ORM::forTable('attribute')->where( array( 'id' => '1', 
            'name' => 'type2' ) )->findOne();
$val2->value = "y";
$val2->save();
ORM::get_db()->commit();

When I look at the table, both rows type1 and type2 have the value set to "y". The Primary key is a combination of id and name. There is other code in there too to handle exceptions and rollback, and create the row if they come back as null, but in the case where both exist and I update the value, the 2nd value overwrites the first.

Am I using the library wrong? I simply want to update 2 records as a transaction and rollback if either update fails. I can do this easily with SQL, but can't seem to figure out how to do it with idiorm.

okachobi commented 8 years ago

Closing this issue. Because I modified the table to have a combination key, but the configuration id_column_overrides set it to just id, both saves resulted in SQL that only references the id column. Once I set the id_column_overrides array to include the array('id', 'name') for table 'attribute' it worked as expected. :moron: