Leenooks customisations for Kohana
Conflicts: application/bootstrap.php modules/unittest/bootstrap.php system/tests/kohana/CoreTest.php
This commit is contained in:
@@ -183,6 +183,18 @@ 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;
|
||||
|
||||
// Disable adding column aliases
|
||||
protected $_disable_column_alias = FALSE;
|
||||
|
||||
/**
|
||||
* Model configuration, reload on wakeup?
|
||||
* @var bool
|
||||
@@ -268,7 +280,7 @@ class Kohana_ORM extends Model implements serializable {
|
||||
else
|
||||
{
|
||||
// Passing the primary key
|
||||
$this->where($this->_object_name.'.'.$this->_primary_key, '=', $id)->find();
|
||||
$this->where(($this->_disable_join_table_name ? '' : $this->_object_name.'.').$this->_primary_key, '=', $id)->find();
|
||||
}
|
||||
}
|
||||
elseif ( ! empty($this->_cast_data))
|
||||
@@ -354,6 +366,7 @@ class Kohana_ORM extends Model implements serializable {
|
||||
}
|
||||
|
||||
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
|
||||
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
|
||||
|
||||
$init['_has_one'][$alias] = array_merge($defaults, $details);
|
||||
}
|
||||
@@ -362,7 +375,7 @@ class Kohana_ORM extends Model implements serializable {
|
||||
{
|
||||
if ( ! isset($details['model']))
|
||||
{
|
||||
$defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', Inflector::singular($alias))));
|
||||
$defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', ($this->_model_names_plural ? Inflector::singular($alias) : $alias))));
|
||||
}
|
||||
|
||||
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
|
||||
@@ -629,7 +642,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->_object_name.'.'.$model->_primary_key;
|
||||
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$model->_primary_key;
|
||||
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
|
||||
|
||||
// Make sure we don't run WHERE "AUTO_INCREMENT column" = NULL queries. This would
|
||||
@@ -646,9 +659,23 @@ class Kohana_ORM extends Model implements serializable {
|
||||
{
|
||||
$model = $this->_related($column);
|
||||
|
||||
// Use this model's primary key value and foreign model's column
|
||||
$col = $model->_object_name.'.'.$this->_has_one[$column]['foreign_key'];
|
||||
$val = $this->pk();
|
||||
if (! is_array($this->_has_one[$column]['foreign_key']))
|
||||
{
|
||||
// Use this model's primary key value and foreign model's column
|
||||
$col = ($this->_disable_join_table_name ? '' : $model->_object_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->_object_name.'.').$fk;
|
||||
$val = $this->_object[$fk];
|
||||
|
||||
$model = $model->where($col, '=', $val);
|
||||
}
|
||||
}
|
||||
|
||||
$model->where($col, '=', $val)->find();
|
||||
|
||||
@@ -658,29 +685,52 @@ class Kohana_ORM extends Model implements serializable {
|
||||
{
|
||||
$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
|
||||
$through = $this->_has_many[$column]['through'];
|
||||
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->_object_name.'.'.$model->_primary_key;
|
||||
// Join on through model's target foreign key (far_key) and target model's primary key
|
||||
$join_col1 = ($this->_disable_join_table_name ? '' : $through.'.').$this->_has_many[$column]['far_key'];
|
||||
$join_col2 = ($this->_disable_join_table_name ? '' : $model->_object_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
|
||||
$col = $through.'.'.$this->_has_many[$column]['foreign_key'];
|
||||
$val = $this->pk();
|
||||
// Through table's source foreign key (foreign_key) should be this model's primary 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 = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$this->_has_many[$column]['foreign_key'];
|
||||
$val = $this->_object[$this->_has_many[$column]['far_key']];
|
||||
}
|
||||
|
||||
return $model->where($col, '=', $val);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Simple has_many relationship, search where target model's foreign key is this model's primary key
|
||||
$col = $model->_object_name.'.'.$this->_has_many[$column]['foreign_key'];
|
||||
$val = $this->pk();
|
||||
}
|
||||
foreach ($this->_has_many[$column]['foreign_key'] as $mk => $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->_object_name.'.').$fk;
|
||||
$val = $this->_object[$mk];
|
||||
}
|
||||
|
||||
return $model->where($col, '=', $val);
|
||||
$model = $model->where($col, '=', $val);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1016,7 +1066,7 @@ class Kohana_ORM extends Model implements serializable {
|
||||
|
||||
foreach ($this->_table_columns as $column => $_)
|
||||
{
|
||||
$columns[] = array($this->_object_name.'.'.$column, $column);
|
||||
$columns[] = $this->_disable_column_alias ? $column : array($this->_object_name.'.'.$column, $column);
|
||||
}
|
||||
|
||||
return $columns;
|
||||
@@ -1034,14 +1084,15 @@ class Kohana_ORM extends Model implements serializable {
|
||||
{
|
||||
$this->_db_builder->from(array($this->_table_name, $this->_object_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
|
||||
$this->_db_builder->select_array($this->_build_select());
|
||||
if (! $this->_disable_wild_select)
|
||||
$this->_db_builder->select_array($this->_build_select());
|
||||
|
||||
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
|
||||
{
|
||||
@@ -1050,7 +1101,7 @@ class Kohana_ORM extends Model implements serializable {
|
||||
if (strpos($column, '.') === FALSE)
|
||||
{
|
||||
// Sorting column for use in JOINs
|
||||
$column = $this->_object_name.'.'.$column;
|
||||
$column = ($this->_disable_join_table_name ? '' : $this->_object_name.'.').$column;
|
||||
}
|
||||
|
||||
$this->_db_builder->order_by($column, $direction);
|
||||
@@ -1168,9 +1219,10 @@ 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)
|
||||
{
|
||||
$filters = $this->filters();
|
||||
if (is_null($filters))
|
||||
$filters = $this->filters();
|
||||
|
||||
// Get the filters for this column
|
||||
$wildcards = empty($filters[TRUE]) ? array() : $filters[TRUE];
|
||||
|
Reference in New Issue
Block a user