Skip to content
Browse files

Merge branch '3.2/develop' into 3.3/develop

  • Loading branch information...
2 parents 7ea060e + e669bf1 commit fadde0bf9df52aeffb6fff4ade2707c80538030a @isaiahdw isaiahdw committed Jan 30, 2012
Showing with 15 additions and 9 deletions.
  1. +15 −9 classes/kohana/orm.php
View
24 classes/kohana/orm.php
@@ -562,8 +562,14 @@ public function __get($column)
// Use this model's column and foreign model's primary key
$col = $model->_object_name.'.'.$model->_primary_key;
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
-
- $model->where($col, '=', $val)->find();
+
+ // Make sure we don't run WHERE "AUTO_INCREMENT column" = NULL queries. This would
+ // return the last inserted record instead of an empty result.
+ // See: http://mysql.localhost.net.ar/doc/refman/5.1/en/server-session-variables.html#sysvar_sql_auto_is_null
+ if ($val !== NULL)
+ {
+ $model->where($col, '=', $val)->find();
+ }
return $this->_related[$column] = $model;
}
@@ -1195,7 +1201,7 @@ public function create(Validation $validation = NULL)
throw new Kohana_Exception('Cannot create :model model because it is already loaded.', array(':model' => $this->_object_name));
// Require model validation before saving
- if ( ! $this->_valid)
+ if ( ! $this->_valid OR $validation)
{
$this->check($validation);
}
@@ -1249,16 +1255,16 @@ public function update(Validation $validation = NULL)
if ( ! $this->_loaded)
throw new Kohana_Exception('Cannot update :model model because it is not loaded.', array(':model' => $this->_object_name));
- if (empty($this->_changed))
+ // Run validation if the model isn't valid or we have additional validation rules.
+ if ( ! $this->_valid OR $validation)
{
- // Nothing to update
- return $this;
+ $this->check($validation);
}
- // Require model validation before saving
- if ( ! $this->_valid)
+ if (empty($this->_changed))
{
- $this->check($validation);
+ // Nothing to update
+ return $this;
}
$data = array();

0 comments on commit fadde0b

Please sign in to comment.
Something went wrong with that request. Please try again.