2012-11-22 03:25:06 +00:00
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.');
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
|
|
|
* [Object Relational Mapping][ref-orm] (ORM) is a method of abstracting database
|
|
|
|
* access to standard PHP calls. All table rows are represented as model objects,
|
|
|
|
* with object properties representing row data. ORM in Kohana generally follows
|
|
|
|
* the [Active Record][ref-act] pattern.
|
|
|
|
*
|
|
|
|
* [ref-orm]: http://wikipedia.org/wiki/Object-relational_mapping
|
|
|
|
* [ref-act]: http://wikipedia.org/wiki/Active_record
|
|
|
|
*
|
|
|
|
* @package Kohana/ORM
|
|
|
|
* @author Kohana Team
|
2012-11-22 03:25:06 +00:00
|
|
|
* @copyright (c) 2007-2012 Kohana Team
|
2011-01-13 14:49:56 +00:00
|
|
|
* @license http://kohanaframework.org/license
|
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
class Kohana_ORM extends Model implements serializable {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores column information for ORM models
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_column_cache = array();
|
2012-11-22 03:25:06 +00:00
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Initialization storage for ORM models
|
2011-05-16 12:47:16 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-11-22 03:25:06 +00:00
|
|
|
protected static $_init_cache = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Creates and returns a new model.
|
|
|
|
* Model name must be passed with its' original casing, e.g.
|
|
|
|
*
|
|
|
|
* $model = ORM::factory('User_Token');
|
2011-05-16 12:47:16 +00:00
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @param string $model Model name
|
|
|
|
* @param mixed $id Parameter for find()
|
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
public static function factory($model, $id = NULL)
|
|
|
|
{
|
|
|
|
// Set class name
|
2012-11-22 03:25:06 +00:00
|
|
|
$model = 'Model_'.$model;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
return new $model($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Has one" relationships
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_has_one = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Belongs to" relationships
|
|
|
|
* @var array
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
protected $_belongs_to = array();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* "Has many" relationships
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_has_many = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Relationships that should always be joined
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_load_with = array();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Validation object created before saving/updating
|
|
|
|
* @var Validation
|
|
|
|
*/
|
|
|
|
protected $_validation = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current object
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_object = array();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_changed = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_original_values = array();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_related = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_valid = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_loaded = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_saved = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_sorting;
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Foreign key suffix
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_foreign_key_suffix = '_id';
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Model name
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_object_name;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Plural model name
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_object_plural;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table name
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_table_name;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table columns
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_table_columns;
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Auto-update columns for updates
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_updated_column = NULL;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-update columns for creation
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_created_column = NULL;
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* Auto-serialize and unserialize columns on get/set
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_serialize_columns = array();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Table primary key
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_primary_key = 'id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Primary key value
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $_primary_key_value;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Model configuration, table names plural?
|
|
|
|
* @var bool
|
|
|
|
*/
|
2011-01-13 14:49:56 +00:00
|
|
|
protected $_table_names_plural = TRUE;
|
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
// 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;
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Model configuration, reload on wakeup?
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_reload_on_wakeup = TRUE;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Database Object
|
|
|
|
* @var Database
|
|
|
|
*/
|
|
|
|
protected $_db = NULL;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Database config group
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $_db_group = NULL;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Database methods applied
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_db_applied = array();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Database methods pending
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_db_pending = array();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Reset builder
|
|
|
|
* @var bool
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
protected $_db_reset = TRUE;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Database query builder
|
2012-11-22 03:25:06 +00:00
|
|
|
* @var Database_Query_Builder_Select
|
2011-05-16 12:47:16 +00:00
|
|
|
*/
|
|
|
|
protected $_db_builder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* With calls already applied
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_with_applied = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data to be loaded into the model from a database call cast
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_cast_data = array();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* The message filename used for validation errors.
|
|
|
|
* Defaults to ORM::$_object_name
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_errors_filename = NULL;
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Constructs a new model and loads a record if given
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param mixed $id Parameter for find or object to load
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function __construct($id = NULL)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
|
|
|
|
|
|
|
if ($id !== NULL)
|
|
|
|
{
|
|
|
|
if (is_array($id))
|
|
|
|
{
|
|
|
|
foreach ($id as $column => $value)
|
|
|
|
{
|
|
|
|
// Passing an array of column => values
|
|
|
|
$this->where($column, '=', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->find();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Passing the primary key
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->where(($this->_disable_join_table_name ? '' : $this->_object_name.'.').$this->_primary_key, '=', $id)->find();
|
2011-05-16 12:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ( ! empty($this->_cast_data))
|
|
|
|
{
|
|
|
|
// Load preloaded data from a database call cast
|
|
|
|
$this->_load_values($this->_cast_data);
|
|
|
|
|
|
|
|
$this->_cast_data = array();
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
/**
|
|
|
|
* Prepares the model database connection, determines the table name,
|
|
|
|
* and loads column information.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _initialize()
|
|
|
|
{
|
|
|
|
// Set the object name and plural name
|
|
|
|
$this->_object_name = strtolower(substr(get_class($this), 6));
|
2012-11-22 03:25:06 +00:00
|
|
|
|
|
|
|
// Check if this model has already been initialized
|
|
|
|
if ( ! $init = Arr::get(ORM::$_init_cache, $this->_object_name, FALSE))
|
|
|
|
{
|
|
|
|
$init = array(
|
|
|
|
'_belongs_to' => array(),
|
|
|
|
'_has_one' => array(),
|
|
|
|
'_has_many' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set the object plural name if none predefined
|
|
|
|
if ( ! isset($this->_object_plural))
|
|
|
|
{
|
|
|
|
$init['_object_plural'] = Inflector::plural($this->_object_name);
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
if ( ! $this->_errors_filename)
|
|
|
|
{
|
|
|
|
$init['_errors_filename'] = $this->_object_name;
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
if ( ! is_object($this->_db))
|
|
|
|
{
|
|
|
|
// Get database instance
|
|
|
|
$init['_db'] = Database::instance($this->_db_group);
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
if (empty($this->_table_name))
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
// Table name is the same as the object name
|
|
|
|
$init['_table_name'] = $this->_object_name;
|
|
|
|
|
|
|
|
if ($this->_table_names_plural === TRUE)
|
|
|
|
{
|
|
|
|
// Make the table name plural
|
|
|
|
$init['_table_name'] = Arr::get($init, '_object_plural', $this->_object_plural);
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
|
|
|
|
$defaults = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
foreach ($this->_belongs_to as $alias => $details)
|
|
|
|
{
|
|
|
|
if ( ! isset($details['model']))
|
|
|
|
{
|
|
|
|
$defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', $alias)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaults['foreign_key'] = $alias.$this->_foreign_key_suffix;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
$init['_belongs_to'][$alias] = array_merge($defaults, $details);
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
foreach ($this->_has_one as $alias => $details)
|
|
|
|
{
|
|
|
|
if ( ! isset($details['model']))
|
|
|
|
{
|
|
|
|
$defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', $alias)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
|
|
|
|
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
$init['_has_one'][$alias] = array_merge($defaults, $details);
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
foreach ($this->_has_many as $alias => $details)
|
|
|
|
{
|
|
|
|
if ( ! isset($details['model']))
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
$defaults['through'] = NULL;
|
|
|
|
|
|
|
|
if ( ! isset($details['far_key']))
|
|
|
|
{
|
|
|
|
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
$init['_has_many'][$alias] = array_merge($defaults, $details);
|
|
|
|
}
|
|
|
|
|
|
|
|
ORM::$_init_cache[$this->_object_name] = $init;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign initialized properties to the current object
|
|
|
|
foreach ($init as $property => $value)
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->{$property} = $value;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Load column information
|
|
|
|
$this->reload_columns();
|
|
|
|
|
|
|
|
// Clear initial model state
|
|
|
|
$this->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes validation rules, and labels
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _validation()
|
|
|
|
{
|
|
|
|
// Build the validation object with its rules
|
|
|
|
$this->_validation = Validation::factory($this->_object)
|
2012-11-22 03:25:06 +00:00
|
|
|
->bind(':model', $this)
|
|
|
|
->bind(':original_values', $this->_original_values)
|
|
|
|
->bind(':changed', $this->_changed);
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
foreach ($this->rules() as $field => $rules)
|
|
|
|
{
|
|
|
|
$this->_validation->rules($field, $rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use column names by default for labels
|
|
|
|
$columns = array_keys($this->_table_columns);
|
|
|
|
|
|
|
|
// Merge user-defined labels
|
|
|
|
$labels = array_merge(array_combine($columns, $columns), $this->labels());
|
|
|
|
|
|
|
|
foreach ($labels as $field => $label)
|
|
|
|
{
|
|
|
|
$this->_validation->label($field, $label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload column definitions.
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @param boolean $force Force reloading
|
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
public function reload_columns($force = FALSE)
|
|
|
|
{
|
|
|
|
if ($force === TRUE OR empty($this->_table_columns))
|
|
|
|
{
|
|
|
|
if (isset(ORM::$_column_cache[$this->_object_name]))
|
|
|
|
{
|
|
|
|
// Use cached column information
|
|
|
|
$this->_table_columns = ORM::$_column_cache[$this->_object_name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Grab column information from database
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_table_columns = $this->list_columns();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Load column cache
|
|
|
|
ORM::$_column_cache[$this->_object_name] = $this->_table_columns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unloads the current object and clears the status.
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
// Create an array with all the columns set to NULL
|
|
|
|
$values = array_combine(array_keys($this->_table_columns), array_fill(0, count($this->_table_columns), NULL));
|
|
|
|
|
|
|
|
// Replace the object and reset the object status
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_object = $this->_changed = $this->_related = $this->_original_values = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Replace the current object with an empty one
|
|
|
|
$this->_load_values($values);
|
|
|
|
|
|
|
|
// Reset primary key
|
|
|
|
$this->_primary_key_value = NULL;
|
2012-11-22 03:25:06 +00:00
|
|
|
|
|
|
|
// Reset the loaded state
|
|
|
|
$this->_loaded = FALSE;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
$this->reset();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reloads the current object from the database.
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$primary_key = $this->pk();
|
|
|
|
|
|
|
|
// Replace the object and reset the object status
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_object = $this->_changed = $this->_related = $this->_original_values = array();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Only reload the object if we have one to reload
|
|
|
|
if ($this->_loaded)
|
|
|
|
return $this->clear()
|
2012-11-22 03:25:06 +00:00
|
|
|
->where($this->_object_name.'.'.$this->_primary_key, '=', $primary_key)
|
2011-05-16 12:47:16 +00:00
|
|
|
->find();
|
|
|
|
else
|
|
|
|
return $this->clear();
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if object data is set.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $column Column name
|
|
|
|
* @return boolean
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function __isset($column)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
return (isset($this->_object[$column]) OR
|
2011-01-13 14:49:56 +00:00
|
|
|
isset($this->_related[$column]) OR
|
|
|
|
isset($this->_has_one[$column]) OR
|
|
|
|
isset($this->_belongs_to[$column]) OR
|
2011-05-16 12:47:16 +00:00
|
|
|
isset($this->_has_many[$column]));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets object data.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $column Column name
|
|
|
|
* @return void
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function __unset($column)
|
|
|
|
{
|
|
|
|
unset($this->_object[$column], $this->_changed[$column], $this->_related[$column]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the primary key of a model when it is converted to a string.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return string
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return (string) $this->pk();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows serialization of only the object data and state, to prevent
|
|
|
|
* "stale" objects being unserialized, which also requires less memory.
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @return string
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function serialize()
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
// Store only information about the object
|
2012-11-22 03:25:06 +00:00
|
|
|
foreach (array('_primary_key_value', '_object', '_changed', '_loaded', '_saved', '_sorting', '_original_values') as $var)
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
|
|
|
$data[$var] = $this->{$var};
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialize($data);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* Check whether the model data has been modified.
|
|
|
|
* If $field is specified, checks whether that field was modified.
|
|
|
|
*
|
|
|
|
* @param string $field field to check for changes
|
|
|
|
* @return bool Whether or not the field has changed
|
|
|
|
*/
|
|
|
|
public function changed($field = NULL)
|
|
|
|
{
|
|
|
|
return ($field === NULL)
|
|
|
|
? $this->_changed
|
|
|
|
: Arr::get($this->_changed, $field);
|
|
|
|
}
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
|
|
|
* Prepares the database connection and reloads the object.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $data String for unserialization
|
2011-01-13 14:49:56 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function unserialize($data)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Initialize model
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->_initialize();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach (unserialize($data) as $name => $var)
|
|
|
|
{
|
|
|
|
$this->{$name} = $var;
|
|
|
|
}
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
if ($this->_reload_on_wakeup === TRUE)
|
|
|
|
{
|
|
|
|
// Reload the object
|
|
|
|
$this->reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Handles retrieval of all model values, relationships, and metadata.
|
|
|
|
* [!!] This should not be overridden.
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @param string $column Column name
|
2011-01-13 14:49:56 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-11-22 03:25:06 +00:00
|
|
|
public function __get($column)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
return $this->get($column);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Handles getting of column
|
|
|
|
* Override this method to add custom get behavior
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $column Column name
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
|
|
|
* @return mixed
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2012-11-22 03:25:06 +00:00
|
|
|
public function get($column)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
if (array_key_exists($column, $this->_object))
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
return (in_array($column, $this->_serialize_columns))
|
|
|
|
? $this->_unserialize_value($this->_object[$column])
|
|
|
|
: $this->_object[$column];
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
elseif (isset($this->_related[$column]))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Return related model that has already been fetched
|
2011-01-13 14:49:56 +00:00
|
|
|
return $this->_related[$column];
|
|
|
|
}
|
|
|
|
elseif (isset($this->_belongs_to[$column]))
|
|
|
|
{
|
|
|
|
$model = $this->_related($column);
|
|
|
|
|
|
|
|
// Use this model's column and foreign model's primary key
|
2012-11-22 03:25:06 +00:00
|
|
|
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$model->_primary_key;
|
2011-01-13 14:49:56 +00:00
|
|
|
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
return $this->_related[$column] = $model;
|
|
|
|
}
|
|
|
|
elseif (isset($this->_has_one[$column]))
|
|
|
|
{
|
|
|
|
$model = $this->_related($column);
|
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
if (! is_array($this->_has_one[$column]['foreign_key']))
|
|
|
|
{
|
|
|
|
// Use this model's primary key value and foreign model's column
|
2012-11-22 03:25:06 +00:00
|
|
|
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$this->_has_one[$column]['foreign_key'];
|
2011-05-23 10:02:00 +00:00
|
|
|
$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
|
2012-11-22 03:25:06 +00:00
|
|
|
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$fk;
|
2011-05-23 10:02:00 +00:00
|
|
|
$val = $this->_object[$fk];
|
|
|
|
|
|
|
|
$model = $model->where($col, '=', $val);
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
$model->where($col, '=', $val)->find();
|
|
|
|
|
|
|
|
return $this->_related[$column] = $model;
|
|
|
|
}
|
|
|
|
elseif (isset($this->_has_many[$column]))
|
|
|
|
{
|
|
|
|
$model = ORM::factory($this->_has_many[$column]['model']);
|
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
if (! is_array($this->_has_many[$column]['foreign_key']))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-23 10:02:00 +00:00
|
|
|
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 = ($this->_disable_join_table_name ? '' : $through.'.').$this->_has_many[$column]['far_key'];
|
2012-11-22 03:25:06 +00:00
|
|
|
$join_col2 = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$model->_primary_key;
|
2011-04-05 23:06:27 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
$model->join($through)->on($join_col1, '=', $join_col2)
|
|
|
|
->on(($this->_disable_join_table_name ? '' : $through.'.').'site_id', '=', ($this->_disable_join_table_name ? '' : $model->_object_name.'.').'site_id');
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
// 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
|
2012-11-22 03:25:06 +00:00
|
|
|
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$this->_has_many[$column]['foreign_key'];
|
2011-05-23 10:02:00 +00:00
|
|
|
$val = $this->_object[$this->_has_many[$column]['far_key']];
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
return $model->where($col, '=', $val);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-24 23:47:06 +00:00
|
|
|
foreach ($this->_has_many[$column]['foreign_key'] as $mk => $fk)
|
2011-05-23 10:02:00 +00:00
|
|
|
{
|
|
|
|
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
|
2012-11-22 03:25:06 +00:00
|
|
|
$col = ($this->_disable_join_table_name ? '' : $model->_object_name.'.').$fk;
|
2011-06-24 23:47:06 +00:00
|
|
|
$val = $this->_object[$mk];
|
2011-05-23 10:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$model = $model->where($col, '=', $val);
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
return $model;
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Kohana_Exception('The :property property does not exist in the :class class',
|
|
|
|
array(':property' => $column, ':class' => get_class($this)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Base set method.
|
|
|
|
* [!!] This should not be overridden.
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $column Column name
|
|
|
|
* @param mixed $value Column value
|
|
|
|
* @return void
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function __set($column, $value)
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->set($column, $value);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Handles setting of columns
|
|
|
|
* Override this method to add custom set behavior
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $column Column name
|
|
|
|
* @param mixed $value Column value
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function set($column, $value)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
if ( ! isset($this->_object_name))
|
|
|
|
{
|
|
|
|
// Object not yet constructed, so we're loading data from a database call cast
|
|
|
|
$this->_cast_data[$column] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($column, $this->_serialize_columns))
|
|
|
|
{
|
|
|
|
$value = $this->_serialize_value($value);
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if (array_key_exists($column, $this->_object))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Filter the data
|
|
|
|
$value = $this->run_filter($column, $value);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// See if the data really changed
|
|
|
|
if ($value !== $this->_object[$column])
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$this->_object[$column] = $value;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Data has changed
|
|
|
|
$this->_changed[$column] = $column;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Object is no longer saved or valid
|
|
|
|
$this->_saved = $this->_valid = FALSE;
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
elseif (isset($this->_belongs_to[$column]))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Update related object itself
|
|
|
|
$this->_related[$column] = $value;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Update the foreign key of this model
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_object[$this->_belongs_to[$column]['foreign_key']] = ($value instanceof ORM)
|
|
|
|
? $value->pk()
|
|
|
|
: NULL;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$this->_changed[$column] = $this->_belongs_to[$column]['foreign_key'];
|
|
|
|
}
|
|
|
|
else
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
throw new Kohana_Exception('The :property: property does not exist in the :class: class',
|
|
|
|
array(':property:' => $column, ':class:' => get_class($this)));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
return $this;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Set values from an array with support for one-one relationships. This method should be used
|
|
|
|
* for loading in post data, etc.
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param array $values Array of column => val
|
|
|
|
* @param array $expected Array of keys to take from $values
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function values(array $values, array $expected = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Default to expecting everything except the primary key
|
|
|
|
if ($expected === NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$expected = array_keys($this->_table_columns);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Don't set the primary key by default
|
|
|
|
unset($values[$this->_primary_key]);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach ($expected as $key => $column)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if (is_string($key))
|
|
|
|
{
|
|
|
|
// isset() fails when the value is NULL (we want it to pass)
|
|
|
|
if ( ! array_key_exists($key, $values))
|
|
|
|
continue;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Try to set values to a related model
|
|
|
|
$this->{$key}->values($values[$key], $column);
|
|
|
|
}
|
|
|
|
else
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// isset() fails when the value is NULL (we want it to pass)
|
|
|
|
if ( ! array_key_exists($column, $values))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update the column, respects __set()
|
|
|
|
$this->$column = $values[$column];
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the values of this object as an array, including any related one-one
|
|
|
|
* models that have already been loaded using with()
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return array
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function as_array()
|
|
|
|
{
|
|
|
|
$object = array();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach ($this->_object as $column => $value)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
// Call __get for any user processing
|
2011-05-16 12:47:16 +00:00
|
|
|
$object[$column] = $this->__get($column);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach ($this->_related as $column => $model)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
// Include any related objects that are already loaded
|
2011-05-16 12:47:16 +00:00
|
|
|
$object[$column] = $model->as_array();
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds another one-to-one object to this model. One-to-one objects
|
|
|
|
* can be nested using 'object1:object2' syntax
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $target_path Target model to bind to
|
2012-11-22 03:25:06 +00:00
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function with($target_path)
|
|
|
|
{
|
|
|
|
if (isset($this->_with_applied[$target_path]))
|
|
|
|
{
|
|
|
|
// Don't join anything already joined
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split object parts
|
|
|
|
$aliases = explode(':', $target_path);
|
2011-05-16 12:47:16 +00:00
|
|
|
$target = $this;
|
2011-01-13 14:49:56 +00:00
|
|
|
foreach ($aliases as $alias)
|
|
|
|
{
|
|
|
|
// Go down the line of objects to find the given target
|
|
|
|
$parent = $target;
|
|
|
|
$target = $parent->_related($alias);
|
|
|
|
|
|
|
|
if ( ! $target)
|
|
|
|
{
|
|
|
|
// Can't find related object
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Target alias is at the end
|
|
|
|
$target_alias = $alias;
|
|
|
|
|
|
|
|
// Pop-off top alias to get the parent path (user:photo:tag becomes user:photo - the parent table prefix)
|
|
|
|
array_pop($aliases);
|
|
|
|
$parent_path = implode(':', $aliases);
|
|
|
|
|
|
|
|
if (empty($parent_path))
|
|
|
|
{
|
|
|
|
// Use this table name itself for the parent path
|
2012-11-22 03:25:06 +00:00
|
|
|
$parent_path = $this->_object_name;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_with_applied[$parent_path]))
|
|
|
|
{
|
|
|
|
// If the parent path hasn't been joined yet, do it first (otherwise LEFT JOINs fail)
|
|
|
|
$this->with($parent_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to with_applied to prevent duplicate joins
|
|
|
|
$this->_with_applied[$target_path] = TRUE;
|
|
|
|
|
|
|
|
// Use the keys of the empty object to determine the columns
|
|
|
|
foreach (array_keys($target->_object) as $column)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$name = $target_path.'.'.$column;
|
|
|
|
$alias = $target_path.':'.$column;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Add the prefix so that load_result can determine the relationship
|
|
|
|
$this->select(array($name, $alias));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($parent->_belongs_to[$target_alias]))
|
|
|
|
{
|
|
|
|
// Parent belongs_to target, use target's primary key and parent's foreign key
|
|
|
|
$join_col1 = $target_path.'.'.$target->_primary_key;
|
|
|
|
$join_col2 = $parent_path.'.'.$parent->_belongs_to[$target_alias]['foreign_key'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Parent has_one target, use parent's primary key as target's foreign key
|
|
|
|
$join_col1 = $parent_path.'.'.$parent->_primary_key;
|
|
|
|
$join_col2 = $target_path.'.'.$parent->_has_one[$target_alias]['foreign_key'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join the related object into the result
|
|
|
|
$this->join(array($target->_table_name, $target_path), 'LEFT')->on($join_col1, '=', $join_col2);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the Database Builder to given query type
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param integer $type Type of Database query
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
protected function _build($type)
|
|
|
|
{
|
|
|
|
// Construct new builder object based on query type
|
|
|
|
switch ($type)
|
|
|
|
{
|
|
|
|
case Database::SELECT:
|
|
|
|
$this->_db_builder = DB::select();
|
|
|
|
break;
|
|
|
|
case Database::UPDATE:
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_db_builder = DB::update(array($this->_table_name, $this->_object_name));
|
2011-01-13 14:49:56 +00:00
|
|
|
break;
|
|
|
|
case Database::DELETE:
|
2012-11-22 03:25:06 +00:00
|
|
|
// Cannot use an alias for DELETE queries
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->_db_builder = DB::delete($this->_table_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process pending database method calls
|
|
|
|
foreach ($this->_db_pending as $method)
|
|
|
|
{
|
|
|
|
$name = $method['name'];
|
|
|
|
$args = $method['args'];
|
|
|
|
|
|
|
|
$this->_db_applied[$name] = $name;
|
|
|
|
|
|
|
|
call_user_func_array(array($this->_db_builder, $name), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds and loads a single database row into the object.
|
|
|
|
*
|
|
|
|
* @chainable
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function find()
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if ($this->_loaded)
|
|
|
|
throw new Kohana_Exception('Method find() cannot be called on loaded objects');
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
if ( ! empty($this->_load_with))
|
|
|
|
{
|
|
|
|
foreach ($this->_load_with as $alias)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Bind auto relationships
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->with($alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_build(Database::SELECT);
|
|
|
|
|
|
|
|
return $this->_load_result(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds multiple database rows and returns an iterator of the rows found.
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return Database_Result
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function find_all()
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if ($this->_loaded)
|
|
|
|
throw new Kohana_Exception('Method find_all() cannot be called on loaded objects');
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
if ( ! empty($this->_load_with))
|
|
|
|
{
|
|
|
|
foreach ($this->_load_with as $alias)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Bind auto relationships
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->with($alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_build(Database::SELECT);
|
|
|
|
|
|
|
|
return $this->_load_result(TRUE);
|
|
|
|
}
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of columns to include in the select query. This method
|
|
|
|
* can be overridden to change the default select behavior.
|
|
|
|
*
|
|
|
|
* @return array Columns to select
|
|
|
|
*/
|
|
|
|
protected function _build_select()
|
|
|
|
{
|
|
|
|
$columns = array();
|
|
|
|
|
|
|
|
foreach ($this->_table_columns as $column => $_)
|
|
|
|
{
|
|
|
|
$columns[] = array($this->_object_name.'.'.$column, $column);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Loads a database result, either as a new record for this model, or as
|
|
|
|
* an iterator for multiple rows.
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @chainable
|
|
|
|
* @param bool $multiple Return an iterator or load a single row
|
|
|
|
* @return ORM|Database_Result
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
protected function _load_result($multiple = FALSE)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_db_builder->from(array($this->_table_name, $this->_object_name));
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2011-05-23 10:02:00 +00:00
|
|
|
if ($multiple === FALSE AND ! $this->_disable_limit)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Only fetch 1 record
|
|
|
|
$this->_db_builder->limit(1);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Select all columns by default
|
2011-05-23 10:02:00 +00:00
|
|
|
if (! $this->_disable_wild_select)
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_db_builder->select_array($this->_build_select());
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach ($this->_sorting as $column => $direction)
|
|
|
|
{
|
|
|
|
if (strpos($column, '.') === FALSE)
|
|
|
|
{
|
|
|
|
// Sorting column for use in JOINs
|
2012-11-22 03:25:06 +00:00
|
|
|
$column = ($this->_disable_join_table_name ? '' : $this->_object_name.'.').$column;
|
2011-05-16 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_db_builder->order_by($column, $direction);
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if ($multiple === TRUE)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Return database iterator casting to this object type
|
|
|
|
$result = $this->_db_builder->as_object(get_class($this))->execute($this->_db);
|
|
|
|
|
|
|
|
$this->reset();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
return $result;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Load the result as an associative array
|
|
|
|
$result = $this->_db_builder->as_assoc()->execute($this->_db);
|
|
|
|
|
|
|
|
$this->reset();
|
|
|
|
|
|
|
|
if ($result->count() === 1)
|
|
|
|
{
|
|
|
|
// Load object values
|
|
|
|
$this->_load_values($result->current());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Clear the object, nothing was found
|
|
|
|
$this->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads an array of values into into the current object.
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @param array $values Values to load
|
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
protected function _load_values(array $values)
|
|
|
|
{
|
|
|
|
if (array_key_exists($this->_primary_key, $values))
|
|
|
|
{
|
|
|
|
if ($values[$this->_primary_key] !== NULL)
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
// Flag as loaded and valid
|
|
|
|
$this->_loaded = $this->_valid = TRUE;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Store primary key
|
|
|
|
$this->_primary_key_value = $values[$this->_primary_key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
// Not loaded or valid
|
|
|
|
$this->_loaded = $this->_valid = FALSE;
|
2011-05-16 12:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Related objects
|
|
|
|
$related = array();
|
|
|
|
|
|
|
|
foreach ($values as $column => $value)
|
|
|
|
{
|
|
|
|
if (strpos($column, ':') === FALSE)
|
|
|
|
{
|
|
|
|
// Load the value to this model
|
|
|
|
$this->_object[$column] = $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Column belongs to a related model
|
|
|
|
list ($prefix, $column) = explode(':', $column, 2);
|
|
|
|
|
|
|
|
$related[$prefix][$column] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($related))
|
|
|
|
{
|
|
|
|
foreach ($related as $object => $values)
|
|
|
|
{
|
|
|
|
// Load the related objects with the values in the result
|
|
|
|
$this->_related($object)->_load_values($values);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
if ($this->_loaded)
|
|
|
|
{
|
|
|
|
// Store the object in its original state
|
|
|
|
$this->_original_values = $this->_object;
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rule definitions for validation
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters a value for a specific column
|
|
|
|
*
|
|
|
|
* @param string $field The column name
|
|
|
|
* @param string $value The value to filter
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-23 10:02:00 +00:00
|
|
|
protected function run_filter($field, $value, $filters=NULL)
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
2011-05-23 10:02:00 +00:00
|
|
|
if (is_null($filters))
|
|
|
|
$filters = $this->filters();
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
// Get the filters for this column
|
|
|
|
$wildcards = empty($filters[TRUE]) ? array() : $filters[TRUE];
|
|
|
|
|
|
|
|
// Merge in the wildcards
|
|
|
|
$filters = empty($filters[$field]) ? $wildcards : array_merge($wildcards, $filters[$field]);
|
|
|
|
|
|
|
|
// Bind the field name and model so they can be used in the filter method
|
|
|
|
$_bound = array
|
|
|
|
(
|
|
|
|
':field' => $field,
|
|
|
|
':model' => $this,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($filters as $array)
|
|
|
|
{
|
|
|
|
// Value needs to be bound inside the loop so we are always using the
|
|
|
|
// version that was modified by the filters that already ran
|
|
|
|
$_bound[':value'] = $value;
|
|
|
|
|
|
|
|
// Filters are defined as array($filter, $params)
|
|
|
|
$filter = $array[0];
|
|
|
|
$params = Arr::get($array, 1, array(':value'));
|
|
|
|
|
|
|
|
foreach ($params as $key => $param)
|
|
|
|
{
|
|
|
|
if (is_string($param) AND array_key_exists($param, $_bound))
|
|
|
|
{
|
|
|
|
// Replace with bound value
|
|
|
|
$params[$key] = $_bound[$param];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($filter) OR ! is_string($filter))
|
|
|
|
{
|
|
|
|
// This is either a callback as an array or a lambda
|
|
|
|
$value = call_user_func_array($filter, $params);
|
|
|
|
}
|
|
|
|
elseif (strpos($filter, '::') === FALSE)
|
|
|
|
{
|
|
|
|
// Use a function call
|
|
|
|
$function = new ReflectionFunction($filter);
|
|
|
|
|
|
|
|
// Call $function($this[$field], $param, ...) with Reflection
|
|
|
|
$value = $function->invokeArgs($params);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Split the class and method of the rule
|
|
|
|
list($class, $method) = explode('::', $filter, 2);
|
|
|
|
|
|
|
|
// Use a static method call
|
|
|
|
$method = new ReflectionMethod($class, $method);
|
|
|
|
|
|
|
|
// Call $Class::$method($this[$field], $param, ...) with Reflection
|
|
|
|
$value = $method->invokeArgs(NULL, $params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter definitions for validation
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function filters()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Label definitions for validation
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function labels()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the current model's data
|
|
|
|
*
|
|
|
|
* @param Validation $extra_validation Validation object
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws ORM_Validation_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return ORM
|
|
|
|
*/
|
|
|
|
public function check(Validation $extra_validation = NULL)
|
|
|
|
{
|
|
|
|
// Determine if any external validation failed
|
|
|
|
$extra_errors = ($extra_validation AND ! $extra_validation->check());
|
|
|
|
|
|
|
|
// Always build a new validation object
|
|
|
|
$this->_validation();
|
|
|
|
|
|
|
|
$array = $this->_validation;
|
|
|
|
|
|
|
|
if (($this->_valid = $array->check()) === FALSE OR $extra_errors)
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
$exception = new ORM_Validation_Exception($this->errors_filename(), $array);
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
if ($extra_errors)
|
|
|
|
{
|
|
|
|
// Merge any possible errors from the external object
|
|
|
|
$exception->add_object('_external', $extra_validation);
|
|
|
|
}
|
|
|
|
throw $exception;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Insert a new object to the database
|
|
|
|
* @param Validation $validation Validation object
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function create(Validation $validation = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if ($this->_loaded)
|
|
|
|
throw new Kohana_Exception('Cannot create :model model because it is already loaded.', array(':model' => $this->_object_name));
|
|
|
|
|
|
|
|
// Require model validation before saving
|
2012-11-22 03:25:06 +00:00
|
|
|
if ( ! $this->_valid OR $validation)
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
|
|
|
$this->check($validation);
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
$data = array();
|
|
|
|
foreach ($this->_changed as $column)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Generate list of column => values
|
2011-01-13 14:49:56 +00:00
|
|
|
$data[$column] = $this->_object[$column];
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if (is_array($this->_created_column))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Fill the created column
|
|
|
|
$column = $this->_created_column['column'];
|
|
|
|
$format = $this->_created_column['format'];
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$result = DB::insert($this->_table_name)
|
|
|
|
->columns(array_keys($data))
|
|
|
|
->values(array_values($data))
|
|
|
|
->execute($this->_db);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if ( ! array_key_exists($this->_primary_key, $data))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Load the insert id as the primary key if it was left out
|
|
|
|
$this->_object[$this->_primary_key] = $this->_primary_key_value = $result[0];
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->_primary_key_value = $this->_object[$this->_primary_key];
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Object is now loaded and saved
|
|
|
|
$this->_loaded = $this->_saved = TRUE;
|
|
|
|
|
|
|
|
// All changes have been saved
|
|
|
|
$this->_changed = array();
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_original_values = $this->_object;
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Updates a single record or multiple records
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
|
|
|
* @chainable
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param Validation $validation Validation object
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function update(Validation $validation = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if ( ! $this->_loaded)
|
|
|
|
throw new Kohana_Exception('Cannot update :model model because it is not loaded.', array(':model' => $this->_object_name));
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
// Run validation if the model isn't valid or we have additional validation rules.
|
|
|
|
if ( ! $this->_valid OR $validation)
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->check($validation);
|
2011-05-16 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
if (empty($this->_changed))
|
2011-05-16 12:47:16 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
// Nothing to update
|
|
|
|
return $this;
|
2011-05-16 12:47:16 +00:00
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
$data = array();
|
|
|
|
foreach ($this->_changed as $column)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Compile changed data
|
2011-01-13 14:49:56 +00:00
|
|
|
$data[$column] = $this->_object[$column];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($this->_updated_column))
|
|
|
|
{
|
|
|
|
// Fill the updated column
|
|
|
|
$column = $this->_updated_column['column'];
|
|
|
|
$format = $this->_updated_column['format'];
|
|
|
|
|
|
|
|
$data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Use primary key value
|
|
|
|
$id = $this->pk();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Update a single record
|
|
|
|
DB::update($this->_table_name)
|
|
|
|
->set($data)
|
|
|
|
->where($this->_primary_key, '=', $id)
|
|
|
|
->execute($this->_db);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if (isset($data[$this->_primary_key]))
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
// Primary key was changed, reflect it
|
|
|
|
$this->_primary_key_value = $data[$this->_primary_key];
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Object has been saved
|
|
|
|
$this->_saved = TRUE;
|
|
|
|
|
|
|
|
// All changes have been saved
|
|
|
|
$this->_changed = array();
|
2012-11-22 03:25:06 +00:00
|
|
|
$this->_original_values = $this->_object;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Updates or Creates the record depending on loaded()
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
|
|
|
* @chainable
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param Validation $validation Validation object
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function save(Validation $validation = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
return $this->loaded() ? $this->update($validation) : $this->create($validation);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 03:25:06 +00:00
|
|
|
* Deletes a single record while ignoring relationships.
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
|
|
|
* @chainable
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Kohana_Exception
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function delete()
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
if ( ! $this->_loaded)
|
|
|
|
throw new Kohana_Exception('Cannot delete :model model because it is not loaded.', array(':model' => $this->_object_name));
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Use primary key value
|
|
|
|
$id = $this->pk();
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Delete the object
|
|
|
|
DB::delete($this->_table_name)
|
|
|
|
->where($this->_primary_key, '=', $id)
|
|
|
|
->execute($this->_db);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
return $this->clear();
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-16 12:47:16 +00:00
|
|
|
* Tests if this object has a relationship to a different model,
|
2012-11-22 03:25:06 +00:00
|
|
|
* or an array of different models. When providing far keys, the number
|
|
|
|
* of relations must equal the number of keys.
|
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* // Check if $model has the login role
|
|
|
|
* $model->has('roles', ORM::factory('role', array('name' => 'login')));
|
|
|
|
* // Check for the login role if you know the roles.id is 5
|
|
|
|
* $model->has('roles', 5);
|
|
|
|
* // Check for all of the following roles
|
|
|
|
* $model->has('roles', array(1, 2, 3, 4));
|
2012-11-22 03:25:06 +00:00
|
|
|
* // Check if $model has any roles
|
|
|
|
* $model->has('roles')
|
|
|
|
*
|
|
|
|
* @param string $alias Alias of the has_many "through" relationship
|
|
|
|
* @param mixed $far_keys Related model, primary key, or an array of primary keys
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function has($alias, $far_keys = NULL)
|
|
|
|
{
|
|
|
|
$count = $this->count_relations($alias, $far_keys);
|
|
|
|
if ($far_keys === NULL)
|
|
|
|
{
|
|
|
|
return (bool) $count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $count === count($far_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-05-16 12:47:16 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
/**
|
|
|
|
* Tests if this object has a relationship to a different model,
|
|
|
|
* or an array of different models. When providing far keys, this function
|
|
|
|
* only checks that at least one of the relationships is satisfied.
|
|
|
|
*
|
|
|
|
* // Check if $model has the login role
|
|
|
|
* $model->has('roles', ORM::factory('role', array('name' => 'login')));
|
|
|
|
* // Check for the login role if you know the roles.id is 5
|
|
|
|
* $model->has('roles', 5);
|
|
|
|
* // Check for any of the following roles
|
|
|
|
* $model->has('roles', array(1, 2, 3, 4));
|
|
|
|
* // Check if $model has any roles
|
|
|
|
* $model->has('roles')
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $alias Alias of the has_many "through" relationship
|
|
|
|
* @param mixed $far_keys Related model, primary key, or an array of primary keys
|
2012-11-22 03:25:06 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function has_any($alias, $far_keys = NULL)
|
|
|
|
{
|
|
|
|
return (bool) $this->count_relations($alias, $far_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of relationships
|
|
|
|
*
|
|
|
|
* // Counts the number of times the login role is attached to $model
|
|
|
|
* $model->has('roles', ORM::factory('role', array('name' => 'login')));
|
|
|
|
* // Counts the number of times role 5 is attached to $model
|
|
|
|
* $model->has('roles', 5);
|
|
|
|
* // Counts the number of times any of roles 1, 2, 3, or 4 are attached to
|
|
|
|
* // $model
|
|
|
|
* $model->has('roles', array(1, 2, 3, 4));
|
|
|
|
* // Counts the number roles attached to $model
|
|
|
|
* $model->has('roles')
|
|
|
|
*
|
|
|
|
* @param string $alias Alias of the has_many "through" relationship
|
|
|
|
* @param mixed $far_keys Related model, primary key, or an array of primary keys
|
|
|
|
* @return integer
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2012-11-22 03:25:06 +00:00
|
|
|
public function count_relations($alias, $far_keys = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
if ($far_keys === NULL)
|
|
|
|
{
|
|
|
|
return (int) DB::select(array(DB::expr('COUNT(*)'), 'records_found'))
|
|
|
|
->from($this->_has_many[$alias]['through'])
|
|
|
|
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
|
|
|
|
->execute($this->_db)->get('records_found');
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// We need an array to simplify the logic
|
|
|
|
$far_keys = (array) $far_keys;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
// Nothing to check if the model isn't loaded or we don't have any far_keys
|
|
|
|
if ( ! $far_keys OR ! $this->_loaded)
|
2012-11-22 03:25:06 +00:00
|
|
|
return 0;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
$count = (int) DB::select(array(DB::expr('COUNT(*)'), 'records_found'))
|
2011-01-13 14:49:56 +00:00
|
|
|
->from($this->_has_many[$alias]['through'])
|
|
|
|
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
|
2011-05-16 12:47:16 +00:00
|
|
|
->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
|
|
|
|
->execute($this->_db)->get('records_found');
|
|
|
|
|
|
|
|
// Rows found need to match the rows searched
|
2012-11-22 03:25:06 +00:00
|
|
|
return (int) $count;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new relationship to between this model and another.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* // Add the login role using a model instance
|
|
|
|
* $model->add('roles', ORM::factory('role', array('name' => 'login')));
|
|
|
|
* // Add the login role if you know the roles.id is 5
|
|
|
|
* $model->add('roles', 5);
|
|
|
|
* // Add multiple roles (for example, from checkboxes on a form)
|
|
|
|
* $model->add('roles', array(1, 2, 3, 4));
|
|
|
|
*
|
|
|
|
* @param string $alias Alias of the has_many "through" relationship
|
|
|
|
* @param mixed $far_keys Related model, primary key, or an array of primary keys
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function add($alias, $far_keys)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
$columns = array($this->_has_many[$alias]['foreign_key'], $this->_has_many[$alias]['far_key']);
|
2011-05-16 12:47:16 +00:00
|
|
|
$foreign_key = $this->pk();
|
|
|
|
|
|
|
|
$query = DB::insert($this->_has_many[$alias]['through'], $columns);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
foreach ( (array) $far_keys as $key)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$query->values(array($foreign_key, $key));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$query->execute($this->_db);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a relationship between this model and another.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* // Remove a role using a model instance
|
|
|
|
* $model->remove('roles', ORM::factory('role', array('name' => 'login')));
|
|
|
|
* // Remove the role knowing the primary key
|
|
|
|
* $model->remove('roles', 5);
|
|
|
|
* // Remove multiple roles (for example, from checkboxes on a form)
|
|
|
|
* $model->remove('roles', array(1, 2, 3, 4));
|
|
|
|
* // Remove all related roles
|
|
|
|
* $model->remove('roles');
|
|
|
|
*
|
|
|
|
* @param string $alias Alias of the has_many "through" relationship
|
|
|
|
* @param mixed $far_keys Related model, primary key, or an array of primary keys
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
2011-05-16 12:47:16 +00:00
|
|
|
public function remove($alias, $far_keys = NULL)
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
|
|
|
|
|
|
|
|
$query = DB::delete($this->_has_many[$alias]['through'])
|
|
|
|
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk());
|
|
|
|
|
|
|
|
if ($far_keys !== NULL)
|
|
|
|
{
|
|
|
|
// Remove all the relationships in the array
|
|
|
|
$query->where($this->_has_many[$alias]['far_key'], 'IN', (array) $far_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute($this->_db);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of records in the table.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return integer
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function count_all()
|
|
|
|
{
|
|
|
|
$selects = array();
|
|
|
|
|
|
|
|
foreach ($this->_db_pending as $key => $method)
|
|
|
|
{
|
|
|
|
if ($method['name'] == 'select')
|
|
|
|
{
|
|
|
|
// Ignore any selected columns for now
|
|
|
|
$selects[] = $method;
|
|
|
|
unset($this->_db_pending[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if ( ! empty($this->_load_with))
|
|
|
|
{
|
|
|
|
foreach ($this->_load_with as $alias)
|
|
|
|
{
|
|
|
|
// Bind relationship
|
|
|
|
$this->with($alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->_build(Database::SELECT);
|
|
|
|
|
2012-11-22 03:25:06 +00:00
|
|
|
$records = $this->_db_builder->from(array($this->_table_name, $this->_object_name))
|
|
|
|
->select(array(DB::expr('COUNT(*)'), 'records_found'))
|
2011-01-13 14:49:56 +00:00
|
|
|
->execute($this->_db)
|
|
|
|
->get('records_found');
|
|
|
|
|
|
|
|
// Add back in selected columns
|
|
|
|
$this->_db_pending += $selects;
|
|
|
|
|
|
|
|
$this->reset();
|
|
|
|
|
|
|
|
// Return the total number of records in a table
|
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy method to Database list_columns.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return array
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function list_columns()
|
|
|
|
{
|
|
|
|
// Proxy to database
|
|
|
|
return $this->_db->list_columns($this->_table_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an ORM model for the given one-one related alias
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param string $alias Alias name
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
protected function _related($alias)
|
|
|
|
{
|
|
|
|
if (isset($this->_related[$alias]))
|
|
|
|
{
|
|
|
|
return $this->_related[$alias];
|
|
|
|
}
|
|
|
|
elseif (isset($this->_has_one[$alias]))
|
|
|
|
{
|
|
|
|
return $this->_related[$alias] = ORM::factory($this->_has_one[$alias]['model']);
|
|
|
|
}
|
|
|
|
elseif (isset($this->_belongs_to[$alias]))
|
|
|
|
{
|
|
|
|
return $this->_related[$alias] = ORM::factory($this->_belongs_to[$alias]['model']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the primary key
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return mixed Primary key
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function pk()
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
return $this->_primary_key_value;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last executed query
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @return string
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function last_query()
|
|
|
|
{
|
|
|
|
return $this->_db->last_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears query builder. Passing FALSE is useful to keep the existing
|
|
|
|
* query conditions for another query.
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @param bool $next Pass FALSE to avoid resetting on the next call
|
|
|
|
* @return ORM
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function reset($next = TRUE)
|
|
|
|
{
|
|
|
|
if ($next AND $this->_db_reset)
|
|
|
|
{
|
|
|
|
$this->_db_pending = array();
|
|
|
|
$this->_db_applied = array();
|
|
|
|
$this->_db_builder = NULL;
|
|
|
|
$this->_with_applied = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset on the next call?
|
|
|
|
$this->_db_reset = $next;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
|
|
|
|
protected function _serialize_value($value)
|
|
|
|
{
|
|
|
|
return json_encode($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _unserialize_value($value)
|
|
|
|
{
|
|
|
|
return json_decode($value, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function object_name()
|
|
|
|
{
|
|
|
|
return $this->_object_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function object_plural()
|
|
|
|
{
|
|
|
|
return $this->_object_plural;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loaded()
|
|
|
|
{
|
|
|
|
return $this->_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saved()
|
|
|
|
{
|
|
|
|
return $this->_saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function primary_key()
|
|
|
|
{
|
|
|
|
return $this->_primary_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function table_name()
|
|
|
|
{
|
|
|
|
return $this->_table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function table_columns()
|
|
|
|
{
|
|
|
|
return $this->_table_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has_one()
|
|
|
|
{
|
|
|
|
return $this->_has_one;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function belongs_to()
|
|
|
|
{
|
|
|
|
return $this->_belongs_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has_many()
|
|
|
|
{
|
|
|
|
return $this->_has_many;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function load_with()
|
|
|
|
{
|
|
|
|
return $this->_load_with;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function original_values()
|
|
|
|
{
|
|
|
|
return $this->_original_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function created_column()
|
|
|
|
{
|
|
|
|
return $this->_created_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updated_column()
|
|
|
|
{
|
|
|
|
return $this->_updated_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validation()
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_validation))
|
|
|
|
{
|
|
|
|
// Initialize the validation object
|
|
|
|
$this->_validation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_validation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function object()
|
|
|
|
{
|
|
|
|
return $this->_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function errors_filename()
|
|
|
|
{
|
|
|
|
return $this->_errors_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of and_where()
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function where($column, $op, $value)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'where',
|
|
|
|
'args' => array($column, $op, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new "AND WHERE" condition for the query.
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_where($column, $op, $value)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_where',
|
|
|
|
'args' => array($column, $op, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new "OR WHERE" condition for the query.
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_where($column, $op, $value)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_where',
|
|
|
|
'args' => array($column, $op, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of and_where_open()
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function where_open()
|
|
|
|
{
|
|
|
|
return $this->and_where_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a new "AND WHERE (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_where_open()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_where_open',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a new "OR WHERE (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_where_open()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_where_open',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "AND WHERE (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function where_close()
|
|
|
|
{
|
|
|
|
return $this->and_where_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "AND WHERE (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_where_close()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_where_close',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "OR WHERE (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_where_close()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_where_close',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies sorting with "ORDER BY ..."
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $direction direction of sorting
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function order_by($column, $direction = NULL)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'order_by',
|
|
|
|
'args' => array($column, $direction),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return up to "LIMIT ..." results
|
|
|
|
*
|
|
|
|
* @param integer $number maximum results to return
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function limit($number)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'limit',
|
|
|
|
'args' => array($number),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables selecting only unique columns using "SELECT DISTINCT"
|
|
|
|
*
|
|
|
|
* @param boolean $value enable or disable distinct columns
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function distinct($value)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'distinct',
|
|
|
|
'args' => array($value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Choose the columns to select from.
|
|
|
|
*
|
|
|
|
* @param mixed $columns column name or array($column, $alias) or object
|
|
|
|
* @param ...
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function select($columns = NULL)
|
|
|
|
{
|
|
|
|
$columns = func_get_args();
|
|
|
|
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'select',
|
|
|
|
'args' => $columns,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Choose the tables to select "FROM ..."
|
|
|
|
*
|
|
|
|
* @param mixed $tables table name or array($table, $alias) or object
|
|
|
|
* @param ...
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function from($tables)
|
|
|
|
{
|
|
|
|
$tables = func_get_args();
|
|
|
|
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'from',
|
|
|
|
'args' => $tables,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds addition tables to "JOIN ...".
|
|
|
|
*
|
|
|
|
* @param mixed $table column name or array($column, $alias) or object
|
|
|
|
* @param string $type join type (LEFT, RIGHT, INNER, etc)
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function join($table, $type = NULL)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'join',
|
|
|
|
'args' => array($table, $type),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds "ON ..." conditions for the last created JOIN statement.
|
|
|
|
*
|
|
|
|
* @param mixed $c1 column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $c2 column name or array($column, $alias) or object
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function on($c1, $op, $c2)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'on',
|
|
|
|
'args' => array($c1, $op, $c2),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a "GROUP BY ..." filter.
|
|
|
|
*
|
|
|
|
* @param mixed $columns column name or array($column, $alias) or object
|
|
|
|
* @param ...
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function group_by($columns)
|
|
|
|
{
|
|
|
|
$columns = func_get_args();
|
|
|
|
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'group_by',
|
|
|
|
'args' => $columns,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of and_having()
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function having($column, $op, $value = NULL)
|
|
|
|
{
|
|
|
|
return $this->and_having($column, $op, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new "AND HAVING" condition for the query.
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_having($column, $op, $value = NULL)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_having',
|
|
|
|
'args' => array($column, $op, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new "OR HAVING" condition for the query.
|
|
|
|
*
|
|
|
|
* @param mixed $column column name or array($column, $alias) or object
|
|
|
|
* @param string $op logic operator
|
|
|
|
* @param mixed $value column value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_having($column, $op, $value = NULL)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_having',
|
|
|
|
'args' => array($column, $op, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of and_having_open()
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function having_open()
|
|
|
|
{
|
|
|
|
return $this->and_having_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a new "AND HAVING (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_having_open()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_having_open',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a new "OR HAVING (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_having_open()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_having_open',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "AND HAVING (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function having_close()
|
|
|
|
{
|
|
|
|
return $this->and_having_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "AND HAVING (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function and_having_close()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'and_having_close',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes an open "OR HAVING (...)" grouping.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function or_having_close()
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'or_having_close',
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start returning results after "OFFSET ..."
|
|
|
|
*
|
|
|
|
* @param integer $number starting result number
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function offset($number)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'offset',
|
|
|
|
'args' => array($number),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables the query to be cached for a specified amount of time.
|
|
|
|
*
|
|
|
|
* @param integer $lifetime number of seconds to cache
|
|
|
|
* @return $this
|
|
|
|
* @uses Kohana::$cache_life
|
|
|
|
*/
|
|
|
|
public function cached($lifetime = NULL)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'cached',
|
|
|
|
'args' => array($lifetime),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a parameter in the query.
|
|
|
|
*
|
|
|
|
* @param string $param parameter key to replace
|
|
|
|
* @param mixed $value value to use
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function param($param, $value)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'param',
|
|
|
|
'args' => array($param, $value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds "USING ..." conditions for the last created JOIN statement.
|
|
|
|
*
|
|
|
|
* @param string $columns column name
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function using($columns)
|
|
|
|
{
|
|
|
|
// Add pending database call which is executed after query type is determined
|
|
|
|
$this->_db_pending[] = array(
|
|
|
|
'name' => 'using',
|
|
|
|
'args' => array($columns),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a column value is unique.
|
|
|
|
* Excludes itself if loaded.
|
|
|
|
*
|
|
|
|
* @param string $field the field to check for uniqueness
|
|
|
|
* @param mixed $value the value to check for uniqueness
|
|
|
|
* @return bool whteher the value is unique
|
|
|
|
*/
|
|
|
|
public function unique($field, $value)
|
|
|
|
{
|
|
|
|
$model = ORM::factory($this->object_name())
|
|
|
|
->where($field, '=', $value)
|
|
|
|
->find();
|
|
|
|
|
|
|
|
if ($this->loaded())
|
|
|
|
{
|
|
|
|
return ( ! ($model->loaded() AND $model->pk() != $this->pk()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( ! $model->loaded());
|
|
|
|
}
|
2011-01-13 14:49:56 +00:00
|
|
|
} // End ORM
|