Enhancements to Kohana ORM
Enhancements to include multiple primary keys in remote joins, added option to suppress table names in joins More enhancements to ORM specific from TSM
This commit is contained in:
parent
2c15a4b40e
commit
939d0a43d6
@ -66,6 +66,12 @@ class Kohana_ORM {
|
|||||||
// Model configuration
|
// Model configuration
|
||||||
protected $_table_names_plural = TRUE;
|
protected $_table_names_plural = TRUE;
|
||||||
protected $_reload_on_wakeup = TRUE;
|
protected $_reload_on_wakeup = TRUE;
|
||||||
|
// Suppress ORMs inclusion of <table_name>.*
|
||||||
|
protected $_disable_wild_select = FALSE;
|
||||||
|
// Suppress ORMs inclusion of <table_name>. to column joins
|
||||||
|
protected $_disable_join_table_name = FALSE;
|
||||||
|
// Suppress ORMs use of limit
|
||||||
|
protected $_disable_limit = FALSE;
|
||||||
|
|
||||||
// Database configuration
|
// Database configuration
|
||||||
protected $_db = NULL;
|
protected $_db = NULL;
|
||||||
@ -327,7 +333,7 @@ class Kohana_ORM {
|
|||||||
$model = $this->_related($column);
|
$model = $this->_related($column);
|
||||||
|
|
||||||
// Use this model's column and foreign model's primary key
|
// Use this model's column and foreign model's primary key
|
||||||
$col = $model->_table_name.'.'.$model->_primary_key;
|
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$model->_primary_key;
|
||||||
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
|
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
|
||||||
|
|
||||||
$model->where($col, '=', $val)->find();
|
$model->where($col, '=', $val)->find();
|
||||||
@ -339,7 +345,7 @@ class Kohana_ORM {
|
|||||||
$model = $this->_related($column);
|
$model = $this->_related($column);
|
||||||
|
|
||||||
// Use this model's primary key value and foreign model's column
|
// Use this model's primary key value and foreign model's column
|
||||||
$col = $model->_table_name.'.'.$this->_has_one[$column]['foreign_key'];
|
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$this->_has_one[$column]['foreign_key'];
|
||||||
$val = $this->pk();
|
$val = $this->pk();
|
||||||
|
|
||||||
$model->where($col, '=', $val)->find();
|
$model->where($col, '=', $val)->find();
|
||||||
@ -350,29 +356,52 @@ class Kohana_ORM {
|
|||||||
{
|
{
|
||||||
$model = ORM::factory($this->_has_many[$column]['model']);
|
$model = ORM::factory($this->_has_many[$column]['model']);
|
||||||
|
|
||||||
if (isset($this->_has_many[$column]['through']))
|
if (! is_array($this->_has_many[$column]['foreign_key']))
|
||||||
{
|
{
|
||||||
// Grab has_many "through" relationship table
|
if (isset($this->_has_many[$column]['through']))
|
||||||
$through = $this->_has_many[$column]['through'];
|
{
|
||||||
|
// Grab has_many "through" relationship table
|
||||||
|
$through = $this->_has_many[$column]['through'];
|
||||||
|
|
||||||
// Join on through model's target foreign key (far_key) and target model's primary key
|
// Join on through model's target foreign key (far_key) and target model's primary key
|
||||||
$join_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
|
$join_col1 = ($this->_disable_join_table_name ? '' : $through.'.').$this->_has_many[$column]['far_key'];
|
||||||
$join_col2 = $model->_table_name.'.'.$model->_primary_key;
|
$join_col2 = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$model->_primary_key;
|
||||||
|
|
||||||
$model->join($through)->on($join_col1, '=', $join_col2);
|
$model->join($through)->on($join_col1, '=', $join_col2);
|
||||||
|
|
||||||
// Through table's source foreign key (foreign_key) should be this model's primary key
|
// Through table's source foreign key (foreign_key) should be this model's primary key
|
||||||
$col = $through.'.'.$this->_has_many[$column]['foreign_key'];
|
$col = ($this->_disable_join_table_name ? '' : $through.'.').$this->_has_many[$column]['foreign_key'];
|
||||||
$val = $this->pk();
|
$val = $this->pk();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Simple has_many relationship, search where target model's foreign key is this model's primary key
|
||||||
|
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$this->_has_many[$column]['foreign_key'];
|
||||||
|
$val = $this->pk();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $model->where($col, '=', $val);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Simple has_many relationship, search where target model's foreign key is this model's primary key
|
foreach ($this->_has_many[$column]['foreign_key'] as $fk)
|
||||||
$col = $model->_table_name.'.'.$this->_has_many[$column]['foreign_key'];
|
{
|
||||||
$val = $this->pk();
|
if (isset($this->_has_many[$column]['through']))
|
||||||
}
|
{
|
||||||
|
throw new Kohana_Exception('This code hasnt been written yet!');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Simple has_many relationship, search where target model's foreign key is this model's primary key
|
||||||
|
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$fk;
|
||||||
|
$val = $this->_object[$fk];
|
||||||
|
}
|
||||||
|
|
||||||
return $model->where($col, '=', $val);
|
$model = $model->where($col, '=', $val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -763,7 +792,7 @@ class Kohana_ORM {
|
|||||||
if ($id !== NULL)
|
if ($id !== NULL)
|
||||||
{
|
{
|
||||||
// Search for a specific column
|
// Search for a specific column
|
||||||
$this->_db_builder->where($this->_table_name.'.'.$this->_primary_key, '=', $id);
|
$this->_db_builder->where(($this->_disable_join_table_name ? '' : $this->_table_name.'.').$this->_primary_key, '=', $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_load_result(FALSE);
|
return $this->_load_result(FALSE);
|
||||||
@ -1260,14 +1289,15 @@ class Kohana_ORM {
|
|||||||
{
|
{
|
||||||
$this->_db_builder->from($this->_table_name);
|
$this->_db_builder->from($this->_table_name);
|
||||||
|
|
||||||
if ($multiple === FALSE)
|
if ($multiple === FALSE AND ! $this->_disable_limit)
|
||||||
{
|
{
|
||||||
// Only fetch 1 record
|
// Only fetch 1 record
|
||||||
$this->_db_builder->limit(1);
|
$this->_db_builder->limit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select all columns by default
|
// Select all columns by default
|
||||||
$this->_db_builder->select($this->_table_name.'.*');
|
if (! $this->_disable_wild_select)
|
||||||
|
$this->_db_builder->select($this->_table_name.'.*');
|
||||||
|
|
||||||
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
|
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
|
||||||
{
|
{
|
||||||
@ -1276,7 +1306,7 @@ class Kohana_ORM {
|
|||||||
if (strpos($column, '.') === FALSE)
|
if (strpos($column, '.') === FALSE)
|
||||||
{
|
{
|
||||||
// Sorting column for use in JOINs
|
// Sorting column for use in JOINs
|
||||||
$column = $this->_table_name.'.'.$column;
|
$column = ($this->_disable_join_table_name ? '' : $this->_table_name.'.').$column;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_db_builder->order_by($column, $direction);
|
$this->_db_builder->order_by($column, $direction);
|
||||||
|
Reference in New Issue
Block a user