Upstream Patch: ORM enhancements for PTA

This commit is contained in:
Deon George 2011-05-23 20:02:00 +10:00
parent ff2370c368
commit 4765770a1a

View File

@ -235,6 +235,15 @@ class Kohana_ORM extends Model implements serializable {
*/
protected $_table_names_plural = 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;
/**
* Model configuration, reload on wakeup?
* @var bool
@ -368,6 +377,7 @@ class Kohana_ORM extends Model implements serializable {
{
$defaults['model'] = $alias;
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
$this->_has_one[$alias] = array_merge($defaults, $details);
}
@ -630,7 +640,7 @@ class Kohana_ORM extends Model implements serializable {
$model = $this->_related($column);
// 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']];
$model->where($col, '=', $val)->find();
@ -641,9 +651,23 @@ class Kohana_ORM extends Model implements serializable {
{
$model = $this->_related($column);
if (! is_array($this->_has_one[$column]['foreign_key']))
{
// Use this model's primary key value and foreign model's column
$col = $model->_table_name.'.'.$this->_has_one[$column]['foreign_key'];
$val = $this->pk();
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$this->_has_one[$column]['foreign_key'];
$val = $this->_object[$this->_has_one[$column]['far_key']];
}
else
{
foreach ($this->_has_one[$column]['foreign_key'] as $fk)
{
// 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];
$model = $model->where($col, '=', $val);
}
}
$model->where($col, '=', $val)->find();
@ -653,31 +677,54 @@ class Kohana_ORM extends Model implements serializable {
{
$model = ORM::factory($this->_has_many[$column]['model']);
if (! is_array($this->_has_many[$column]['foreign_key']))
{
if (isset($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_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
$join_col2 = $model->_table_name.'.'.$model->_primary_key;
$join_col1 = ($this->_disable_join_table_name ? '' : $through.'.').$this->_has_many[$column]['far_key'];
$join_col2 = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$model->_primary_key;
$model->join($through)->on($join_col1, '=', $join_col2);
// 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();
}
else
{
// Simple has_many relationship, search where target model's foreign key is this model's primary key
$col = $model->_table_name.'.'.$this->_has_many[$column]['foreign_key'];
$val = $this->pk();
$col = ($this->_disable_join_table_name ? '' : $model->_table_name.'.').$this->_has_many[$column]['foreign_key'];
$val = $this->_object[$this->_has_many[$column]['far_key']];
}
return $model->where($col, '=', $val);
}
else
{
foreach ($this->_has_many[$column]['foreign_key'] as $fk)
{
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];
}
$model = $model->where($col, '=', $val);
}
return $model;
}
}
else
{
throw new Kohana_Exception('The :property property does not exist in the :class class',
array(':property' => $column, ':class' => get_class($this)));
@ -999,13 +1046,14 @@ class Kohana_ORM extends Model implements serializable {
{
$this->_db_builder->from($this->_table_name);
if ($multiple === FALSE)
if ($multiple === FALSE AND ! $this->_disable_limit)
{
// Only fetch 1 record
$this->_db_builder->limit(1);
}
// Select all columns by default
if (! $this->_disable_wild_select)
$this->_db_builder->select($this->_table_name.'.*');
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
@ -1015,7 +1063,7 @@ class Kohana_ORM extends Model implements serializable {
if (strpos($column, '.') === FALSE)
{
// 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);
@ -1127,8 +1175,9 @@ class Kohana_ORM extends Model implements serializable {
* @param string $value The value to filter
* @return string
*/
protected function run_filter($field, $value)
protected function run_filter($field, $value, $filters=NULL)
{
if (is_null($filters))
$filters = $this->filters();
// Get the filters for this column