Modules

Model_Menu
extends ORM_MPTT
extends Gleez_ORM_MPTT
extends ORM
extends Gleez_ORM_Core
extends Model
extends Kohana_Model

Implements: Serializable

Menu Model Class

package
Gleez\ORM
author
Sandeep Sangamreddi - Gleez
copyright
© 2011-2013 Gleez Technologies
license
http://gleezcms.org/license

Class declared in MODPATH/gleez/classes/model/menu.php on line 10.

Constants

Methods

Constants

DELETE

integer 5

Properties

public string $left_column

left column name

public string $level_column

level column name

public $parent_column

public string $right_column

right column name

public $scope_column

protected array $_belongs_to

"Belongs to" relationships

protected bool $_cache

protected array $_cast_data

Data to be loaded into the model from a database call cast

protected array $_changed

protected static array $_column_cache

Stores column information for ORM models

array(0) 

protected string $_created_column

Auto-update columns for creation

protected object $_datatables

DataTables

protected Database $_db

Database Object

protected array $_db_applied

Database methods applied

protected Database_Query_Builder_Where $_db_builder

Database query builder

protected String $_db_group

Database config group

protected array $_db_pending

Database methods pending

protected bool $_db_reset

Reset builder

protected string $_errors_filename

The message filename used for validation errors. Defaults to ORM::$_object_name

protected string $_foreign_key_suffix

Foreign key suffix

protected array $_has_many

"Has many" relationships

protected array $_has_many_keys

"Many to many" relationships

protected array $_has_one

"Has one" relationships

protected array $_ignored_columns

Ignored columns

protected static array $_init_cache

Initialization storage for ORM models

array(0) 

protected array $_load_with

Relationships that should always be joined

protected bool $_loaded

protected array $_object

Current object

protected string $_object_name

Model name

protected string $_object_plural

Plural model name

protected array $_original_values

protected string $_primary_key

Table primary key

protected mixed $_primary_key_value

Primary key value

protected bool $_reload_on_wakeup

Model configuration, reload on wakeup?

protected bool $_saved

protected array $_serialize_columns

Auto-serialize and unserialize columns on get/set

protected array $_sorting

protected $_table_columns

protected string $_table_name

Table name

protected bool $_table_names_plural

Model configuration, table names plural?

protected bool $_updated

protected string $_updated_column

Auto-update columns for updates

protected bool $_valid

protected Validation $_validation

Validation object created before saving/updating

protected array $_with_applied

With calls already applied

Methods

public create_at( ORM_MPTT|integer $parent [, string|integer $location = string(4) "last" ] ) (defined in Model_Menu)

Create a new term in the tree as a child of $parent

  • if $location is "first" or "last" the term will be the first or last child
  • if $location is an int, the term will be the next sibling of term with id $location

Parameters

  • ORM_MPTT|integer $parent required - The parent
  • string|integer $location = string(4) "last" - The location [Optional]

Tags

Return Values

  • Model_Menu

Source Code

public function create_at($parent, $location = 'last')
{
	// Create the term as first child, last child, or as next sibling based on location
	if ($location == 'first')
	{
		$this->insert_as_first_child($parent);
	}
	else if ($location == 'last')
	{
		$this->insert_as_last_child($parent);
	}
	else
	{
		$target = ORM::factory('menu',(int) $location);
		
		if ( ! $target->loaded())
		{
			throw new Gleez_Exception("Could not create menu, could not find target for
						  insert_as_next_sibling id: " . (int) $location);
		}

		$this->insert_as_last_child($target);
	}

	return $this;
}

public labels( ) (defined in Model_Menu)

Labels for fields in this model

Return Values

  • array - Array of labels

Source Code

public function labels()
{
	return array(
		'title'  => __('Title'),
		'name'   => __('Slug'),
		'url'    => __('Link'),
	);
}

public move_to( $target $target [, $action $action = string(5) "after" ] ) (defined in Model_Menu)

Move the item to $target based on action

Parameters

  • $target $target required - Integer The target term id
  • $action $action = string(5) "after" - String The action to perform (before/after/first/last) after

Tags

Source Code

public function move_to($target, $action = 'after')
{
	// Find the target
	$target = ORM::factory('menu',(int) $target);

	// Make sure it exists
	if ( ! $target->loaded())
	{
		throw new Gleez_Exception("Could not move item, target item did not exist." . (int) $target->id);
	}

	if ($action == 'before')
		$this->move_to_prev_sibling($target);
	elseif ($action == 'after')
		$this->move_to_next_sibling($target);
	elseif ($action == 'first')
		$this->move_to_first_child($target);
	elseif ($action == 'last')
		$this->move_to_last_child($target);
	else
		throw new Gleez_Exception("Could not move item, action should be 'before', 'after', 'first' or 'last'.");
}

public rules( ) (defined in Model_Menu)

Rule definitions for validation

Return Values

  • array - Array of rules

Source Code

public function rules()
{
	return array(
		'name' => array(
			array('not_empty'),
		),
	);
}

public save( [ Validation $validation = NULL ] ) (defined in Model_Menu)

Updates or Creates the record depending on loaded()

Parameters

  • Validation $validation = NULL - Validation object

Return Values

  • ORM

Source Code

public function save(Validation $validation = NULL)
{
	$this->name   = $this->_unique_slug(URL::title(empty($this->name) ? $this->title : $this->name));
	$this->params = empty($this->params) ? NULL : serialize($this->params);

	return parent::save( $validation );
}

public __construct( [ mixed $id = NULL ] ) (defined in Gleez_ORM_MPTT)

Load the default column names.

Parameters

  • mixed $id = NULL - Parameter for find or object to load

Return Values

  • void

Source Code

public function __construct($id = NULL)
{
	if (empty($this->_sorting))
	{
		$this->_sorting = array($this->scope_column => 'ASC', $this->left_column => 'ASC');
	}
	else
	{
		$this->_sorting = Arr::unshift($this->_sorting, $this->left_column, 'ASC');
		$this->_sorting = Arr::unshift($this->_sorting, $this->scope_column, 'ASC');
	}
	
	parent::__construct($id);
}

public __get( string $column ) (defined in Gleez_ORM_MPTT)

Magic get function, maps field names to class functions.

Parameters

  • string $column required - Name of the field to get

Return Values

  • mixed

Source Code

public function __get($column)
{
	switch ($column)
	{
		case 'parent':
			return $this->parent();
		case 'parents':
			return $this->parents();
		case 'children':
			return $this->children();
		case 'first_child':
			return $this->children(FALSE, 'ASC', 1);
		case 'last_child':
			return $this->children(FALSE, 'DESC', 1);
		case 'siblings':
			return $this->siblings();
		case 'root':
			return $this->root();
		case 'roots':
			return $this->roots();
		case 'leaves':
			return $this->leaves();
		case 'descendants':
			return $this->descendants();
		case 'fulltree':
			return $this->fulltree();
		default:
			return parent::__get($column);
	}
}

public children( [ bool $self = bool FALSE , string $direction = string(3) "ASC" , int $limit = bool FALSE ] ) (defined in Gleez_ORM_MPTT)

Returns direct children of the current node.

Parameters

  • bool $self = bool FALSE - Include the current node
  • string $direction = string(3) "ASC" - Direction to order the left column by
  • int $limit = bool FALSE - Number of children to get

Return Values

  • ORM_MPTT

Source Code

public function children($self = FALSE, $direction = 'ASC', $limit = FALSE)
{
	return $this->descendants($self, $direction, TRUE, FALSE, $limit);
}

public count( ) (defined in Gleez_ORM_MPTT)

Returns the number of descendants the current node has.

Return Values

  • int

Source Code

public function count()
{
	return ($this->size() - 2) / 2;
}

public delete( ) (defined in Gleez_ORM_MPTT)

Deletes the current node and all descendants.

Return Values

  • void

Source Code

public function delete($query = NULL)
{
	if ($query !== NULL)
	{
		throw new Kohana_Exception('ORM_MPTT does not support passing a query object to delete()');
	}
	
	//$this->lock();
	// Start the transaction
	$this->_db->begin();

	try
	{
		DB::delete($this->_table_name)
			->where($this->left_column,' >=',$this->left())
			->where($this->right_column,' <= ',$this->right())
			->where($this->scope_column,' = ',$this->scope())
			->execute($this->_db);

		$this->delete_space($this->left(), $this->size());
	}
	catch (Kohana_Exception $e)
	{
		//$this->unlock();
		$this->_db->rollback();
		throw $e;
	}

	//$this->unlock();
	// Commit the transaction
	$this->_db->commit();
}

public descendants( [ bool $self = bool FALSE , string $direction = string(3) "ASC" , bool $direct_children_only = bool FALSE , bool $leaves_only = bool FALSE , int $limit = bool FALSE ] ) (defined in Gleez_ORM_MPTT)

Returns the descendants of the current node.

Parameters

  • bool $self = bool FALSE - Include the current node
  • string $direction = string(3) "ASC" - Direction to order the left column by.
  • bool $direct_children_only = bool FALSE - Include direct children only
  • bool $leaves_only = bool FALSE - Include leaves only
  • int $limit = bool FALSE - Number of results to get

Return Values

  • ORM_MPTT

Source Code

public function descendants($self = FALSE, $direction = 'ASC', $direct_children_only = FALSE, $leaves_only = FALSE, $limit = FALSE)
{
	$left_operator = $self ? '>=' : '>';
	$right_operator = $self ? '<=' : '<';
	
	$query = self::factory($this->object_name())
		->where($this->left_column, $left_operator, $this->left())
		->where($this->right_column, $right_operator, $this->right())
		->where($this->scope_column, '=', $this->scope())
		->order_by($this->left_column, $direction);
	
	if ($direct_children_only)
	{
		if ($self)
		{
			$query
				->and_where_open()
				->where($this->level_column, '=', $this->level())
				->or_where($this->level_column, '=', $this->level() + 1)
				->and_where_close();
		}
		else
		{
			$query->where($this->level_column, '=', $this->level() + 1);
		}
	}
	
	if ($leaves_only)
	{
		$query->where($this->right_column, '=', DB::expr($this->left_column.' + 1'));
	}
	
	if ($limit !== FALSE)
	{
		$query->limit($limit);
	}
	
	return $query->find_all();
}

public fulltree( [ bool $scope = NULL ] ) (defined in Gleez_ORM_MPTT)

Returns a full hierarchical tree, with or without scope checking.

Parameters

  • bool $scope = NULL - Only retrieve nodes with specified scope

Return Values

  • object

Source Code

public function fulltree($scope = NULL)
{
	$result = self::factory($this->object_name());

	if ( ! is_null($scope))
	{
		$result->where($this->scope_column, '=', $scope);
	}
	else
	{
		$result->order_by($this->scope_column, 'ASC')
				->order_by($this->left_column, 'ASC');
	}

	return $result->find_all();
}

public get_levels( [ int $scope = NULL ] ) (defined in Gleez_ORM_MPTT)

Get all posible level values

Parameters

  • int $scope = NULL - Restrict to the given scope

Return Values

  • Database_Result

Source Code

public function get_levels($scope = NULL)
{
	$result = DB::select($this->level_column)
		->distinct(TRUE)
		->from($this->_table_name);
	
	if ( ! empty($scope))
	{
		$result->where($this->scope_column, '=', (int) $scope);
	}
		
	return $result->execute($this->_db);
}

public has_children( ) (defined in Gleez_ORM_MPTT)

Checks if the current node has any children.

Return Values

  • bool

Source Code

public function has_children()
{
	return ($this->size() > 2);
}

public insert_as_first_child( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Inserts a new node as the first child of the target node.

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node

Return Values

  • ORM_MPTT

Source Code

public function insert_as_first_child($target)
{
	$target = $this->parent_from($target);
	return $this->insert($target, $this->left_column, 1, 1);
}

public insert_as_last_child( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Inserts a new node as the last child of the target node.

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node

Return Values

  • ORM_MPTT

Source Code

public function insert_as_last_child($target)
{
	$target = $this->parent_from($target, $this->primary_key());
	return $this->insert($target, $this->right_column, 0, 1);
}

public insert_as_next_sibling( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Inserts a new node as the next sibling of the target node.

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node

Return Values

  • ORM_MPTT

Source Code

public function insert_as_next_sibling($target)
{
	$target = $this->parent_from($target, $this->parent_column);
	return $this->insert($target, $this->right_column, 1, 0);
}

public insert_as_prev_sibling( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Inserts a new node as a previous sibling of the target node.

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node

Return Values

  • ORM_MPTT

Source Code

public function insert_as_prev_sibling($target)
{
	$target = $this->parent_from($target, $this->parent_column);
	return $this->insert($target, $this->left_column, 0, 0);
}

public is_child( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Checks if the current node is a direct child of the supplied node.

Parameters

  • ORM_MPTT|int $target required - ORM_MPTT object or primary key value of target node

Return Values

  • bool

Source Code

public function is_child($target)
{
	if ( ! ($target instanceof $this))
	{
		$target = self::factory($this->object_name(), $target);
	}

	return ((int) $this->{$this->parent_column} === (int) $target->pk());
}

public is_descendant( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Is the current node a descendant of the supplied node.

Parameters

  • ORM_MPTT|int $target required - ORM_MPTT object or primary key value of target node

Return Values

  • bool

Source Code

public function is_descendant($target)
{
	if ( ! ($target instanceof $this))
	{
		$target = self::factory($this->object_name(), $target);
	}
	
	return (
			$this->{$this->left_column} > $target->{$target->left_column}
			AND $this->{$this->right_column} < $target->{$target->right_column}
			AND $this->{$this->scope_column} = $target->{$target->scope_column}
		);
}

public is_in_parents( int|object $target ) (defined in Gleez_ORM_MPTT)

Checks if the current node is one of the parents of a specific node.

Parameters

  • int|object $target required - Id or object of parent node

Return Values

  • bool

Source Code

public function is_in_parents($target)
{
	if ( ! ($target instanceof $this))
	{
		$target = self::factory($this->object_name(), $target);
	}

	return $target->is_descendant($this);
}

public is_leaf( ) (defined in Gleez_ORM_MPTT)

Is the current node a leaf node?

Return Values

  • bool

Source Code

public function is_leaf()
{
	return ( ! $this->has_children());
}

public is_parent( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Checks if the current node is a direct parent of a specific node.

Parameters

  • ORM_MPTT|int $target required - ORM_MPTT object or primary key value of child node

Return Values

  • bool

Source Code

public function is_parent($target)
{
	if ( ! ($target instanceof $this))
	{
		$target = self::factory($this->object_name(), $target);
	}

	return ((int) $this->pk() === (int) $target->{$this->parent_column});
}

public is_root( ) (defined in Gleez_ORM_MPTT)

Checks if the current node is a root node.

Return Values

  • bool

Source Code

public function is_root()
{
	return ($this->left() === 1);
}

public is_sibling( ORM_MPTT|int $target ) (defined in Gleez_ORM_MPTT)

Checks if the current node is a sibling of a supplied node. (Both have the same direct parent)

Parameters

  • ORM_MPTT|int $target required - ORM_MPTT object or primary key value of target node

Return Values

  • bool

Source Code

public function is_sibling($target)
{
	if ( ! ($target instanceof $this))
	{
		$target = self::factory($this->object_name(), $target);
	}
	
	if ((int) $this->pk() === (int) $target->pk())
		return FALSE;

	return ((int) $this->{$this->parent_column} === (int) $target->{$target->parent_column});
}

public leaves( [ bool $self = bool FALSE , string $direction = string(3) "ASC" ] ) (defined in Gleez_ORM_MPTT)

Returns the leaves of the current node.

Parameters

  • bool $self = bool FALSE - Include the current node
  • string $direction = string(3) "ASC" - Direction to order the left column by

Return Values

  • ORM_MPTT

Source Code

public function leaves($self = FALSE, $direction = 'ASC')
{
	return $this->descendants($self, $direction, TRUE, TRUE);
}

public left( ) (defined in Gleez_ORM_MPTT)

Returns the value of the current nodes left column.

Return Values

  • int

Source Code

public function left()

return (INT) $this->{$this->left_column};

public level( ) (defined in Gleez_ORM_MPTT)

Returns the value of the current nodes level column.

Return Values

  • int

Source Code

public function level()
{
	return (INT) $this->{$this->level_column};
}

public make_root( [ int $validation = NULL , $scope = NULL ] ) (defined in Gleez_ORM_MPTT)

Creates a new node as root, or moves a node to root

Parameters

  • int $validation = NULL - The new scope
  • unknown $scope = NULL

Tags

Return Values

  • ORM_MPTT

Source Code

public function make_root(Validation $validation = NULL, $scope = NULL)
{
	// If node already exists, and already root, exit
	if ($this->loaded() AND $this->is_root())
		return $this;

	// delete node space first
	if ($this->loaded())
	{
		$this->delete_space($this->left(), $this->size());
	}

	if (is_null($scope))
	{
		// Increment next scope
		$scope = self::get_next_scope();
	}
	elseif ( ! $this->scope_available($scope))
	{
		return FALSE;
	}

	$this->{$this->scope_column} = $scope;
	$this->{$this->level_column} = 1;
	$this->{$this->left_column} = 1;
	$this->{$this->right_column} = 2;
	$this->{$this->parent_column} = NULL;

	try
	{
		parent::save($validation);
	}
	catch (ORM_Validation_Exception $e)
	{
		// Some fields didn't validate, throw an exception
		throw $e;
	}

	return $this;
}

public move_to_first_child( ) (defined in Gleez_ORM_MPTT)

Source Code

public function move_to_first_child($target)
{
	$target = $this->parent_from($target, $this->primary_key());
	return $this->move($target, TRUE, 1, 1, TRUE);
}

public move_to_last_child( ) (defined in Gleez_ORM_MPTT)

Source Code

public function move_to_last_child($target)
{
	$target = $this->parent_from($target, $this->primary_key());
	return $this->move($target, FALSE, 0, 1, TRUE);
}

public move_to_next_sibling( ) (defined in Gleez_ORM_MPTT)

Source Code

public function move_to_next_sibling($target)
{
	$target = $this->parent_from($target, $this->parent_column);
	return $this->move($target, FALSE, 1, 0, FALSE);
}

public move_to_prev_sibling( ) (defined in Gleez_ORM_MPTT)

Source Code

public function move_to_prev_sibling($target)
{
	$target = $this->parent_from($target, $this->parent_column);
	return $this->move($target, TRUE, 0, 0, FALSE);
}

public parent( ) (defined in Gleez_ORM_MPTT)

Returns the parent node of the current node

Return Values

  • ORM_MPTT

Source Code

public function parent()
{
	if ($this->is_root())
		return NULL;

	return self::factory($this->object_name(), $this->{$this->parent_column});
}

public parents( [ bool $root = bool TRUE , bool $with_self = bool FALSE , string $direction = string(3) "ASC" , bool $direct_parent_only = bool FALSE ] ) (defined in Gleez_ORM_MPTT)

Returns all of the current nodes parents.

Parameters

  • bool $root = bool TRUE - Include root node
  • bool $with_self = bool FALSE - Include current node
  • string $direction = string(3) "ASC" - Direction to order the left column by
  • bool $direct_parent_only = bool FALSE - Retrieve the direct parent only

Return Values

  • ORM_MPTT

Source Code

public function parents($root = TRUE, $with_self = FALSE, $direction = 'ASC', $direct_parent_only = FALSE)
{
	$suffix = $with_self ? '=' : '';

	$query = self::factory($this->object_name())
		->where($this->left_column, '<'.$suffix, $this->left())
		->where($this->right_column, '>'.$suffix, $this->right())
		->where($this->scope_column, '=', $this->scope())
		->order_by($this->left_column, $direction);
	
	if ( ! $root)
	{
		$query->where($this->left_column, '!=', 1);
	}
	
	if ($direct_parent_only)
	{
		$query
			->where($this->level_column, '=', $this->level() - 1)
			->limit(1);
	}
	
	return $query->find_all();
}

public rebuild_tree( [ int $left = integer 1 , ORM_MPTT $target = NULL ] ) (defined in Gleez_ORM_MPTT)

Rebuilds the tree using the parent_column. Order of the tree is not guaranteed to be consistent with structure prior to reconstruction. This method will reduce the tree structure to eliminating any holes. If you have a child node that is outside of the left/right constraints it will not be moved under the root.

Parameters

  • int $left = integer 1 - Left Starting value for left branch
  • ORM_MPTT $target = NULL - Target Target node to use as root

Return Values

  • int

Source Code

public function rebuild_tree($left = 1, $target = NULL)
{
	// check if using target or self as root and load if not loaded
	if (is_null($target) AND ! $this->loaded())
	{
		return FALSE;
	}
	elseif (is_null($target))
	{
		$target = $this;
	}

	if ( ! $target->loaded())
	{
		$target->_load();
	}

	// Use the current node left value for entire tree
	if (is_null($left))
	{
		$left = $target->{$target->left_column};
	}

	//$target->lock();
	// Start the transaction
	$this->_db->begin();
	$right = $left + 1;
	$children = $target->children();

	foreach ($children as $child)
	{
		$right = $child->rebuild_tree($right);
	}

	$target->{$target->left_column} = $left;
	$target->{$target->right_column} = $right;
	$target->save();
	//$target->unlock();
	// Commit the transaction
	$this->_db->commit();

	return $right + 1;
}

Returns the value of the current nodes right column.

Return Values

  • int

Source Code

public function right()
{
	return (INT) $this->{$this->right_column};
}

public root( [ int $scope = NULL ] ) (defined in Gleez_ORM_MPTT)

Returns the root node of the current object instance.

Parameters

  • int $scope = NULL - Scope

Return Values

  • ORM_MPTT|FALSE

Source Code

public function root($scope = NULL)
{
	if (is_null($scope) AND $this->loaded())
	{
		$scope = $this->scope();
	}
	elseif (is_null($scope) AND ! $this->loaded())
	{
		throw new Kohana_Exception(':method must be called on an ORM_MPTT object instance.', array(':method' => 'root'));
	}
	
	return self::factory($this->object_name(), array($this->left_column => 1, $this->scope_column => $scope));
}

public roots( ) (defined in Gleez_ORM_MPTT)

Returns all root node's

Return Values

  • ORM_MPTT

Source Code

public function roots()
{
	return self::factory($this->object_name())
			->where($this->left_column, '=', 1)
			->find_all();
}

public scope( ) (defined in Gleez_ORM_MPTT)

Returns the value of the current nodes scope column.

Return Values

  • int

Source Code

public function scope()
{
	return (INT) $this->{$this->scope_column};
}

public select_list( [ string $key = NULL , string $value = NULL , string $indent = NULL ] ) (defined in Gleez_ORM_MPTT)

Overloads the select_list method to support indenting.

Parameters

  • string $key = NULL - First table column.
  • string $value = NULL - $val second table column.
  • string $indent = NULL - Character used for indenting.

Return Values

  • array

Source Code

public function select_list($key = NULL, $value = NULL, $indent = NULL)
{
	if ($key === NULL)
	{
		// Use the default key
		$key = $this->_primary_key;
	}

	if ($value === NULL)
	{
		// Use the default value
		$value = $this->pk();
	}

	$result = DB::select($key, $value, $this->level_column)
			->from($this->_table_name)
			->where($this->scope_column, '=', $this->{$this->scope_column})
			->where($this->_primary_key, '<>', $this->pk())
			->where($this->level_column, '<>', 1)
			->order_by($this->left_column, 'ASC')
			->execute($this->_db);
	
	if (is_string($indent))
	{
		$array = array('last' => '');
		foreach ($result as $row)
		{
			//$array[$row->$key] = str_repeat($indent, ($row->{$this->_level_column} - 1)).$row->$value;
			$array[$row[$key]] = str_repeat($indent, $row[$this->level_column] - 2).$row[$value];
		}

		return $array;
	}

	return $result->as_array($key, $value);
}

public siblings( [ bool $self = bool FALSE , string $direction = string(3) "ASC" ] ) (defined in Gleez_ORM_MPTT)

Returns the siblings of the current node

Parameters

  • bool $self = bool FALSE - Include the current node
  • string $direction = string(3) "ASC" - Direction to order the left column by

Return Values

  • ORM_MPTT

Source Code

public function siblings($self = FALSE, $direction = 'ASC')
{
	$query = self::factory($this->object_name())
		->where($this->left_column, '>', $this->parent->left())
		->where($this->right_column, '<', $this->parent->right())
		->where($this->scope_column, '=', $this->scope())
		->where($this->level_column, '=', $this->level())
		->order_by($this->left_column, $direction);
	 
	if ( ! $self)
	{
		$query->where($this->primary_key(), '<>', $this->pk());
	}
	 
	return $query->find_all();
}

public size( ) (defined in Gleez_ORM_MPTT)

Returns the size of the current node.

Return Values

  • int

Source Code

public function size()
{
	return $this->right() - $this->left() + 1;
}

public __isset( string $column ) (defined in Gleez_ORM_Core)

Checks if object data is set.

Parameters

  • string $column required - Column name

Return Values

  • boolean

Source Code

public function __isset($column)
{
	return (isset($this->_object[$column]) OR
		isset($this->_related[$column]) OR
		isset($this->_has_one[$column]) OR
		isset($this->_belongs_to[$column]) OR
		isset($this->_has_many[$column]));
}

public __set( string $column , mixed $value ) (defined in Gleez_ORM_Core)

Base set method - this should not be overridden.

Parameters

  • string $column required - Column name
  • mixed $value required - Column value

Return Values

  • void

Source Code

public function __set($column, $value)
{
	if ( ! isset($this->_object_name))
	{
		// Object not yet constructed, so we're loading data from a database call cast
		$this->_cast_data[$column] = $value;
	}
	else
	{
		// Set the model's column to given value
		$this->set($column, $value);
	}
}

public __toString( ) (defined in Gleez_ORM_Core)

Displays the primary key of a model when it is converted to a string.

Return Values

  • string

Source Code

public function __toString()
{
	return (string) $this->pk();
}

public __unset( string $column ) (defined in Gleez_ORM_Core)

Unsets object data.

Parameters

  • string $column required - Column name

Return Values

  • void

Source Code

public function __unset($column)
{
	unset($this->_object[$column], $this->_changed[$column], $this->_related[$column]);
}

public add( string $alias , mixed $far_keys [, array $data = NULL ] ) (defined in Gleez_ORM_Core)

Adds a new relationship to between this model and another.

// 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));

Parameters

  • string $alias required - Alias of the has_many "through" relationship
  • mixed $far_keys required - Related model, primary key, or an array of primary keys
  • array $data = NULL - Additional data to store in "through"/pivot table

Return Values

  • ORM

Source Code

public function add($alias, $far_keys, $data = NULL)
{
	$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;

	$columns = array($this->_has_many[$alias]['foreign_key'], $this->_has_many[$alias]['far_key']);
	$foreign_key = $this->pk();

	if ($data !== NULL)
	{
		// Additional data stored in pivot table
		$columns = array_merge($columns, array_keys($data));
	}

	$query = DB::insert($this->_has_many[$alias]['through'], $columns);

	foreach ( (array) $far_keys as $key)
	{
		$values = array($foreign_key, $key);
		if ($data !== NULL)
		{
			// Additional data stored in pivot table
			$values  = array_merge($values, array_values($data));
		}
		
		$query->values($values);
	}

	$query->execute($this->_db);

	return $this;
}

public and_having( mixed $column , string $op [, mixed $value = NULL ] ) (defined in Gleez_ORM_Core)

Creates a new "AND HAVING" condition for the query.

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value = NULL - Column value

Return Values

  • $this

Source Code

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;
}

public and_having_close( ) (defined in Gleez_ORM_Core)

Closes an open "AND HAVING (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public and_having_open( ) (defined in Gleez_ORM_Core)

Opens a new "AND HAVING (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public and_where( mixed $column , string $op , mixed $value ) (defined in Gleez_ORM_Core)

Creates a new "AND WHERE" condition for the query.

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value required - Column value

Return Values

  • $this

Source Code

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;
}

public and_where_close( ) (defined in Gleez_ORM_Core)

Closes an open "AND WHERE (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public and_where_open( ) (defined in Gleez_ORM_Core)

Opens a new "AND WHERE (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public as_array( ) (defined in Gleez_ORM_Core)

Returns the values of this object as an array, including any related one-one models that have already been loaded using with()

Return Values

  • array

Source Code

public function as_array()
{
	$object = array();
	$extra = array('url', 'edit_url', 'delete_url');

	foreach ($this->_object as $column => $value)
	{
		// Call __get for any user processing
		$object[$column] = $this->__get($column);
	}

	foreach ($this->_related as $column => $model)
	{
		// Include any related objects that are already loaded
		$object[$column] = $model->as_array();
	}

	foreach ($extra as $column)
	{
		try
		{
			// Call __get for any user processing
			$object[$column] = $this->__get($column);
		}catch(Exception $e){}
	}

	return $object;
}

public belongs_to( ) (defined in Gleez_ORM_Core)

Source Code

public function belongs_to()
{
	return $this->_belongs_to;
}

public cached( [ integer $lifetime = NULL ] ) (defined in Gleez_ORM_Core)

Enables the query to be cached for a specified amount of time.

Parameters

  • integer $lifetime = NULL - Number of seconds to cache

Tags

Return Values

  • $this

Source Code

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;
}

public changed( [ string $field = NULL ] ) (defined in Gleez_ORM_Core)

Check whether the model data has been modified. If $field is specified, checks whether that field was modified.

Parameters

  • string $field = NULL - Field to check for changes

Return Values

  • bool - Whether or not the field has changed

Source Code

public function changed($field = NULL)
{
	return ($field === NULL)
		? $this->_changed
		: Arr::get($this->_changed, $field);
}

public check( [ Validation $extra_validation = NULL ] ) (defined in Gleez_ORM_Core)

Validates the current model's data

Parameters

  • Validation $extra_validation = NULL - Validation object

Return Values

  • ORM

Source Code

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();

               // add custom rules to $this->_validation();
	Module::event($this->_object_name .'_validation', $this->_validation, $extra_errors);
       
	$array = $this->_validation;

	if (($this->_valid = $array->check()) === FALSE OR $extra_errors)
	{
		$exception = new ORM_Validation_Exception($this->errors_filename(), $array);

		if ($extra_errors)
		{
			// Merge any possible errors from the external object
			$exception->add_object('_external', $extra_validation);
		}
		$this->_validation = NULL; //Fixed memory leak @http://dev.kohanaframework.org/issues/4286
		throw $exception;
	}

	$this->_validation = NULL; //Fixed memory leak @http://dev.kohanaframework.org/issues/4286
	return $this;
}

public clear( ) (defined in Gleez_ORM_Core)

Unloads the current object and clears the status.

Tags

  • Chainable -

Return Values

  • ORM

Source Code

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
	$this->_object = $this->_changed = $this->_related = $this->_original_values = array();

	// Replace the current object with an empty one
	$this->_load_values($values);

	// Reset primary key
	$this->_primary_key_value = NULL;

	$this->reset();

	return $this;
}

public count_all( ) (defined in Gleez_ORM_Core)

Count the number of records in the table.

Return Values

  • integer

Source Code

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]);
		}
		elseif ($method['name'] == 'order_by')
		{
			// Also, ignore order clause
			$order_by[$key] = $method; // Fix of the fix here!
			unset($this->_db_pending[$key]);
		}
	}

	if ( ! empty($this->_load_with))
	{
		foreach ($this->_load_with as $alias)
		{
			// Bind relationship
			$this->with($alias);
		}
	}

	$this->_build(Database::SELECT);

	$records = $this->_db_builder->from(array($this->_table_name, $this->_object_name))
		->select(array('COUNT("*")', 'records_found'))
		->execute($this->_db)
		->get('records_found');

	// Add back in selected columns
	//$this->_db_pending += $selects;
	$this->_db_pending = array_merge($this->_db_pending, $selects);

	if( isset($order_by) )
	{
		// Add back in order_by clause
		//$this->_db_pending += $order_by;
		$this->_db_pending = array_merge($this->_db_pending, $order_by);
	}

	$this->reset();

	// Return the total number of records in a table
	return $records;
}

public create( [ Validation $validation = NULL ] ) (defined in Gleez_ORM_Core)

Insert a new object to the database added event support

Parameters

  • Validation $validation = NULL - Validation object

Return Values

  • ORM

Source Code

public function create(Validation $validation = NULL)
{
	if ($this->_loaded)
		throw new Kohana_Exception('Cannot create :model model because it is already loaded.', array(':model' => $this->_object_name));

               Module::event($this->_object_name .'_prevalid', $this, $validation);
       
	// Require model validation before saving
	if ( ! $this->_valid OR $validation)
	{
		$this->check($validation);
	}

	$this->before_save();
               Module::event($this->_object_name .'_presave', $this, $validation);

	$data = array();
	foreach ($this->_changed as $column)
	{
		// Generate list of column => values
		$data[$column] = $this->_object[$column];
	}

	if (is_array($this->_created_column))
	{
		// Fill the created column
		$column = $this->_created_column['column'];
		$format = $this->_created_column['format'];

		$data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
	}

	$result = DB::insert($this->_table_name)
		->columns(array_keys($data))
		->values(array_values($data))
		->execute($this->_db);

	if ( ! array_key_exists($this->_primary_key, $data))
	{
		// Load the insert id as the primary key if it was left out
		$this->_object[$this->_primary_key] = $this->_primary_key_value = $result[0];
	}

	// Object is now loaded and saved
	$this->_loaded = $this->_saved = TRUE;

	// All changes have been saved
	$this->_changed = array();
	$this->_original_values = $this->_object;

	$this->after_save();
               Module::event($this->_object_name .'_save', $this);

	return $this;
}

public created_column( ) (defined in Gleez_ORM_Core)

Source Code

public function created_column()
{
	return $this->_created_column;
}

public dataTables( ) (defined in Gleez_ORM_Core)

Setter/Getter for jquery DataTables support

Return Values

  • object - DataTables

Source Code

public function dataTables(array $columns = NULL)
{
	if ( ! empty($columns) )
	{
		$this->_datatables = DataTables::factory($this)->columns($columns)->execute();
	}
	
	return $this->_datatables;
}

public delete_all( ) (defined in Gleez_ORM_Core)

Delete all objects in the associated table. This does NOT destroy relationships that have been created with other objects.

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public function delete_all()
{
	if ( $this->_loaded)
		throw new Kohana_Exception('Cannot delete all :model model because it is loaded.', array(':model' => $this->_object_name));
       
               Module::event($this->_object_name .'_pre_delete_all', $this);
       
	$this->_build(ORM::DELETE);
	$this->_db_builder->execute($this->_db);

               Module::event($this->_object_name .'_delete_all', $this);
       
	return $this->clear();
}

public distinct( boolean $value ) (defined in Gleez_ORM_Core)

Enables or disables selecting only unique columns using "SELECT DISTINCT"

Parameters

  • boolean $value required - Enable or disable distinct columns

Return Values

  • $this

Source Code

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;
}

public errors_filename( ) (defined in Gleez_ORM_Core)

Source Code

public function errors_filename()
{
	return $this->_errors_filename;
}

public static factory( string $model [, mixed $id = NULL ] ) (defined in Gleez_ORM_Core)

Creates and returns a new model.

Parameters

  • string $model required - Model name
  • mixed $id = NULL - Parameter for find()

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public static function factory($model, $id = NULL)
{
	// Set class name
	$model = 'Model_'.ucfirst($model);

	return new $model($id);
}

public filters( ) (defined in Gleez_ORM_Core)

Filter definitions for validation

Return Values

  • array

Source Code

public function filters()
{
	return array();
}

public find( ) (defined in Gleez_ORM_Core)

Finds and loads a single database row into the object.

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public function find()
{
	if ($this->_loaded)
		throw new Kohana_Exception('Method find() cannot be called on loaded objects');

	if ( ! empty($this->_load_with))
	{
		foreach ($this->_load_with as $alias)
		{
			// Bind auto relationships
			$this->with($alias);
		}
	}

	$this->_build(Database::SELECT);

	return $this->_load_result(FALSE);
}

public find_all( ) (defined in Gleez_ORM_Core)

Finds multiple database rows and returns an iterator of the rows found.

Return Values

  • Database_Result

Source Code

public function find_all()
{
	if ($this->_loaded)
		throw new Kohana_Exception('Method find_all() cannot be called on loaded objects');

	if ( ! empty($this->_load_with))
	{
		foreach ($this->_load_with as $alias)
		{
			// Bind auto relationships
			$this->with($alias);
		}
	}

	$this->_build(Database::SELECT);

	return $this->_load_result(TRUE);
}

public find_or_create( array $values ) (defined in Gleez_ORM_Core)

Finds or creates a model instance with the given values

Parameters

  • array $values required - $values

Tags

Return Values

  • $this

Source Code

public function find_or_create($values)
{
	// Unload any previously loaded objects
	$this->clear();

	// Attempt to find the object
	foreach ($values as $key => $value)
	{
	    $this->where($key, '=', $value);
	}
	
	$found = $this->find()->loaded();
	
	if ($found)
	{
	    return $this;
	}
	else
	{
	    $this->values($values);
	    $s = $this->save();
	    return $s;
	}
}

public from( mixed $tables ) (defined in Gleez_ORM_Core)

Choose the tables to select "FROM ..."

Parameters

  • mixed $tables required - Table name or array($table, $alias) or object

Return Values

  • $this

Source Code

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;
}

public group_by( mixed $columns ) (defined in Gleez_ORM_Core)

Creates a "GROUP BY ..." filter.

Parameters

  • mixed $columns required - Column name or array($column, $alias) or object

Return Values

  • $this

Source Code

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;
}

public has( string $alias [, mixed $far_keys = NULL ] ) (defined in Gleez_ORM_Core)

Tests if this object has a relationship to a different model, or an array of different models.

// 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));
// Check if $model has any roles
$model->has('roles')

Parameters

  • string $alias required - Alias of the has_many "through" relationship
  • mixed $far_keys = NULL - Related model, primary key, or an array of primary keys

Return Values

  • Database_Result

Source Code

public function has($alias, $far_keys = NULL)
{
	if ($far_keys === NULL)
	{
		return (bool) DB::select(array('COUNT("*")', 'records_found'))
			->from($this->_has_many[$alias]['through'])
			->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
			->execute($this->_db)->get('records_found');
	}

	$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;

	// We need an array to simplify the logic
	$far_keys = (array) $far_keys;

	// Nothing to check if the model isn't loaded or we don't have any far_keys
	if ( ! $far_keys OR ! $this->_loaded)
		return FALSE;

	$count = (int) DB::select(array('COUNT("*")', 'records_found'))
		->from($this->_has_many[$alias]['through'])
		->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
		->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
		->execute($this->_db)->get('records_found');

	// Rows found need to match the rows searched
	return $count === count($far_keys);
}

public has_many( ) (defined in Gleez_ORM_Core)

Source Code

public function has_many()
{
	return $this->_has_many;
}

public has_many_keys( ) (defined in Gleez_ORM_Core)

Source Code

public function has_many_keys()
{
	return $this->_has_many_keys;
}

public has_one( ) (defined in Gleez_ORM_Core)

Source Code

public function has_one()
{
	return $this->_has_one;
}

public having( mixed $column , string $op [, mixed $value = NULL ] ) (defined in Gleez_ORM_Core)

Alias of and_having()

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value = NULL - Column value

Return Values

  • $this

Source Code

public function having($column, $op, $value = NULL)
{
	return $this->and_having($column, $op, $value);
}

public having_close( ) (defined in Gleez_ORM_Core)

Closes an open "AND HAVING (...)" grouping.

Return Values

  • $this

Source Code

public function having_close()
{
	return $this->and_having_close();
}

public having_open( ) (defined in Gleez_ORM_Core)

Alias of and_having_open()

Return Values

  • $this

Source Code

public function having_open()
{
	return $this->and_having_open();
}

public join( mixed $table [, string $type = NULL ] ) (defined in Gleez_ORM_Core)

Adds addition tables to "JOIN ...".

Parameters

  • mixed $table required - Column name or array($column, $alias) or object
  • string $type = NULL - Join type (LEFT, RIGHT, INNER, etc)

Return Values

  • $this

Source Code

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;
}

public last_query( ) (defined in Gleez_ORM_Core)

Returns last executed query

Return Values

  • string

Source Code

public function last_query()
{
	return $this->_db->last_query;
}

public limit( integer $number ) (defined in Gleez_ORM_Core)

Return up to "LIMIT ..." results

Parameters

  • integer $number required - Maximum results to return

Return Values

  • $this

Source Code

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;
}

public list_columns( ) (defined in Gleez_ORM_Core)

Proxy method to Database list_columns.

Return Values

  • array

Source Code

public function list_columns()
{
	// Proxy to database
	return $this->_db->list_columns($this->_table_name);
}

public load_with( ) (defined in Gleez_ORM_Core)

Source Code

public function load_with()
{
	return $this->_load_with;
}

public loaded( ) (defined in Gleez_ORM_Core)

Source Code

public function loaded()
{
	return $this->_loaded;
}

public my_on( mixed $c1 , string $op , mixed $c2 ) (defined in Gleez_ORM_Core)

Adds "ON ..." conditions for the last created JOIN statement.

Parameters

  • mixed $c1 required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $c2 required - Column name or array($column, $alias) or object

Return Values

  • $this

Source Code

public function my_on($c1, $op, $c2)
{
	// Add pending database call which is executed after query type is determined
	$this->_db_pending[] = array(
		'name' => 'join_and',
		'args' => array($c1, $op, $c2),
	);

	return $this;
}

public object( ) (defined in Gleez_ORM_Core)

Source Code

public function object()
{
	return $this->_object;
}

public object_name( ) (defined in Gleez_ORM_Core)

Source Code

public function object_name()
{
	return $this->_object_name;
}

public object_plural( ) (defined in Gleez_ORM_Core)

Source Code

public function object_plural()
{
	return $this->_object_plural;
}

public offset( integer $number ) (defined in Gleez_ORM_Core)

Start returning results after "OFFSET ..."

Parameters

  • integer $number required - Starting result number

Return Values

  • $this

Source Code

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;
}

public on( mixed $c1 , string $op , mixed $c2 ) (defined in Gleez_ORM_Core)

Adds "ON ..." conditions for the last created JOIN statement.

Parameters

  • mixed $c1 required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $c2 required - Column name or array($column, $alias) or object

Return Values

  • $this

Source Code

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;
}

public or_having( mixed $column , string $op [, mixed $value = NULL ] ) (defined in Gleez_ORM_Core)

Creates a new "OR HAVING" condition for the query.

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value = NULL - Column value

Return Values

  • $this

Source Code

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;
}

public or_having_close( ) (defined in Gleez_ORM_Core)

Closes an open "OR HAVING (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public or_having_open( ) (defined in Gleez_ORM_Core)

Opens a new "OR HAVING (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public or_where( mixed $column , string $op , mixed $value ) (defined in Gleez_ORM_Core)

Creates a new "OR WHERE" condition for the query.

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value required - Column value

Return Values

  • $this

Source Code

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;
}

public or_where_close( ) (defined in Gleez_ORM_Core)

Closes an open "OR WHERE (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public or_where_open( ) (defined in Gleez_ORM_Core)

Opens a new "OR WHERE (...)" grouping.

Return Values

  • $this

Source Code

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;
}

public order_by( mixed $column [, string $direction = NULL ] ) (defined in Gleez_ORM_Core)

Applies sorting with "ORDER BY ..."

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $direction = NULL - Direction of sorting

Return Values

  • $this

Source Code

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;
}

public original_values( ) (defined in Gleez_ORM_Core)

Source Code

public function original_values()
{
	return $this->_original_values;
}

public param( string $param , mixed $value ) (defined in Gleez_ORM_Core)

Set the value of a parameter in the query.

Parameters

  • string $param required - Parameter key to replace
  • mixed $value required - Value to use

Return Values

  • $this

Source Code

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;
}

public pk( ) (defined in Gleez_ORM_Core)

Returns the value of the primary key

Return Values

  • mixed - Primary key

Source Code

public function pk()
{
	return $this->_primary_key_value;
}

public primary_key( ) (defined in Gleez_ORM_Core)

Source Code

public function primary_key()
{
	return $this->_primary_key;
}

public reload( ) (defined in Gleez_ORM_Core)

Reloads the current object from the database.

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public function reload()
{
	$primary_key = $this->pk();

	// Replace the object and reset the object status
	$this->_object = $this->_changed = $this->_related = $this->_original_values = array();

	// Only reload the object if we have one to reload
	if ($this->_loaded)
		return $this->clear()
			->where($this->_object_name.'.'.$this->_primary_key, '=', $primary_key)
			->find();
	else
		return $this->clear();
}

public reload_columns( [ boolean $force = bool FALSE ] ) (defined in Gleez_ORM_Core)

Reload column definitions.

Parameters

  • boolean $force = bool FALSE - Force reloading

Tags

  • Chainable -

Return Values

  • ORM

Source Code

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
			$this->_table_columns = $this->list_columns(TRUE);

			// Load column cache
			ORM::$_column_cache[$this->_object_name] = $this->_table_columns;
		}
	}

	return $this;
}

public remove( string $alias [, mixed $far_keys = NULL ] ) (defined in Gleez_ORM_Core)

Removes a relationship between this model and another.

// 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');

Parameters

  • string $alias required - Alias of the has_many "through" relationship
  • mixed $far_keys = NULL - Related model, primary key, or an array of primary keys

Return Values

  • ORM

Source Code

public function remove($alias, $far_keys = NULL)
{
	$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);

	return $this;
}

public reset( [ bool $next = bool TRUE ] ) (defined in Gleez_ORM_Core)

Clears query builder. Passing FALSE is useful to keep the existing query conditions for another query.

Parameters

  • bool $next = bool TRUE - Pass FALSE to avoid resetting on the next call

Return Values

  • ORM

Source Code

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;
}

public saved( ) (defined in Gleez_ORM_Core)

Source Code

public function saved()
{
	return $this->_saved;
}

public select( [ mixed $columns = NULL ] ) (defined in Gleez_ORM_Core)

Choose the columns to select from.

Parameters

  • mixed $columns = NULL - Column name or array($column, $alias) or object

Return Values

  • $this

Source Code

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;
}

public serialize( ) (defined in Gleez_ORM_Core)

Allows serialization of only the object data and state, to prevent "stale" objects being unserialized, which also requires less memory.

Return Values

  • array

Source Code

public function serialize()
{
	// Store only information about the object
	foreach (array('_primary_key_value', '_object', '_changed', '_loaded', '_saved', '_sorting', '_original_values', '_ignored_columns') as $var)
	{
		$data[$var] = $this->{$var};
	}

	return serialize($data);
}

public set( string $column , mixed $value ) (defined in Gleez_ORM_Core)

Handles setting of column

Parameters

  • string $column required - Column name
  • mixed $value required - Column value

Return Values

  • void

Source Code

public function set($column, $value)
{
               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);
	}

	if (array_key_exists($column, $this->_ignored_columns))
	{
		// No processing for ignored columns, just store it
		$this->_object[$column] = $value;
	}
	elseif (array_key_exists($column, $this->_object))
	{
		// Filter the data
		$value = $this->run_filter($column, $value);

		// See if the data really changed
		if ($value !== $this->_object[$column])
		{
			$this->_object[$column] = $value;

			// Data has changed
			$this->_changed[$column] = $column;

			// Object is no longer saved or valid
			$this->_saved = $this->_valid = FALSE;
		}
	}
	elseif (isset($this->_belongs_to[$column]))
	{
		// Update related object itself
		$this->_related[$column] = $value;

		// Update the foreign key of this model
		$this->_object[$this->_belongs_to[$column]['foreign_key']] = $value->pk();

		$this->_changed[$column] = $this->_belongs_to[$column]['foreign_key'];
	}
	else
	{
		throw new Kohana_Exception('The :property: property does not exist in the :class: class',
			array(':property:' => $column, ':class:' => get_class($this)));
	}

	return $this;
}

public table_columns( ) (defined in Gleez_ORM_Core)

Source Code

public function table_columns()
{
	return $this->_table_columns;
}

public table_name( ) (defined in Gleez_ORM_Core)

Source Code

public function table_name()
{
	return $this->_table_name;
}

public unique( string $field , mixed $value ) (defined in Gleez_ORM_Core)

Checks whether a column value is unique. Excludes itself if loaded.

Parameters

  • string $field required - The field to check for uniqueness
  • mixed $value required - The value to check for uniqueness

Return Values

  • bool - Whteher the value is unique

Source Code

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());
}

public unserialize( string $data ) (defined in Gleez_ORM_Core)

Prepares the database connection and reloads the object.

Parameters

  • string $data required - String for unserialization

Return Values

  • void

Source Code

public function unserialize($data)
{
	// Initialize model
	$this->_initialize();

	foreach (unserialize($data) as $name => $var)
	{
		$this->{$name} = $var;
	}

	if ($this->_reload_on_wakeup === TRUE)
	{
		// Reload the object
		$this->reload();
	}
}

public update( [ Validation $validation = NULL ] ) (defined in Gleez_ORM_Core)

Updates a single record or multiple records, added event support

Parameters

  • Validation $validation = NULL - Validation object

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public function update(Validation $validation = NULL)
{
	if ( ! $this->_loaded)
		throw new Kohana_Exception('Cannot update :model model because it is not loaded.', array(':model' => $this->_object_name));

	Module::event($this->_object_name .'_prevalid', $this, $validation);

	// Run validation if the model isn't valid or we have additional validation rules.
	if ( ! $this->_valid OR $validation)
	{
		$this->check($validation);
	}

	if (empty($this->_changed))
	{
		return $this;
	}

	$this->before_save();
               Module::event($this->_object_name .'_presave', $this, $validation);

	$data = array();
	foreach ($this->_changed as $column)
	{
		// Compile changed data
		$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);
	}

	// Use primary key value
	$id = $this->pk();

	// Update a single record
	DB::update($this->_table_name)
		->set($data)
		->where($this->_primary_key, '=', $id)
		->execute($this->_db);

	if (isset($data[$this->_primary_key]))
	{
		// Primary key was changed, reflect it
		$this->_primary_key_value = $data[$this->_primary_key];
	}

	// Object has been saved
	$this->_saved  = $this->_updated = TRUE;

	// All changes have been saved
	$this->_changed = array();
	$this->_original_values = $this->_object;

	$this->after_save();
               Module::event($this->_object_name .'_save', $this);

	return $this;
}

public updated( ) (defined in Gleez_ORM_Core)

Source Code

public function updated()
{
	return $this->_updated;
}

public updated_column( ) (defined in Gleez_ORM_Core)

Source Code

public function updated_column()
{
	return $this->_updated_column;
}

public using( string $columns ) (defined in Gleez_ORM_Core)

Adds "USING ..." conditions for the last created JOIN statement.

Parameters

  • string $columns required - Column name

Return Values

  • $this

Source Code

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;
}

public validation( ) (defined in Gleez_ORM_Core)

Source Code

public function validation()
{
	if ( ! isset($this->_validation))
	{
		// Initialize the validation object
		$this->_validation();
	}

	return $this->_validation;
}

public values( array $values [, array $expected = NULL ] ) (defined in Gleez_ORM_Core)

Set values from an array with support for one-one relationships. This method should be used for loading in post data, etc.

Parameters

  • array $values required - Array of column => val
  • array $expected = NULL - Array of keys to take from $values

Return Values

  • ORM

Source Code

public function values(array $values, array $expected = NULL)
{
	// Default to expecting everything except the primary key
	if ($expected === NULL)
	{
		$expected = array_keys($this->_table_columns);

		// Don't set the primary key by default
		unset($values[$this->_primary_key]);
	}
       
               if ( ! empty($this->_ignored_columns) )
	{
		// merge the columns needed to process
		$expected = array_merge($expected, array_keys($this->_ignored_columns) );
	}
       
	foreach ($expected as $key => $column)
	{
		if (is_string($key))
		{
			// isset() fails when the value is NULL (we want it to pass)
			if ( ! array_key_exists($key, $values))
				continue;

			// Try to set values to a related model
			$this->{$key}->values($values[$key], $column);
		}
		else
		{
			// 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];
		}
	}

	return $this;
}

public where( mixed $column , string $op , mixed $value ) (defined in Gleez_ORM_Core)

Alias of and_where()

Parameters

  • mixed $column required - Column name or array($column, $alias) or object
  • string $op required - Logic operator
  • mixed $value required - Column value

Return Values

  • $this

Source Code

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;
}

public where_close( ) (defined in Gleez_ORM_Core)

Closes an open "AND WHERE (...)" grouping.

Return Values

  • $this

Source Code

public function where_close()
{
	return $this->and_where_close();
}

public where_open( ) (defined in Gleez_ORM_Core)

Alias of and_where_open()

Return Values

  • $this

Source Code

public function where_open()
{
	return $this->and_where_open();
}

public with( string $target_path ) (defined in Gleez_ORM_Core)

Binds another one-to-one object to this model. One-to-one objects can be nested using 'object1:object2' syntax

Parameters

  • string $target_path required - Target model to bind to

Return Values

  • void

Source Code

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);
	$target = $this;
	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
		$parent_path = $this->_object_name;
	}
	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)
	{
		$name = $target_path.'.'.$column;
		$alias = $target_path.':'.$column;

		// Add the prefix so that load_result can determine the relationship
		$this->select(array($name, $alias));
	}

	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;
}

protected create_space( int $start [, int $size = integer 2 ] ) (defined in Gleez_ORM_MPTT)

Adds space to the tree for adding or inserting nodes.

Parameters

  • int $start required - Start position
  • int $size = integer 2 - Size of the gap to add [optional]

Return Values

  • void

Source Code

protected function create_space($start, $size = 2)
{
	DB::update($this->_table_name)
		->set(array($this->left_column => DB::expr($this->left_column.' + '.$size)))
		->where($this->left_column,'>=', $start)
		->where($this->scope_column, '=', $this->scope())
		->execute($this->_db);

	DB::update($this->_table_name)
		->set(array($this->right_column => DB::expr($this->right_column.' + '.$size)))
		->where($this->right_column,'>=', $start)
		->where($this->scope_column, '=', $this->scope())
		->execute($this->_db);
}

protected delete_space( int $start [, int $size = integer 2 ] ) (defined in Gleez_ORM_MPTT)

Removes space from the tree after deleting or moving nodes.

Parameters

  • int $start required - Start position
  • int $size = integer 2 - Size of the gap to remove [optional]

Return Values

  • void

Source Code

protected function delete_space($start, $size = 2)
{
	DB::update($this->_table_name)
		->set(array($this->left_column => DB::expr($this->left_column.' - '.$size)))
		->where($this->left_column, '>=', $start)
		->where($this->scope_column, '=', $this->scope())
		->execute($this->_db);

	DB::update($this->_table_name)
		->set(array($this->right_column => DB::expr($this->right_column.' - '.$size)))
		->where($this->right_column,'>=', $start)
		->where($this->scope_column, '=', $this->scope())
		->execute($this->_db);
}

protected get_next_scope( ) (defined in Gleez_ORM_MPTT)

Returns the next available value for scope.

Return Values

  • int

Source Code

protected function get_next_scope()
{
	$scope = DB::select(DB::expr('IFNULL(MAX(`'.$this->scope_column.'`), 0) as scope'))
			->from($this->_table_name)
			->execute($this->_db)
			->current();

	if ($scope AND intval($scope['scope']) > 0)
		return intval($scope['scope']) + 1;

	return 1;
}

protected insert( ORM_MPTT|int $target , string $copy_left_from , int $left_offset , int $level_offset ) (defined in Gleez_ORM_MPTT)

Insert the object

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node.
  • string $copy_left_from required - Target object property to take new left value from
  • int $left_offset required - Offset for left value
  • int $level_offset required - Offset for level value

Tags

Return Values

  • ORM_MPTT

Source Code

protected function insert($target, $copy_left_from, $left_offset, $level_offset)
{
	// Insert should only work on new nodes.. if its already it the tree it needs to be moved!
	if ($this->loaded())
		return FALSE;
	 
	 
	if ( ! $target instanceof $this)
	{
		$target = self::factory($this->object_name(), array($this->primary_key() => $target));
	 
		if ( ! $target->loaded())
		{
			return FALSE;
		}
	}
	else
	{
		$target->reload();
	}
	
	//$this->lock();
	// Start the transaction
	$this->_db->begin();

	$this->{$this->left_column} = $target->{$copy_left_from} + $left_offset;
	$this->{$this->right_column} = $this->{$this->left_column} + 1;
	$this->{$this->level_column} = $target->{$this->level_column} + $level_offset;
	$this->{$this->scope_column} = $target->{$this->scope_column};

	$this->create_space($this->{$this->left_column});
	 
	try
	{
		parent::save();
	}
	catch (ORM_Validation_Exception $e)
	{
		// We had a problem saving, make sure we clean up the tree
		$this->delete_space($this->left());
		//$this->unlock();
		$this->_db->commit();
		throw $e;
	}
	
	//$this->unlock();
	// Commit the transaction
	$this->_db->commit();
	
	return $this;
}

protected lock( ) (defined in Gleez_ORM_MPTT)

Locks the current table.

Return Values

  • void

Source Code

protected function lock()
{
	$this->_db->query(NULL, 'LOCK TABLE '.$this->_db->quote_table($this->_table_name).' WRITE', TRUE);
}

protected move( ) (defined in Gleez_ORM_MPTT)

Source Code

protected function move($target, $left_column, $left_offset, $level_offset, $allow_root_target)
{
	if ( ! $this->loaded())
		return FALSE;
  
	// store the changed parent id before reload
	$parent_id = $this->{$this->parent_column};

	// Make sure we have the most upto date version of this AFTER we lock
	//$this->lock();
	// Start the transaction
	$this->_db->begin();
	$this->reload();
	 
	// Catch any database or other excpetions and unlock
	try
	{
		if ( ! $target instanceof $this)
		{
			$target = self::factory($this->object_name(), array($this->primary_key() => $target));
			 
			if ( ! $target->loaded())
			{
				//$this->unlock();
				$this->_db->rollback();
				return FALSE;
			}
		}
		else
		{
			$target->reload();
		}

		// Stop $this being moved into a descendant or itself or disallow if target is root
		if ($target->is_descendant($this)
			OR $this->{$this->primary_key()} === $target->{$this->primary_key()}
			OR ($allow_root_target === FALSE AND $target->is_root()))
		{
			//$this->unlock();
			$this->_db->rollback();
			return FALSE;
		}

		if ($level_offset > 0)
		{
			// We're moving to a child node so add 1 to left offset.
			$left_offset = ($left_column === TRUE) ? ($target->left() + 1) : ($target->right() + $left_offset);
		}
		else
		{
			$left_offset = ($left_column === TRUE) ? $target->left() : ($target->right() + $left_offset);
		}
		
		$level_offset = $target->level() - $this->level() + $level_offset;
		$size = $this->size();

		$this->create_space($left_offset, $size);

		$this->reload();

		$offset = ($left_offset - $this->left());
		
		$this->_db->query(Database::UPDATE, 'UPDATE '.$this->_db->quote_table($this->_table_name).' SET `'
			. $this->left_column.'` = `'.$this->left_column.'` + '
			. $offset.', `'.$this->right_column.'` =  `'.$this->right_column.'` + '
			. $offset.', `'.$this->level_column.'` =  `'.$this->level_column.'` + '
			. $level_offset.', `'.$this->scope_column.'` = '.$target->scope()
			. ' WHERE `'.$this->left_column.'` >= '.$this->left().' AND `'
			. $this->right_column.'` <= '.$this->right().' AND `'
			. $this->scope_column.'` = '.$this->scope(), TRUE);
		
		$this->delete_space($this->left(), $size);
	}
	catch (Kohana_Exception $e)
	{
		// Unlock table and re-throw exception
		//$this->unlock();
		$this->_db->rollback();
		throw $e;
	}

	// all went well so save the parent_id if changed
	if ($parent_id != $this->{$this->parent_column})
	{
		$this->{$this->parent_column} = $parent_id;
		$this->save();
	}

	//$this->unlock();
	// Commit the transaction
	$this->_db->commit();
	$this->reload();

	return $this;
}

protected parent_from( ORM_MPTT|int $target [, string $column = NULL ] ) (defined in Gleez_ORM_MPTT)

Sets the parent_column value to the given targets column value. Returns the target ORM_MPTT object.

Parameters

  • ORM_MPTT|int $target required - Primary key value or ORM_MPTT object of target node
  • string $column = NULL - Name of the targets nodes column to use

Return Values

  • ORM_MPTT

Source Code

protected function parent_from($target, $column = NULL)
{
	if ( ! $target instanceof $this)
	{
		$target = self::factory($this->object_name(), array($this->primary_key() => $target));
	}

	if ($column === NULL)
	{
		$column = $target->primary_key();
	}

	if ($target->loaded())
	{
		$this->{$this->parent_column} = $target->{$column};
	}
	else
	{
		$this->{$this->parent_column} = NULL;
	}

	return $target;
}

protected scope_available( int $scope ) (defined in Gleez_ORM_MPTT)

Checks if the supplied scope is available.

Parameters

  • int $scope required - Scope to check availability of

Return Values

  • bool

Source Code

protected function scope_available($scope)
{
	return (bool) ! self::factory($this->_object_name)
		->where($this->scope_column, '=', $scope)
		->count_all();
}

protected unlock( ) (defined in Gleez_ORM_MPTT)

Unlocks the current table.

Return Values

  • void

Source Code

protected function unlock()
{
	$this->_db->query(NULL, 'UNLOCK TABLES', TRUE);
}

protected _build( integer $type ) (defined in Gleez_ORM_Core)

Initializes the Database Builder to given query type

Parameters

  • integer $type required - Type of Database query

Return Values

  • ORM

Source Code

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:
			$this->_db_builder = DB::update(array($this->_table_name, $this->_object_name));
		break;
		case Database::DELETE:
			$this->_db_builder = DB::delete(array($this->_table_name, $this->_object_name));
		break;
		case ORM::DELETE:
			$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;
}

protected _build_select( ) (defined in Gleez_ORM_Core)

Returns an array of columns to include in the select query. This method can be overridden to change the default select behavior.

Return Values

  • array - Columns to select

Source Code

protected function _build_select()
{
        $columns = array();
        
        foreach ($this->_table_columns as $column => $_)
        {
        $columns[] = array($this->_object_name.'.'.$column, $column);
        }
        
        return $columns;
}

protected _initialize( ) (defined in Gleez_ORM_Core)

Prepares the model database connection, determines the table name, and loads column information.

Return Values

  • void

Source Code

protected function _initialize()
{
	// Set the object name and plural name
	$this->_object_name = strtolower(substr(get_class($this), 6));

	// 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(),
			'_has_many_keys'  => array(),
		);

		// Set the object plural name if none predefined
		if ( ! isset($this->_object_plural))
		{
			$init['_object_plural'] = Inflector::plural($this->_object_name);
		}

		if ( ! $this->_errors_filename)
		{
			$init['_errors_filename'] = $this->_object_name;
		}

		if ( ! is_object($this->_db))
		{
			// Get database instance
			$init['_db'] = Database::instance($this->_db_group);
		}

		if (empty($this->_table_name))
		{
			// 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);
			}
		}

		if ( ! empty($this->_ignored_columns))
		{
			// Optimize for performance
			$init['_ignored_columns']= array_combine($this->_ignored_columns, $this->_ignored_columns);
		}

		$defaults = array();

		foreach ($this->_belongs_to as $alias => $details)
		{
			$defaults['model'] = $alias;
			$defaults['foreign_key'] = $alias.$this->_foreign_key_suffix;

			$init['_belongs_to'][$alias] = array_merge($defaults, $details);
		}

		foreach ($this->_has_one as $alias => $details)
		{
			$defaults['model'] = $alias;
			$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;

			$init['_has_one'][$alias] = array_merge($defaults, $details);
		}

		foreach ($this->_has_many as $alias => $details)
		{
			$defaults['model'] = Inflector::singular($alias);
			$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
			$defaults['through'] = NULL;
			$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
		
			$init['_has_many'][$alias] = array_merge($defaults, $details);
		}

		foreach ($this->_has_many_keys as $alias => $details)
		{
			$defaults['model'] = Inflector::singular($alias);
			$defaults['foreign_key'] = array($this->_object_name.$this->_foreign_key_suffix);
			$defaults['far_key'] = array(Inflector::singular($alias).$this->_foreign_key_suffix);
		
			$init['_has_many_keys'][$alias] = array_merge($defaults, $details);
		}
		
		ORM::$_init_cache[$this->_object_name] = $init;
	}

	// Assign initialized properties to the current object
	foreach ($init as $property => $value)
	{
		$this->{$property} = $value;
	}

	// Load column information
	$this->reload_columns();

	// Clear initial model state
	$this->clear();
}

protected _load_result( [ bool $multiple = bool FALSE ] ) (defined in Gleez_ORM_Core)

Loads a database result, either as a new record for this model, or as an iterator for multiple rows.

Parameters

  • bool $multiple = bool FALSE - Return an iterator or load a single row

Tags

  • Chainable -

Return Values

  • ORM|Database_Result

Source Code

protected function _load_result($multiple = FALSE)
{
	$this->_db_builder->from(array($this->_table_name, $this->_object_name));

	if ($multiple === FALSE)
	{
		// Only fetch 1 record
		$this->_db_builder->limit(1);
	}

	// Select all columns by default
	$this->_db_builder->select($this->_object_name.'.*');

	if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
	{
		foreach ($this->_sorting as $column => $direction)
		{
			if (strpos($column, '.') === FALSE)
			{
				// Sorting column for use in JOINs
				$column = $this->_object_name.'.'.$column;
			}

			$this->_db_builder->order_by($column, $direction);
		}
	}

	if ($multiple === TRUE)
	{
		// Return database iterator casting to this object type
		$result = $this->_db_builder->as_object(get_class($this))->execute($this->_db);

		$this->reset();

		return $result;
	}
	else
	{
		// 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;
	}
}

protected _load_values( array $values ) (defined in Gleez_ORM_Core)

Loads an array of values into into the current object.

Parameters

  • array $values required - Values to load

Tags

  • Chainable -

Return Values

  • ORM

Source Code

protected function _load_values(array $values)
{
	if (array_key_exists($this->_primary_key, $values))
	{
		if ($values[$this->_primary_key] !== NULL)
		{
			// Flag as loaded and valid
			$this->_loaded = $this->_valid = TRUE;

			// Store primary key
			$this->_primary_key_value = $values[$this->_primary_key];
		}
		else
		{
			// Not loaded or valid
			$this->_loaded = $this->_valid = FALSE;
		}
	}

	// 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);
		}
	}

	if ($this->_loaded)
	{
		// Store the object in its original state
		$this->_original_values = $this->_object;
	}

	return $this;
}

Returns an ORM model for the given one-one related alias

Parameters

  • string $alias required - Alias name

Return Values

  • ORM

Source Code

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;
	}
}

protected _serialize_value( ) (defined in Gleez_ORM_Core)

Source Code

protected function _serialize_value($value)
{
	return json_encode($value);
}

protected _unserialize_value( ) (defined in Gleez_ORM_Core)

Source Code

protected function _unserialize_value($value)
{
	return json_decode($value);
}

protected _validation( ) (defined in Gleez_ORM_Core)

Initializes validation rules, and labels

Return Values

  • void

Source Code

protected function _validation()
{
	// Build the validation object with its rules
	$this->_validation = Validation::factory($this->_object)
		->bind(':model', $this)
		->bind(':original_values', $this->_original_values)
		->bind(':changed', $this->_changed);

	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);
	}
}

protected after_delete( ) (defined in Gleez_ORM_Core)

Override this method to take actions after the document is deleted

Source Code

protected function after_delete($id){}

protected after_load( ) (defined in Gleez_ORM_Core)

Override this method to take actions after the values are loaded

Source Code

protected function after_load(){}

protected after_save( ) (defined in Gleez_ORM_Core)

Override this method to take actions after data is saved

Source Code

protected function after_save(){}

protected before_delete( ) (defined in Gleez_ORM_Core)

Override this method to take actions before the document is deleted

Source Code

protected function before_delete($id){}

protected before_load( ) (defined in Gleez_ORM_Core)

Override this method to take actions before the values are loaded

Source Code

protected function before_load(){}

protected before_save( ) (defined in Gleez_ORM_Core)

Override this method to take certain actions before the data is saved

Source Code

protected function before_save(){}

protected run_filter( string $field , string $value ) (defined in Gleez_ORM_Core)

Filters a value for a specific column

Parameters

  • string $field required - The column name
  • string $value required - The value to filter

Return Values

  • string

Source Code

protected function run_filter($field, $value)
{
	$filters = $this->filters();

	// 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;
}

private _unique_slug( string $str ) (defined in Model_Menu)

Creates unique slug for menu

Parameters

  • string $str required - $str

Return Values

  • string

Source Code

private function _unique_slug($str)
{
	static $i;

	$i = 1;
	$original = $str;
	
	while ($post = ORM::factory('menu', array('name' => $str)) AND $post->loaded() AND $post->id !== $this->id)
	{
		$str = $original . '-' . $i;
		$i++;
	}

	return $str;
}
Documentation comments powered by Disqus