summaryrefslogtreecommitdiff
path: root/kohana/libraries
diff options
context:
space:
mode:
Diffstat (limited to 'kohana/libraries')
-rw-r--r--kohana/libraries/Database.php13
-rw-r--r--kohana/libraries/ORM.php49
-rw-r--r--kohana/libraries/ORM_Versioned.php143
-rw-r--r--kohana/libraries/drivers/Database/Mssql.php4
-rw-r--r--kohana/libraries/drivers/Image/GraphicsMagick.php6
5 files changed, 205 insertions, 10 deletions
diff --git a/kohana/libraries/Database.php b/kohana/libraries/Database.php
index ca4f6512..fd1ae3fa 100644
--- a/kohana/libraries/Database.php
+++ b/kohana/libraries/Database.php
@@ -621,6 +621,12 @@ class Database_Core {
if ($val != '')
{
+ // Add the table prefix if we are using table.column names
+ if(strpos($val, '.'))
+ {
+ $val = $this->config['table_prefix'].$val;
+ }
+
$this->groupby[] = $this->driver->escape_column($val);
}
}
@@ -674,11 +680,18 @@ class Database_Core {
{
$direction = strtoupper(trim($direction));
+ // Add a direction if the provided one isn't valid
if ( ! in_array($direction, array('ASC', 'DESC', 'RAND()', 'RANDOM()', 'NULL')))
{
$direction = 'ASC';
}
+ // Add the table prefix if a table.column was passed
+ if (strpos($column, '.'))
+ {
+ $column = $this->config['table_prefix'].$column;
+ }
+
$this->orderby[] = $this->driver->escape_column($column).' '.$direction;
}
diff --git a/kohana/libraries/ORM.php b/kohana/libraries/ORM.php
index a26fdcdd..4a76701b 100644
--- a/kohana/libraries/ORM.php
+++ b/kohana/libraries/ORM.php
@@ -1,5 +1,4 @@
-<?php
-
+<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* [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,
@@ -438,7 +437,15 @@ class ORM_Core {
*/
public function as_array()
{
- return $this->object;
+ $object = array();
+
+ foreach ($this->object as $key => $val)
+ {
+ // Reconstruct the array (calls __get)
+ $object[$key] = $this->$key;
+ }
+
+ return $object;
}
/**
@@ -450,9 +457,11 @@ class ORM_Core {
*/
public function with($object)
{
- // Don't join anything already joined
if (isset($this->with_applied[$object]))
+ {
+ // Don't join anything already joined
return $this;
+ }
$prefix = $table = $object;
@@ -815,11 +824,16 @@ class ORM_Core {
// Delete only given ids
$this->db->in($this->primary_key, $ids);
}
- else
+ elseif (is_null($ids))
{
// Delete all records
$this->db->where(TRUE);
}
+ else
+ {
+ // Do nothing - safeguard
+ return $this;
+ }
// Delete all objects
$this->db->delete($this->table_name);
@@ -1167,6 +1181,29 @@ class ORM_Core {
return $table;
}
+
+ /**
+ * Outputs ORM iterator joined with given model
+ * @param $related_model ORM Model the model related to this ORM
+ * @return ORM iterator
+ * @author credits to Josh Domagala
+ */
+ public function join_model($related_model)
+ {
+ if( !in_array( $related_model->table_name, $this->has_and_belongs_to_many ) )
+ {
+ return FALSE;
+ }
+
+ // Get the join table name
+ $join_table = $this->join_table($related_model->table_name);
+
+ // Return ORM iterator of model
+ return $this
+ ->join($join_table, $this->foreign_key(NULL, $join_table), $this->foreign_key(TRUE))
+ ->where($related_model->foreign_key(NULL, $join_table), $related_model->id)
+ ->find_all();
+ }
/**
* Returns an ORM model for the given object name;
@@ -1211,7 +1248,7 @@ class ORM_Core {
$this->object = $this->changed = $this->related = array();
// Set the loaded and saved object status based on the primary key
- $this->loaded = $this->saved = ($values[$this->primary_key] > 0);
+ $this->loaded = $this->saved = (bool) $values[$this->primary_key];
}
// Related objects
diff --git a/kohana/libraries/ORM_Versioned.php b/kohana/libraries/ORM_Versioned.php
new file mode 100644
index 00000000..7c3ee5d3
--- /dev/null
+++ b/kohana/libraries/ORM_Versioned.php
@@ -0,0 +1,143 @@
+<?php
+/**
+ * Object Relational Mapping (ORM) "versioned" extension. Allows ORM objects to
+ * be revisioned instead of updated.
+ *
+ * $Id$
+ *
+ * @package ORM
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class ORM_Versioned_Core extends ORM {
+
+ protected $last_version = NULL;
+
+ /**
+ * Overload ORM::save() to support versioned data
+ *
+ * @chainable
+ * @return ORM
+ */
+ public function save()
+ {
+ $this->last_version = 1 + ($this->last_version === NULL ? $this->object['version'] : $this->last_version);
+ $this->__set('version', $this->last_version);
+
+ parent::save();
+
+ if ($this->saved)
+ {
+ $data = array();
+ foreach ($this->object as $key => $value)
+ {
+ if ($key === 'id')
+ continue;
+
+ $data[$key] = $value;
+ }
+ $data[$this->foreign_key()] = $this->id;
+
+ $this->db->insert($this->table_name.'_versions', $data);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Loads previous version from current object
+ *
+ * @chainable
+ * @return ORM
+ */
+ public function previous()
+ {
+ if ( ! $this->loaded)
+ return $this;
+
+ $this->last_version = ($this->last_version === NULL) ? $this->object['version'] : $this->last_version;
+ $version = $this->last_version - 1;
+
+ $query = $this->db
+ ->where($this->foreign_key(), $this->object[$this->primary_key])
+ ->where('version', $version)
+ ->limit(1)
+ ->get($this->table_name.'_versions');
+
+ if ($query->count())
+ {
+ $this->load_values($query->result(FALSE)->current());
+ }
+
+ return $this;
+ }
+
+ /**
+ * Restores the object with data from stored version
+ *
+ * @param integer version number you want to restore
+ * @return ORM
+ */
+ public function restore($version)
+ {
+ if ( ! $this->loaded)
+ return $this;
+
+ $query = $this->db
+ ->where($this->foreign_key(), $this->object[$this->primary_key])
+ ->where('version', $version)
+ ->limit(1)
+ ->get($this->table_name.'_versions');
+
+ if ($query->count())
+ {
+ $row = $query->result(FALSE)->current();
+
+ foreach ($row as $key => $value)
+ {
+ if ($key === $this->primary_key OR $key === $this->foreign_key())
+ {
+ // Do not overwrite the primary key
+ continue;
+ }
+
+ if ($key === 'version')
+ {
+ // Always use the current version
+ $value = $this->version;
+ }
+
+ $this->__set($key, $value);
+ }
+
+ $this->save();
+ }
+
+ return $this;
+ }
+
+ /**
+ * Overloads ORM::delete() to delete all versioned entries of current object
+ * and the object itself
+ *
+ * @param integer id of the object you want to delete
+ * @return ORM
+ */
+ public function delete($id = NULL)
+ {
+ if ($id === NULL)
+ {
+ // Use the current object id
+ $id = $this->object[$this->primary_key];
+ }
+
+ if ($status = parent::delete($id))
+ {
+ $this->db->where($this->foreign_key(), $id)->delete($this->table_name.'_versions');
+ }
+
+ return $status;
+ }
+
+} \ No newline at end of file
diff --git a/kohana/libraries/drivers/Database/Mssql.php b/kohana/libraries/drivers/Database/Mssql.php
index 644d4a13..401770a2 100644
--- a/kohana/libraries/drivers/Database/Mssql.php
+++ b/kohana/libraries/drivers/Database/Mssql.php
@@ -58,7 +58,9 @@ class Database_Mssql_Driver extends Database_Driver
// Build the connection info
$host = isset($host) ? $host : $socket;
- $port = (isset($port) AND is_string($port)) ? ':'.$port : '';
+
+ // Windows uses a comma instead of a colon
+ $port = (isset($port) AND is_string($port)) ? (KOHANA_IS_WIN ? ',' : ':').$port : '';
// Make the connection and select the database
if (($this->link = $connect($host.$port, $user, $pass, TRUE)) AND mssql_select_db($database, $this->link))
diff --git a/kohana/libraries/drivers/Image/GraphicsMagick.php b/kohana/libraries/drivers/Image/GraphicsMagick.php
index a1392c23..8840eb80 100644
--- a/kohana/libraries/drivers/Image/GraphicsMagick.php
+++ b/kohana/libraries/drivers/Image/GraphicsMagick.php
@@ -30,7 +30,7 @@ class Image_GraphicsMagick_Driver extends Image_Driver {
if (empty($config['directory']))
{
// Attempt to locate GM by using "which" (only works for *nix!)
- if ( ! is_file($path = exec('which gmdisplay')))
+ if ( ! is_file($path = exec('which gm')))
throw new Kohana_Exception('image.graphicsmagick.not_found');
$config['directory'] = dirname($path);
@@ -40,8 +40,8 @@ class Image_GraphicsMagick_Driver extends Image_Driver {
$this->ext = (PHP_SHLIB_SUFFIX === 'dll') ? '.exe' : '';
// Check to make sure the provided path is correct
- if ( ! is_file(realpath($config['directory']).'/gmdisplay'.$this->ext))
- throw new Kohana_Exception('image.graphicsmagick.not_found', 'gmdisplay'.$this->ext);
+ if ( ! is_file(realpath($config['directory']).'/gm'.$this->ext))
+ throw new Kohana_Exception('image.graphicsmagick.not_found', 'gm'.$this->ext);
// Set the installation directory