spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

insert autoincrement integer col #26

Closed kiswap closed 12 years ago

kiswap commented 12 years ago

Hi!

when I have a model with autoincrement integer col, and try to insert some new records I cannot do save(true) to get the insert id, because the autoincrement field has a default value of 0. then it will have a hash_code, and $builder wont be executed with argument: true.

ie: model: class Model_Leap_User extends DB_ORM_Model {

public function construct() { parent::construct(); $this->fields = array( 'id' => new DB_ORM_Field_Integer($this, array( 'max_length' => 11, 'nullable' => FALSE, 'unsigned' => TRUE, )), 'email' => new DB_ORM_Field_String($this, array( 'max_length' => 50, 'nullable' => FALSE, )), ... public static function primary_key() { return array('id'); } public static function is_auto_incremented() { return true; } }

inserting: $user = DB_ORM::model('user'); $user->email = 'example@example.com'; $user->save(TRUE);

now, save function is called in modules/leap/classes/base/db/orm/model.php and (line 401) $hash_code = $this->hash_code(); returns a not null value, because (line 297) "if ( ! is_null($value) )" is not true (integer field's default value is 0 not null) maybe "if ( ! is_null($value) && $value != 0)" would be better, but I'm not sure. any other solution? setting the primary key col to null won't work (ie $user->id=null) just don't work.

bluesnowman commented 12 years ago

Thanks for posting this issue. I will look into a possible solution and get back to you shortly.

CubedEye commented 12 years ago

I've noticed this bug too. We've just been setting ID field to be nullable => TRUE, that gets around the problem as a temporary fix. The only issue is it doesn't reflect the DB properly.

bluesnowman commented 12 years ago

We have pushed a potential fix to this auto-incrementing issue in 3.2/develop branch. We are currently testing it but if anyone wants to test it as well, we should be able to move it into production very soon. Please let us know if it works for you without problem.

This issue is related to Issue #25.