update idiorm
This commit is contained in:
parent
cefc6c0365
commit
27a2938534
170
system/orm.php
170
system/orm.php
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
* Idiorm
|
||||
*
|
||||
@ -36,9 +36,83 @@
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*
|
||||
* The methods documented below are magic methods that conform to PSR-1.
|
||||
* This documentation exposes these methods to doc generators and IDEs.
|
||||
* @see http://www.php-fig.org/psr/psr-1/
|
||||
*
|
||||
* @method static array|string getConfig($key = null, $connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method static null resetConfig()
|
||||
* @method static \ORM forTable($table_name, $connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method static null setDb($db, $connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method static null resetDb()
|
||||
* @method static null setupLimitClauseStyle($connection_name)
|
||||
* @method static \PDO getDb($connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method static bool rawExecute($query, $parameters = array())
|
||||
* @method static \PDOStatement getLastStatement()
|
||||
* @method static string getLastQuery($connection_name = null)
|
||||
* @method static array getQueryLog($connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method array getConnectionNames()
|
||||
* @method $this useIdColumn($id_column)
|
||||
* @method \ORM|bool findOne($id=null)
|
||||
* @method array|\IdiormResultSet findMany()
|
||||
* @method \IdiormResultSet findResultSet()
|
||||
* @method array findArray()
|
||||
* @method $this forceAllDirty()
|
||||
* @method $this rawQuery($query, $parameters = array())
|
||||
* @method $this tableAlias($alias)
|
||||
* @method int countNullIdColumns()
|
||||
* @method $this selectExpr($expr, $alias=null)
|
||||
* @method \ORM selectMany($values)
|
||||
* @method \ORM selectManyExpr($values)
|
||||
* @method $this rawJoin($table, $constraint, $table_alias, $parameters = array())
|
||||
* @method $this innerJoin($table, $constraint, $table_alias=null)
|
||||
* @method $this leftOuterJoin($table, $constraint, $table_alias=null)
|
||||
* @method $this rightOuterJoin($table, $constraint, $table_alias=null)
|
||||
* @method $this fullOuterJoin($table, $constraint, $table_alias=null)
|
||||
* @method $this whereEqual($column_name, $value=null)
|
||||
* @method $this whereNotEqual($column_name, $value=null)
|
||||
* @method $this whereIdIs($id)
|
||||
* @method $this whereAnyIs($values, $operator='=')
|
||||
* @method array|string whereIdIn($ids)
|
||||
* @method $this whereLike($column_name, $value=null)
|
||||
* @method $this whereNotLike($column_name, $value=null)
|
||||
* @method $this whereGt($column_name, $value=null)
|
||||
* @method $this whereLt($column_name, $value=null)
|
||||
* @method $this whereGte($column_name, $value=null)
|
||||
* @method $this whereLte($column_name, $value=null)
|
||||
* @method $this whereIn($column_name, $values)
|
||||
* @method $this whereNotIn($column_name, $values)
|
||||
* @method $this whereNull($column_name)
|
||||
* @method $this whereNotNull($column_name)
|
||||
* @method $this whereRaw($clause, $parameters=array())
|
||||
* @method $this orderByDesc($column_name)
|
||||
* @method $this orderByAsc($column_name)
|
||||
* @method $this orderByExpr($clause)
|
||||
* @method $this groupBy($column_name)
|
||||
* @method $this groupByExpr($expr)
|
||||
* @method $this havingEqual($column_name, $value=null)
|
||||
* @method $this havingNotEqual($column_name, $value=null)
|
||||
* @method $this havingIdIs($id)
|
||||
* @method $this havingLike($column_name, $value=null)
|
||||
* @method $this havingNotLike($column_name, $value=null)
|
||||
* @method $this havingGt($column_name, $value=null)
|
||||
* @method $this havingLt($column_name, $value=null)
|
||||
* @method $this havingGte($column_name, $value=null)
|
||||
* @method $this havingLte($column_name, $value=null)
|
||||
* @method $this havingIn($column_name, $values=null)
|
||||
* @method $this havingNotIn($column_name, $values=null)
|
||||
* @method $this havingNull($column_name)
|
||||
* @method $this havingNotNull($column_name)
|
||||
* @method $this havingRaw($clause, $parameters=array())
|
||||
* @method static this clearCache($table_name = null, $connection_name = self::DEFAULT_CONNECTION)
|
||||
* @method array asArray()
|
||||
* @method bool setExpr($key, $value = null)
|
||||
* @method bool isDirty($key)
|
||||
* @method bool isNew()
|
||||
*/
|
||||
|
||||
class ORM implements ArrayAccess {
|
||||
class ORM implements ArrayAccess {
|
||||
|
||||
// ----------------------- //
|
||||
// --- CLASS CONSTANTS --- //
|
||||
@ -179,7 +253,7 @@ class ORM implements ArrayAccess {
|
||||
* required to use Idiorm). If you have more than one setting
|
||||
* you wish to configure, another shortcut is to pass an array
|
||||
* of settings (and omit the second argument).
|
||||
* @param string $key
|
||||
* @param string|array $key
|
||||
* @param mixed $value
|
||||
* @param string $connection_name Which connection to use
|
||||
*/
|
||||
@ -238,12 +312,6 @@ class ORM implements ArrayAccess {
|
||||
return new self($table_name, array(), $connection_name);
|
||||
}
|
||||
|
||||
|
||||
public static function execute($query) {
|
||||
self::_setup_db(self::DEFAULT_CONNECTION);
|
||||
return self::_execute($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the database connection used by the class
|
||||
* @param string $connection_name Which connection to use
|
||||
@ -293,9 +361,11 @@ class ORM implements ArrayAccess {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all registered PDO objects in _db array.
|
||||
* Close and delete all registered PDO objects in _db array.
|
||||
*/
|
||||
public static function reset_db() {
|
||||
self::$_db = null;
|
||||
|
||||
self::$_db = array();
|
||||
}
|
||||
|
||||
@ -462,15 +532,14 @@ class ORM implements ArrayAccess {
|
||||
self::$_query_log[$connection_name] = array();
|
||||
}
|
||||
|
||||
// Strip out any non-integer indexes from the parameters
|
||||
foreach($parameters as $key => $value) {
|
||||
if (!is_int($key)) unset($parameters[$key]);
|
||||
}
|
||||
|
||||
if (count($parameters) > 0) {
|
||||
if (empty($parameters)) {
|
||||
$bound_query = $query;
|
||||
} else {
|
||||
// Escape the parameters
|
||||
$parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters);
|
||||
|
||||
if (array_values($parameters) === $parameters) {
|
||||
// ? placeholders
|
||||
// Avoid %format collision for vsprintf
|
||||
$query = str_replace("%", "%%", $query);
|
||||
|
||||
@ -484,8 +553,13 @@ class ORM implements ArrayAccess {
|
||||
// Replace the question marks in the query with the parameters
|
||||
$bound_query = vsprintf($query, $parameters);
|
||||
} else {
|
||||
// named placeholders
|
||||
foreach ($parameters as $key => $val) {
|
||||
$query = str_replace($key, $val, $query);
|
||||
}
|
||||
$bound_query = $query;
|
||||
}
|
||||
}
|
||||
|
||||
self::$_last_query = $bound_query;
|
||||
self::$_query_log[$connection_name][] = $bound_query;
|
||||
@ -1261,14 +1335,14 @@ class ORM implements ArrayAccess {
|
||||
$data = array();
|
||||
$query = array("((");
|
||||
$first = true;
|
||||
foreach ($values as $item) {
|
||||
foreach ($values as $value) {
|
||||
if ($first) {
|
||||
$first = false;
|
||||
} else {
|
||||
$query[] = ") OR (";
|
||||
}
|
||||
$firstsub = true;
|
||||
foreach($item as $key => $item) {
|
||||
foreach($value as $key => $item) {
|
||||
$op = is_string($operator) ? $operator : (isset($operator[$key]) ? $operator[$key] : '=');
|
||||
if ($firstsub) {
|
||||
$firstsub = false;
|
||||
@ -1281,7 +1355,7 @@ class ORM implements ArrayAccess {
|
||||
}
|
||||
}
|
||||
$query[] = "))";
|
||||
return $this->where_raw(join($query, ' '), $data);
|
||||
return $this->where_raw(join(' ', $query), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1475,7 +1549,7 @@ class ORM implements ArrayAccess {
|
||||
*/
|
||||
public function having_id_is($id) {
|
||||
return (is_array($this->_get_id_column_name())) ?
|
||||
$this->having($this->_get_compound_id_column_values($value)) :
|
||||
$this->having($this->_get_compound_id_column_values($id), null) :
|
||||
$this->having($this->_get_id_column_name(), $id);
|
||||
}
|
||||
|
||||
@ -1827,6 +1901,7 @@ class ORM implements ArrayAccess {
|
||||
$cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name);
|
||||
|
||||
if ($cached_result !== false) {
|
||||
$this->_reset_idiorm_state();
|
||||
return $cached_result;
|
||||
}
|
||||
}
|
||||
@ -1843,12 +1918,17 @@ class ORM implements ArrayAccess {
|
||||
self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name);
|
||||
}
|
||||
|
||||
// reset Idiorm after executing the query
|
||||
$this->_reset_idiorm_state();
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the Idiorm instance state
|
||||
*/
|
||||
private function _reset_idiorm_state() {
|
||||
$this->_values = array();
|
||||
$this->_result_columns = array('*');
|
||||
$this->_using_default_result_columns = true;
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1970,7 +2050,7 @@ class ORM implements ArrayAccess {
|
||||
* object was saved.
|
||||
*/
|
||||
public function is_dirty($key) {
|
||||
return isset($this->_dirty_fields[$key]);
|
||||
return array_key_exists($key, $this->_dirty_fields);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2029,7 +2109,7 @@ class ORM implements ArrayAccess {
|
||||
// if the primary key is compound, assign the last inserted id
|
||||
// to the first column
|
||||
if (is_array($column)) {
|
||||
$column = array_slice($column, 0, 1);
|
||||
$column = reset($column);
|
||||
}
|
||||
$this->_data[$column] = $db->lastInsertId();
|
||||
}
|
||||
@ -2129,14 +2209,17 @@ class ORM implements ArrayAccess {
|
||||
// --- ArrayAccess --- //
|
||||
// --------------------- //
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($key) {
|
||||
return array_key_exists($key, $this->_data);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($key) {
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($key, $value) {
|
||||
if(is_null($key)) {
|
||||
throw new InvalidArgumentException('You must specify a key/array index.');
|
||||
@ -2144,6 +2227,7 @@ class ORM implements ArrayAccess {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($key) {
|
||||
unset($this->_data[$key]);
|
||||
unset($this->_dirty_fields[$key]);
|
||||
@ -2210,9 +2294,9 @@ class ORM implements ArrayAccess {
|
||||
|
||||
return call_user_func_array(array('ORM', $method), $arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* A class to handle str_replace operations that involve quoted strings
|
||||
* @example IdiormString::str_replace_outside_quotes('?', '%s', 'columnA = "Hello?" AND columnB = ?');
|
||||
* @example IdiormString::value('columnA = "Hello?" AND columnB = ?')->replace_outside_quotes('?', '%s');
|
||||
@ -2220,7 +2304,7 @@ class ORM implements ArrayAccess {
|
||||
* @author Simon Holywell <treffynnon@php.net>
|
||||
* @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer
|
||||
*/
|
||||
class IdiormString {
|
||||
class IdiormString {
|
||||
protected $subject;
|
||||
protected $search;
|
||||
protected $replace;
|
||||
@ -2314,13 +2398,15 @@ class IdiormString {
|
||||
return preg_replace('/'. preg_quote($this->search, '/') .'/',
|
||||
$this->replace, $matches[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* A result set class for working with collections of model instances
|
||||
* @author Simon Holywell <treffynnon@php.net>
|
||||
* @method null setResults(array $results)
|
||||
* @method array getResults()
|
||||
*/
|
||||
class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable {
|
||||
class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable {
|
||||
/**
|
||||
* The current result set as an array
|
||||
* @var array
|
||||
@ -2363,6 +2449,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* Get the number of records in the result set
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count() {
|
||||
return count($this->_results);
|
||||
}
|
||||
@ -2372,6 +2459,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* over the result set.
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->_results);
|
||||
}
|
||||
@ -2381,6 +2469,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* @param int|string $offset
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->_results[$offset]);
|
||||
}
|
||||
@ -2390,6 +2479,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* @param int|string $offset
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset) {
|
||||
return $this->_results[$offset];
|
||||
}
|
||||
@ -2399,6 +2489,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* @param int|string $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($offset, $value) {
|
||||
$this->_results[$offset] = $value;
|
||||
}
|
||||
@ -2407,10 +2498,19 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
* ArrayAccess
|
||||
* @param int|string $offset
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->_results[$offset]);
|
||||
}
|
||||
|
||||
public function __serialize() {
|
||||
return $this->serialize();
|
||||
}
|
||||
|
||||
public function __unserialize($data) {
|
||||
$this->unserialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializable
|
||||
* @return string
|
||||
@ -2447,11 +2547,11 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* A placeholder for exceptions eminating from the IdiormString class
|
||||
*/
|
||||
class IdiormStringException extends Exception {}
|
||||
class IdiormStringException extends Exception {}
|
||||
|
||||
class IdiormMethodMissingException extends Exception {}
|
||||
class IdiormMethodMissingException extends Exception {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user