Bug Report #4405

Belongs to relationships return the wrong record

Added by Isaiah DeRose-Wilson 4 months ago. Updated 4 months ago.

Status:Closed Start date:01/16/2012
Priority:Normal Due date:
Assignee:Isaiah DeRose-Wilson % Done:

100%

Category:-
Target version:Kohana v3.x - v3.3.0
Resolution:fixed Points:1

Description

When ORM loads "belongs to" relationships it runs $model->where($col, '=', $val)->find() without making sure $val is something other than NULL. If $col is an auto increment column (very common) and $val is NULL, mysql will return the last inserted record instead of an empty record.

Example:

$model_a = ORM::factory('model')...->save(); // This records id is 42

$model_b = ORM::factory('test', $id);

$model_c = $model_b->model_c; // model_c is a belongs to relationship and doesn't exist in the database

echo $model_c->id; // Outputs 42 instead of NULL. This is because the WHERE id = NULL query (what ORM runs to find related data) returned the data for the insert we did above instead of finding no records.

Related mysql docs: http://mysql.localhost.net.ar/doc/refman/5.1/en/server-session-variables.html#sysvar_sql_auto_is_null

History

Updated by Isaiah DeRose-Wilson 4 months ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100
  • Resolution set to fixed

Also available in: Atom PDF