2006-01-12 02:10:47 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-05-26 05:29:17 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2006-05-26 05:29:17 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.datasources
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* DboSource
|
|
|
|
*
|
|
|
|
* Creates DBO-descendant objects from a given db connection configuration
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.datasources
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
class DboSource extends DataSource {
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Description string for this Database Data Source.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $description = "Database Data Source";
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $__bypass = false;
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @var array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $__assocJoins = null;
|
2006-02-22 09:15:12 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $startQuote = null;
|
2006-02-22 09:15:12 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $endQuote = null;
|
2006-04-27 10:04:08 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $alias = 'AS ';
|
2006-04-27 10:04:08 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $goofyLimit = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2006-10-09 21:03:45 +00:00
|
|
|
function __construct($config = null, $autoConnect = true) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$this->debug = DEBUG > 0;
|
|
|
|
$this->fullDebug = DEBUG > 1;
|
|
|
|
parent::__construct($config);
|
2006-11-01 03:52:41 +00:00
|
|
|
|
2006-10-09 21:03:45 +00:00
|
|
|
if ($autoConnect) {
|
|
|
|
return $this->connect();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-04-24 16:24:57 +00:00
|
|
|
/**
|
|
|
|
* Reconnects to database server with optional new settings
|
|
|
|
*
|
|
|
|
* @param array $config An array defining the new configuration settings
|
|
|
|
* @return boolean True on success, false on failure
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function reconnect($config = null) {
|
|
|
|
$this->disconnect();
|
|
|
|
if ($config != null) {
|
|
|
|
$this->config = am($this->_baseConfig, $config);
|
|
|
|
}
|
|
|
|
return $this->connect();
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Prepares a value, or an array of values for database queries by quoting and escaping them.
|
|
|
|
*
|
|
|
|
* @param mixed $data A value or an array of values to prepare.
|
|
|
|
* @return mixed Prepared value or array of values.
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function value($data, $column = null) {
|
|
|
|
if (is_array($data)) {
|
|
|
|
$out = array();
|
|
|
|
$keys = array_keys($data);
|
|
|
|
$count = count($data);
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
$out[$keys[$i]] = $this->value($data[$keys[$i]]);
|
|
|
|
}
|
|
|
|
return $out;
|
2006-09-11 07:09:14 +00:00
|
|
|
} elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'))) {
|
|
|
|
return $data;
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-04-11 15:46:29 +00:00
|
|
|
/**
|
|
|
|
* Caches/returns cached results for child instances
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function listSources($data = null) {
|
|
|
|
if ($this->__sources != null) {
|
|
|
|
return $this->__sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG > 0) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$expires = "+30 seconds";
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
$expires = "+999 days";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data != null) {
|
|
|
|
$data = serialize($data);
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
$filename = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
|
2006-05-26 05:29:17 +00:00
|
|
|
$new = cache('models' . DS . $filename, $data, $expires);
|
|
|
|
|
|
|
|
if ($new != null) {
|
|
|
|
$new = unserialize($new);
|
|
|
|
$this->__sources = $new;
|
|
|
|
}
|
|
|
|
return $new;
|
|
|
|
}
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Convenience method for DboSource::listSources().
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function sources() {
|
|
|
|
$return = array_map('strtolower', $this->listSources());
|
|
|
|
return $return;
|
|
|
|
}
|
2006-04-11 15:46:29 +00:00
|
|
|
/**
|
2006-05-26 05:29:17 +00:00
|
|
|
* SQL Query abstraction
|
2006-04-11 15:46:29 +00:00
|
|
|
*
|
|
|
|
* @return resource Result resource identifier
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function query() {
|
|
|
|
$args = func_get_args();
|
|
|
|
$fields = null;
|
|
|
|
$order = null;
|
|
|
|
$limit = null;
|
|
|
|
$page = null;
|
|
|
|
$recursive = null;
|
2006-07-04 01:29:48 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (count($args) == 1) {
|
|
|
|
return $this->fetchAll($args[0]);
|
|
|
|
|
|
|
|
} elseif (count($args) > 1 && (strpos(low($args[0]), 'findby') === 0 || strpos(low($args[0]), 'findallby') === 0)) {
|
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
$params = $args[1];
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if (strpos(strtolower($args[0]), 'findby') === 0) {
|
|
|
|
$all = false;
|
|
|
|
$field = Inflector::underscore(preg_replace('/findBy/i', '', $args[0]));
|
|
|
|
} else {
|
|
|
|
$all = true;
|
|
|
|
$field = Inflector::underscore(preg_replace('/findAllBy/i', '', $args[0]));
|
2006-07-04 01:29:48 +00:00
|
|
|
}
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
$or = (strpos($field, '_or_') !== false);
|
|
|
|
if ($or) {
|
|
|
|
$field = explode('_or_', $field);
|
|
|
|
} else {
|
|
|
|
$field = explode('_and_', $field);
|
|
|
|
}
|
|
|
|
$off = count($field) - 1;
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
if (isset($params[1 + $off])) {
|
|
|
|
$fields = $params[1 + $off];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
if (isset($params[2 + $off])) {
|
|
|
|
$order = $params[2 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
$query = array();
|
|
|
|
foreach ($field as $f) {
|
|
|
|
$query[$args[2]->name . '.' . $f] = $params[$c++];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($or) {
|
|
|
|
$query = array('OR' => $query);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($all) {
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
if (isset($params[3 + $off])) {
|
|
|
|
$limit = $params[3 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[4 + $off])) {
|
|
|
|
$page = $params[4 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[5 + $off])) {
|
|
|
|
$recursive = $params[5 + $off];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
return $args[2]->findAll($query, $fields, $order, $limit, $page, $recursive);
|
|
|
|
} else {
|
2006-07-04 01:29:48 +00:00
|
|
|
if (isset($params[3 + $off])) {
|
|
|
|
$recursive = $params[3 + $off];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
return $args[2]->find($query, $fields, $order, $recursive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->fetchAll($args[0], false);
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function rawQuery($sql) {
|
|
|
|
$this->took = $this->error = $this->numRows = false;
|
|
|
|
return $this->execute($sql);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-01-12 03:56:59 +00:00
|
|
|
* Queries the database with given SQL statement, and obtains some metadata about the result
|
|
|
|
* (rows affected, timing, any errors, number of rows in resultset). The query is also logged.
|
2006-01-12 02:10:47 +00:00
|
|
|
* If DEBUG is set, the log is shown all the time, else it is only shown on errors.
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function execute($sql) {
|
|
|
|
$t = getMicrotime();
|
|
|
|
$this->_result = $this->_execute($sql);
|
|
|
|
$this->affected = $this->lastAffected();
|
|
|
|
$this->took = round((getMicrotime() - $t) * 1000, 0);
|
|
|
|
$this->error = $this->lastError();
|
|
|
|
$this->numRows = $this->lastNumRows($this->_result);
|
2006-11-01 03:52:41 +00:00
|
|
|
|
|
|
|
if($this->fullDebug) {
|
|
|
|
$this->logQuery($sql);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($this->error) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->_result;
|
|
|
|
}
|
|
|
|
}
|
2006-07-12 04:37:49 +00:00
|
|
|
/**
|
2006-10-25 13:27:32 +00:00
|
|
|
* Returns a row from current resultset as an array .
|
2006-07-12 04:37:49 +00:00
|
|
|
*
|
|
|
|
* @return array The fetched row as an array
|
|
|
|
*/
|
2006-10-25 13:27:32 +00:00
|
|
|
function fetchRow($sql = null) {
|
|
|
|
|
|
|
|
if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
|
|
|
|
if (!$this->execute($sql)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_resource($this->_result) || is_object($this->_result)) {
|
2006-07-12 04:37:49 +00:00
|
|
|
$this->resultSet($this->_result);
|
|
|
|
$resultRow = $this->fetchResult();
|
|
|
|
return $resultRow;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-10-25 13:27:32 +00:00
|
|
|
* @deprecated
|
|
|
|
* @see DboSource::fetchRow
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-10-25 13:27:32 +00:00
|
|
|
function fetchArray() {
|
2006-11-10 18:45:46 +00:00
|
|
|
trigger_error('Deprecated: Use DboSource::fetchRow() instead', E_USER_WARNING);
|
2006-10-25 13:27:32 +00:00
|
|
|
return $this->fetchRow();
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-10-25 13:27:32 +00:00
|
|
|
* @deprecated
|
|
|
|
* @see DboSource::fetchRow
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function one($sql) {
|
2006-11-10 18:45:46 +00:00
|
|
|
trigger_error('Deprecated: Use DboSource::fetchRow($sql) instead', E_USER_WARNING);
|
2006-10-25 13:27:32 +00:00
|
|
|
return $this->fetchRow($sql);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-01-12 03:56:59 +00:00
|
|
|
* Returns an array of all result rows for a given SQL query.
|
2006-01-12 02:10:47 +00:00
|
|
|
* Returns false if no rows matched.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
2006-03-16 05:33:47 +00:00
|
|
|
* @param boolean $cache Enables returning/storing cached query results
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return array Array of resultset rows, or false if no rows matched
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function fetchAll($sql, $cache = true, $modelName = null) {
|
|
|
|
if ($cache && isset($this->_queryCache[$sql])) {
|
|
|
|
if (strpos(trim(strtolower($sql)), 'select') !== false) {
|
|
|
|
return $this->_queryCache[$sql];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->execute($sql)) {
|
|
|
|
$out = array();
|
|
|
|
|
2006-10-25 13:27:32 +00:00
|
|
|
while($item = $this->fetchRow()) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$out[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cache) {
|
|
|
|
if (strpos(trim(strtolower($sql)), 'select') !== false) {
|
|
|
|
$this->_queryCache[$sql] = $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Returns a single field of the first of query results for a given SQL query, or false if empty.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the field
|
|
|
|
* @param string $sql SQL query
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function field($name, $sql) {
|
2006-10-25 13:27:32 +00:00
|
|
|
$data = $this->fetchRow($sql);
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if (empty($data[$name])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $data[$name];
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Checks if it's connected to the database
|
|
|
|
*
|
|
|
|
* @return boolean True if the database is connected, else false
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function isConnected() {
|
|
|
|
return $this->connected;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Outputs the contents of the queries log.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param boolean $sorted
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function showLog($sorted = false) {
|
|
|
|
if ($sorted) {
|
|
|
|
$log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
|
|
|
|
} else {
|
|
|
|
$log = $this->_queriesLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_queriesCnt > 1) {
|
2006-11-01 15:58:52 +00:00
|
|
|
$text = 'queries';
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2006-11-01 15:58:52 +00:00
|
|
|
$text = 'query';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-11-01 15:58:52 +00:00
|
|
|
|
|
|
|
if (php_sapi_name() != 'cli') {
|
|
|
|
print ("<table id=\"cakeSqlLog\" cellspacing=\"0\" border = \"0\">\n<caption>{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</caption>\n");
|
|
|
|
print ("<thead>\n<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>\n</thead>\n<tbody>\n");
|
|
|
|
|
|
|
|
foreach($log as $k => $i) {
|
2006-05-26 05:29:17 +00:00
|
|
|
print ("<tr><td>" . ($k + 1) . "</td><td>{$i['query']}</td><td>{$i['error']}</td><td style = \"text-align: right\">{$i['affected']}</td><td style = \"text-align: right\">{$i['numRows']}</td><td style = \"text-align: right\">{$i['took']}</td></tr>\n");
|
2006-11-01 15:58:52 +00:00
|
|
|
}
|
|
|
|
print ("</table>\n");
|
|
|
|
} else {
|
|
|
|
foreach($log as $k => $i) {
|
|
|
|
print (($k + 1) . ". {$i['query']} {$i['error']}\n");
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Log given SQL query.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
2006-05-26 05:29:17 +00:00
|
|
|
* @todo: Add hook to log errors instead of returning false
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function logQuery($sql) {
|
|
|
|
$this->_queriesCnt++;
|
|
|
|
$this->_queriesTime += $this->took;
|
|
|
|
$this->_queriesLog[] = array('query' => $sql,
|
|
|
|
'error' => $this->error,
|
|
|
|
'affected' => $this->affected,
|
|
|
|
'numRows' => $this->numRows,
|
|
|
|
'took' => $this->took
|
|
|
|
);
|
|
|
|
if (count($this->_queriesLog) > $this->_queriesLogMax) {
|
|
|
|
array_pop($this->_queriesLog);
|
|
|
|
}
|
|
|
|
if ($this->error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-01-12 03:56:59 +00:00
|
|
|
* Output information about an SQL query. The SQL statement, number of rows in resultset,
|
2006-02-01 13:26:23 +00:00
|
|
|
* and execution time in microseconds. If the query fails, an error is output instead.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param string $sql Query to show information on.
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function showQuery($sql) {
|
|
|
|
$error = $this->error;
|
|
|
|
if (strlen($sql) > 200 && !$this->fullDebug) {
|
|
|
|
$sql = substr($sql, 0, 200) . '[...]';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->debug || $error) {
|
|
|
|
print ("<p style = \"text-align:left\"><b>Query:</b> {$sql} <small>[Aff:{$this->affected} Num:{$this->numRows} Took:{$this->took}ms]</small>");
|
|
|
|
if ($error) {
|
|
|
|
print ("<br /><span style = \"color:Red;text-align:left\"><b>ERROR:</b> {$this->error}</span>");
|
|
|
|
}
|
|
|
|
print ('</p>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Gets full table name including prefix
|
|
|
|
*
|
|
|
|
* @param mixed $model
|
2006-06-19 16:15:49 +00:00
|
|
|
* @param boolean $quote
|
2006-05-26 05:29:17 +00:00
|
|
|
* @return string Full quoted table name
|
|
|
|
*/
|
2006-06-19 16:15:49 +00:00
|
|
|
function fullTableName($model, $quote = true) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (is_object($model)) {
|
|
|
|
$table = $model->table;
|
|
|
|
if ($model->tablePrefix != null && !empty($model->tablePrefix)) {
|
|
|
|
$table = $model->tablePrefix . $table;
|
|
|
|
}
|
|
|
|
} elseif (isset($this->config['prefix'])) {
|
|
|
|
$table = $this->config['prefix'] . strval($model);
|
|
|
|
} else {
|
|
|
|
$table = strval($model);
|
|
|
|
}
|
2006-06-19 16:15:49 +00:00
|
|
|
if ($quote) {
|
|
|
|
return $this->name($table);
|
|
|
|
}
|
|
|
|
return $table;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-03-15 20:45:59 +00:00
|
|
|
* The "C" in CRUD
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
2006-03-15 20:45:59 +00:00
|
|
|
* @param array $fields
|
|
|
|
* @param array $values
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return boolean Success
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function create(&$model, $fields = null, $values = null) {
|
|
|
|
$fieldInsert = array();
|
|
|
|
$valueInsert = array();
|
2006-10-25 13:27:32 +00:00
|
|
|
$id = null;
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($fields == null) {
|
|
|
|
unset($fields, $values);
|
|
|
|
$fields = array_keys($model->data);
|
|
|
|
$values = array_values($model->data);
|
|
|
|
}
|
|
|
|
|
2006-10-25 13:27:32 +00:00
|
|
|
$count = count($fields);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$fieldInsert[] = $this->name($fields[$i]);
|
|
|
|
if ($fields[$i] == $model->primaryKey) {
|
|
|
|
$id = $values[$i];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
2006-10-25 13:27:32 +00:00
|
|
|
$count = count($values);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$set = $this->value($values[$i], $model->getColumnType($fields[$i]));
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($set === "''") {
|
2006-10-25 13:27:32 +00:00
|
|
|
unset ($fieldInsert[$i]);
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
$valueInsert[] = $set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->execute('INSERT INTO ' . $this->fullTableName($model) . ' (' . join(',', $fieldInsert). ') VALUES (' . join(',', $valueInsert) . ')')) {
|
2006-11-10 18:45:46 +00:00
|
|
|
if (empty($id)) {
|
2006-10-25 13:27:32 +00:00
|
|
|
$id = $this->lastInsertId($this->fullTableName($model, false), $model->primaryKey);
|
|
|
|
}
|
|
|
|
$model->setInsertID($id);
|
|
|
|
$model->id = $id;
|
2006-05-26 05:29:17 +00:00
|
|
|
return true;
|
2006-07-22 14:13:07 +00:00
|
|
|
} else {
|
|
|
|
$model->onError();
|
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-03-12 00:11:40 +00:00
|
|
|
* The "R" in CRUD
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param array $queryData
|
|
|
|
* @param integer $recursive Number of levels of association
|
2006-01-17 17:52:23 +00:00
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function read(&$model, $queryData = array(), $recursive = null) {
|
|
|
|
|
|
|
|
$this->__scrubQueryData($queryData);
|
|
|
|
$null = null;
|
|
|
|
$array = array();
|
|
|
|
$linkedModels = array();
|
|
|
|
$this->__bypass = false;
|
|
|
|
$this->__assocJoins = null;
|
|
|
|
|
|
|
|
if (!is_null($recursive)) {
|
|
|
|
$_recursive = $model->recursive;
|
|
|
|
$model->recursive = $recursive;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($queryData['fields'])) {
|
|
|
|
$this->__bypass = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($model->__associations as $type) {
|
2006-06-14 18:02:37 +00:00
|
|
|
foreach($model->{$type} as $assoc => $assocData) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($model->recursive > -1) {
|
2006-09-14 21:59:51 +00:00
|
|
|
$linkModel =& $model->{$assoc};
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
|
|
|
|
if (true === $this->generateSelfAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null)) {
|
|
|
|
$linkedModels[] = $type . '/' . $assoc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($model->useDbConfig == $linkModel->useDbConfig) {
|
|
|
|
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null)) {
|
|
|
|
$linkedModels[] = $type . '/' . $assoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Build final query SQL
|
2006-06-14 18:02:37 +00:00
|
|
|
$query = $this->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null);
|
2006-05-26 05:29:17 +00:00
|
|
|
$resultSet = $this->fetchAll($query, $model->cacheQueries, $model->name);
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2006-07-22 14:13:07 +00:00
|
|
|
if ($resultSet === false) {
|
|
|
|
$model->onError();
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$filtered = $this->__filterResults($resultSet, $model);
|
|
|
|
|
|
|
|
if ($model->recursive > 0) {
|
|
|
|
foreach($model->__associations as $type) {
|
2006-06-14 18:02:37 +00:00
|
|
|
foreach($model->{$type} as $assoc => $assocData) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$db = null;
|
2006-09-14 21:59:51 +00:00
|
|
|
$linkModel =& $model->{$assoc};
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if (!in_array($type . '/' . $assoc, $linkedModels)) {
|
|
|
|
if ($model->useDbConfig == $linkModel->useDbConfig) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$db =& $this;
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2006-06-14 18:02:37 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($linkModel->useDbConfig);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
} elseif($model->recursive > 1 && ($type == 'belongsTo' || $type == 'hasOne')) {
|
|
|
|
// Do recursive joins on belongsTo and hasOne relationships
|
|
|
|
$db =& $this;
|
|
|
|
} else {
|
|
|
|
unset ($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($db) && $db != null) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$stack = array($assoc);
|
|
|
|
$db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1, $stack);
|
2006-05-26 05:29:17 +00:00
|
|
|
unset($db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->__filterResults($resultSet, $model, $filtered);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($recursive)) {
|
|
|
|
$model->recursive = $_recursive;
|
|
|
|
}
|
|
|
|
return $resultSet;
|
|
|
|
}
|
2006-04-24 16:24:57 +00:00
|
|
|
/**
|
2006-05-26 05:29:17 +00:00
|
|
|
* Private method. Passes association results thru afterFind filter of corresponding model
|
2006-04-24 16:24:57 +00:00
|
|
|
*
|
2006-04-27 10:04:08 +00:00
|
|
|
* @param unknown_type $results
|
|
|
|
* @param unknown_type $model
|
|
|
|
* @param unknown_type $filtered
|
|
|
|
* @return unknown
|
2006-04-24 16:24:57 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __filterResults(&$results, &$model, $filtered = array()) {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$filtering = array();
|
|
|
|
$associations = am($model->belongsTo, $model->hasOne, $model->hasMany, $model->hasAndBelongsToMany);
|
|
|
|
$count = count($results);
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
if (is_array($results[$i])) {
|
|
|
|
$keys = array_keys($results[$i]);
|
|
|
|
$count2 = count($keys);
|
|
|
|
|
|
|
|
for($j = 0; $j < $count2; $j++) {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$key = $keys[$j];
|
2006-06-14 18:02:37 +00:00
|
|
|
if (isset($associations[$key])) {
|
|
|
|
$className = $associations[$key]['className'];
|
|
|
|
} else {
|
|
|
|
$className = $key;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if ($model->name != $className && !in_array($key, $filtered)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!in_array($key, $filtering)) {
|
|
|
|
$filtering[] = $key;
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if (isset($model->{$className}) && is_object($model->{$className})) {
|
2006-08-18 08:00:54 +00:00
|
|
|
$data = $model->{$className}->afterFind(array(array($key => $results[$i][$key])), false);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
$results[$i][$key] = $data[0][$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $filtering;
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-01-17 17:52:23 +00:00
|
|
|
* Enter description here...
|
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $linkModel
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param string $type Association type
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $assocData
|
|
|
|
* @param unknown_type $queryData
|
|
|
|
* @param unknown_type $external
|
|
|
|
* @param unknown_type $resultSet
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param integer $recursive Number of levels of association
|
2006-06-14 18:02:37 +00:00
|
|
|
* @param array $stack
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
|
|
|
if ($query) {
|
|
|
|
|
|
|
|
if (!isset($resultSet) || !is_array($resultSet)) {
|
|
|
|
if (DEBUG) {
|
|
|
|
e('<div style = "font: Verdana bold 12px; color: #FF0000">SQL Error in model ' . $model->name . ': ');
|
|
|
|
if (isset($this->error) && $this->error != null) {
|
|
|
|
e($this->error);
|
|
|
|
}
|
|
|
|
e('</div>');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
$count = count($resultSet);
|
2006-05-26 05:29:17 +00:00
|
|
|
for($i = 0; $i < $count; $i++) {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$row =& $resultSet[$i];
|
2006-06-14 18:02:37 +00:00
|
|
|
$q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack);
|
2006-09-14 02:11:38 +00:00
|
|
|
if($q != false){
|
|
|
|
$fetch = $this->fetchAll($q, $model->cacheQueries, $model->name);
|
|
|
|
} else {
|
|
|
|
$fetch = null;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!empty($fetch) && is_array($fetch)) {
|
2006-09-14 02:11:38 +00:00
|
|
|
if ($recursive > 0) {
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
foreach($linkModel->__associations as $type1) {
|
2006-06-14 18:02:37 +00:00
|
|
|
foreach($linkModel->{$type1} as $assoc1 => $assocData1) {
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$deepModel =& $linkModel->{$assocData1['className']};
|
2006-06-24 21:33:51 +00:00
|
|
|
if ($deepModel->alias != $model->name) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$tmpStack = $stack;
|
|
|
|
$tmpStack[] = $assoc1;
|
2006-06-14 23:55:17 +00:00
|
|
|
if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
|
|
|
|
$db =& $this;
|
|
|
|
} else {
|
|
|
|
$db =& ConnectionManager::getDataSource($deepModel->useDbConfig);
|
|
|
|
}
|
|
|
|
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->__mergeAssociation($resultSet[$i], $fetch, $association, $type);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$tempArray[0][$association] = false;
|
|
|
|
$this->__mergeAssociation($resultSet[$i], $tempArray, $association, $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-27 10:04:08 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $data
|
|
|
|
* @param unknown_type $merge
|
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __mergeAssociation(&$data, $merge, $association, $type) {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (isset($merge[0]) && !isset($merge[0][$association])) {
|
|
|
|
$association = Inflector::pluralize($association);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type == 'belongsTo' || $type == 'hasOne') {
|
|
|
|
if (isset($merge[$association])) {
|
|
|
|
$data[$association] = $merge[$association][0];
|
|
|
|
} else {
|
|
|
|
if (count($merge[0][$association]) > 1) {
|
|
|
|
foreach($merge[0] as $assoc => $data2) {
|
|
|
|
if ($assoc != $association) {
|
|
|
|
$merge[0][$association][$assoc] = $data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-20 03:26:52 +00:00
|
|
|
if(!isset($data[$association])) {
|
|
|
|
$data[$association] = $merge[0][$association];
|
|
|
|
} else {
|
|
|
|
if(is_array($merge[0][$association])){
|
|
|
|
$data[$association] = array_merge($merge[0][$association], $data[$association]);
|
|
|
|
}
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($merge[0][$association] === false) {
|
2006-09-19 02:08:54 +00:00
|
|
|
if(!isset($data[$association])){
|
|
|
|
$data[$association] = array();
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
foreach($merge as $i => $row) {
|
|
|
|
if (count($row) == 1) {
|
|
|
|
$data[$association][] = $row[$association];
|
|
|
|
} else {
|
|
|
|
$tmp = array_merge($row[$association], $row);
|
|
|
|
unset($tmp[$association]);
|
|
|
|
$data[$association][] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-01-17 17:52:23 +00:00
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
|
|
|
* @param unknown_type $linkModel
|
|
|
|
* @param unknown_type $type
|
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $assocData
|
|
|
|
* @param unknown_type $queryData
|
|
|
|
* @param unknown_type $external
|
|
|
|
* @param unknown_type $resultSet
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function generateSelfAssociationQuery(&$model, &$linkModel, $type, $association = null, $assocData = array(), &$queryData, $external = false, &$resultSet) {
|
|
|
|
$alias = $association;
|
|
|
|
if (!isset($queryData['selfJoin'])) {
|
|
|
|
$queryData['selfJoin'] = array();
|
2006-09-19 15:24:15 +00:00
|
|
|
$sql = 'SELECT ' . join(', ', $this->fields($model, $model->name, $queryData['fields']));
|
|
|
|
if($this->__bypass === false){
|
|
|
|
$sql .= ', ';
|
|
|
|
$sql .= join(', ', $this->fields($linkModel, $alias, ''));
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
$sql .= ' FROM ' . $this->fullTableName($model) . ' ' . $this->alias . $this->name($model->name);
|
|
|
|
$sql .= ' LEFT JOIN ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
|
|
|
|
$sql .= ' ON ' . $this->name($model->name) . '.' . $this->name($assocData['foreignKey']);
|
|
|
|
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
|
|
|
|
|
|
|
|
if (!in_array($sql, $queryData['selfJoin'])) {
|
|
|
|
$queryData['selfJoin'][] = $sql;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} elseif (isset($linkModel)) {
|
|
|
|
return $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
|
|
|
} else {
|
|
|
|
if (isset($this->__assocJoins)) {
|
|
|
|
$replace = ', ';
|
|
|
|
$replace .= join(', ', $this->__assocJoins['fields']);
|
|
|
|
$replace .= ' FROM';
|
|
|
|
} else {
|
|
|
|
$replace = 'FROM';
|
|
|
|
}
|
|
|
|
$sql = $queryData['selfJoin'][0];
|
|
|
|
$sql .= ' ' . join(' ', $queryData['joins']);
|
|
|
|
$sql .= $this->conditions($queryData['conditions']) . ' ' . $this->order($queryData['order']);
|
2006-06-14 18:02:37 +00:00
|
|
|
$sql .= ' ' . $this->limit($queryData['limit'], $queryData['offset']);
|
2006-05-26 05:29:17 +00:00
|
|
|
$result = preg_replace('/FROM/', $replace, $sql);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-01-17 17:52:23 +00:00
|
|
|
* Enter description here...
|
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $linkModel
|
|
|
|
* @param unknown_type $type
|
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $assocData
|
|
|
|
* @param unknown_type $queryData
|
|
|
|
* @param unknown_type $external
|
|
|
|
* @param unknown_type $resultSet
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function generateAssociationQuery(&$model, &$linkModel, $type, $association = null, $assocData = array(), &$queryData, $external = false, &$resultSet) {
|
|
|
|
$this->__scrubQueryData($queryData);
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->__scrubQueryData($assocData);
|
2006-05-26 05:29:17 +00:00
|
|
|
$joinedOnSelf = false;
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($linkModel == null) {
|
|
|
|
if (array_key_exists('selfJoin', $queryData)) {
|
|
|
|
return $this->generateSelfAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
|
|
|
} else {
|
|
|
|
if (isset($this->__assocJoins)) {
|
|
|
|
$joinFields = ', ';
|
|
|
|
$joinFields .= join(', ', $this->__assocJoins['fields']);
|
|
|
|
} else {
|
|
|
|
$joinFields = null;
|
|
|
|
}
|
|
|
|
|
2006-11-01 19:19:11 +00:00
|
|
|
$sql = 'SELECT ';
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($this->goofyLimit) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$sql .= $this->limit($queryData['limit'], $queryData['offset']);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
$sql .= ' ' . join(', ', $this->fields($model, $model->name, $queryData['fields'])) . $joinFields . ' FROM ';
|
|
|
|
$sql .= $this->fullTableName($model) . ' ' . $this->alias;
|
|
|
|
$sql .= $this->name($model->name) . ' ' . join(' ', $queryData['joins']) . ' ';
|
|
|
|
$sql .= $this->conditions($queryData['conditions']) . ' ' . $this->order($queryData['order']);
|
|
|
|
|
|
|
|
if (!$this->goofyLimit) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$sql .= ' ' . $this->limit($queryData['limit'], $queryData['offset']);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
$alias = $association;
|
|
|
|
|
|
|
|
if ($model->name == $linkModel->name) {
|
|
|
|
$joinedOnSelf = true;
|
|
|
|
}
|
2006-10-25 13:27:32 +00:00
|
|
|
if (is_string($queryData['conditions'])) {
|
|
|
|
$queryData['conditions'] = array($queryData['conditions']);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
switch($type) {
|
|
|
|
case 'hasOne':
|
2006-06-15 14:39:34 +00:00
|
|
|
if ($external || isset($assocData['external'])) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (isset($assocData['finderQuery'])) {
|
|
|
|
return $assocData['finderQuery'];
|
|
|
|
}
|
|
|
|
|
2006-07-22 16:45:18 +00:00
|
|
|
if (!isset($assocData['fields'])) {
|
|
|
|
$assocData['fields'] = '';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-07-22 16:45:18 +00:00
|
|
|
|
|
|
|
$limit = '';
|
|
|
|
|
|
|
|
if (isset($queryData['limit']) && !empty($queryData['limit'])) {
|
|
|
|
$limit = $this->limit($queryData['limit'], $queryData['offset']);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql = 'SELECT ';
|
|
|
|
if ($this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' ' . join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias) . ' ';
|
|
|
|
|
|
|
|
$conditions = $queryData['conditions'];
|
|
|
|
$condition = $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
|
|
|
|
$condition .= ' = {$__cakeForeignKey__$}';
|
|
|
|
|
|
|
|
if (is_array($conditions)) {
|
|
|
|
$conditions[] = $condition;
|
|
|
|
} else {
|
|
|
|
$cond = $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
|
|
|
|
$cond .= ' = {$__cakeID__$}';
|
|
|
|
|
|
|
|
if (trim($conditions) != '') {
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$conditions .= $cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $this->conditions($conditions) . $this->order($queryData['order']);
|
|
|
|
if (!$this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
|
|
|
|
} else {
|
2006-07-22 16:45:18 +00:00
|
|
|
if (!isset($assocData['fields'])) {
|
|
|
|
$assocData['fields'] = '';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($this->__bypass === false) {
|
|
|
|
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$this->__assocJoins['fields'][] = $fields;
|
|
|
|
} else {
|
|
|
|
$this->__assocJoins = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = ' LEFT JOIN ' . $this->fullTableName($linkModel);
|
|
|
|
$sql .= ' ' . $this->alias . $this->name($alias) . ' ON ' . $this->name($alias) . '.';
|
|
|
|
$sql .= $this->name($assocData['foreignKey']) . ' = ' . $model->escapeField($model->primaryKey);
|
|
|
|
|
|
|
|
if ($assocData['order'] != null) {
|
|
|
|
$queryData['order'][] = $assocData['order'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($assocData['conditions']) && !empty($assocData['conditions'])) {
|
|
|
|
if (is_array($queryData['conditions'])) {
|
|
|
|
$queryData['conditions'] = array_merge($assocData['conditions'], $queryData['conditions']);
|
|
|
|
} else {
|
|
|
|
$queryData['conditions'] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($sql, $queryData['joins'])) {
|
|
|
|
$queryData['joins'][] = $sql;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'belongsTo':
|
2006-06-15 14:39:34 +00:00
|
|
|
if ($external || isset($assocData['external'])) {
|
2006-07-22 16:45:18 +00:00
|
|
|
$limit = '';
|
|
|
|
if (isset($assocData['limit'])) {
|
|
|
|
$limit = $this->limit($assocData['limit'], $queryData['offset']);
|
2006-07-22 15:05:01 +00:00
|
|
|
}
|
2006-07-22 16:45:18 +00:00
|
|
|
|
|
|
|
if (!isset($assocData['fields'])) {
|
|
|
|
$assocData['fields'] = '';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql = 'SELECT ';
|
|
|
|
if ($this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' ' . join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias) . ' ';
|
|
|
|
|
|
|
|
$conditions = $assocData['conditions'];
|
|
|
|
$condition = $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
|
|
|
|
$condition .= ' = {$__cakeForeignKey__$}';
|
|
|
|
|
|
|
|
if (is_array($conditions)) {
|
|
|
|
$conditions[] = $condition;
|
|
|
|
} else {
|
|
|
|
if (trim($conditions) != '') {
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
$conditions .= $condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $this->conditions($conditions) . $this->order($assocData['order']);
|
|
|
|
|
|
|
|
if (!$this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
|
|
|
|
} else {
|
2006-07-22 16:45:18 +00:00
|
|
|
if (!isset($assocData['fields'])) {
|
|
|
|
$assocData['fields'] = '';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($this->__bypass === false) {
|
|
|
|
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$this->__assocJoins['fields'][] = $fields;
|
|
|
|
} else {
|
|
|
|
$this->__assocJoins = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = ' LEFT JOIN ' . $this->fullTableName($linkModel);
|
|
|
|
$sql .= ' ' . $this->alias . $this->name($alias) . ' ON ';
|
|
|
|
$sql .= $this->name($model->name) . '.' . $this->name($assocData['foreignKey']);
|
|
|
|
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
|
|
|
|
|
|
|
|
if (isset($assocData['conditions']) && !empty($assocData['conditions'])) {
|
|
|
|
if (is_array($queryData['conditions'])) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$queryData['conditions'] = array_merge((array)$assocData['conditions'], $queryData['conditions']);
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2006-06-14 18:02:37 +00:00
|
|
|
if (!empty($queryData['conditions'])){
|
|
|
|
$queryData['conditions'] = array($queryData['conditions']);
|
|
|
|
if (is_array($assocData['conditions'])){
|
|
|
|
array_merge($queryData['conditions'],$assocData['conditions']);
|
|
|
|
} else {
|
|
|
|
$queryData['conditions'][] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$queryData['conditions'] = $assocData['conditions'];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($sql, $queryData['joins'])) {
|
|
|
|
$queryData['joins'][] = $sql;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'hasMany':
|
|
|
|
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
|
|
|
|
$sql = $assocData['finderQuery'];
|
|
|
|
} else {
|
|
|
|
|
2006-07-22 16:45:18 +00:00
|
|
|
$limit = '';
|
|
|
|
if (isset($assocData['limit'])) {
|
2006-08-27 16:47:05 +00:00
|
|
|
if (!isset($assocData['offset']) && isset($assocData['page'])) {
|
|
|
|
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
|
|
|
|
} elseif (!isset($assocData['offset'])) {
|
|
|
|
$assocData['offset'] = null;
|
|
|
|
}
|
|
|
|
$limit = $this->limit($assocData['limit'], $assocData['offset']);
|
2006-07-22 16:45:18 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
$conditions = $assocData['conditions'];
|
|
|
|
$sql = 'SELECT ';
|
|
|
|
|
|
|
|
if ($this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' ' . join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
|
|
|
|
|
|
|
|
if (is_array($conditions)) {
|
|
|
|
$conditions[$alias . '.' . $assocData['foreignKey']] = '{$__cakeID__$}';
|
|
|
|
} else {
|
|
|
|
$cond = $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
|
|
|
|
$cond .= ' = {$__cakeID__$}';
|
|
|
|
if (trim($conditions) != '') {
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
$conditions .= $cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $this->conditions($conditions);
|
|
|
|
$sql .= $this->order($assocData['order']);
|
|
|
|
|
|
|
|
if (!$this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
break;
|
|
|
|
case 'hasAndBelongsToMany':
|
|
|
|
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
|
|
|
|
$sql = $assocData['finderQuery'];
|
|
|
|
} else {
|
|
|
|
$joinTbl = $this->fullTableName($assocData['joinTable']);
|
|
|
|
|
2006-07-22 16:45:18 +00:00
|
|
|
$limit = '';
|
|
|
|
if (isset($assocData['limit'])) {
|
2006-08-27 16:47:05 +00:00
|
|
|
if (!isset($assocData['offset']) && isset($assocData['page'])) {
|
|
|
|
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
|
|
|
|
} elseif (!isset($assocData['offset'])) {
|
|
|
|
$assocData['offset'] = null;
|
|
|
|
}
|
|
|
|
$limit = $this->limit($assocData['limit'], $assocData['offset']);
|
2006-07-22 16:45:18 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
$sql = 'SELECT ';
|
|
|
|
|
|
|
|
if ($this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
|
2006-07-04 01:29:48 +00:00
|
|
|
$joinFields = array();
|
2006-10-09 21:03:45 +00:00
|
|
|
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
|
2006-07-04 01:29:48 +00:00
|
|
|
$joinName = array_keys($assocData['with']);
|
|
|
|
$joinFields = $assocData['with'][$joinName[0]];
|
|
|
|
|
|
|
|
if (is_array($joinFields) && !empty($joinFields)) {
|
|
|
|
$joinFields = $this->fields($linkModel, $joinName[0], $joinFields);
|
|
|
|
} else {
|
|
|
|
$joinFields = array($this->name($joinName[0]) . '.*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' ' . join(', ', am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields));
|
2006-05-26 05:29:17 +00:00
|
|
|
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
|
2006-07-04 01:29:48 +00:00
|
|
|
$sql .= ' JOIN ' . $joinTbl;
|
|
|
|
|
|
|
|
$joinAssoc = $joinTbl;
|
2006-10-09 21:03:45 +00:00
|
|
|
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
|
2006-07-04 01:29:48 +00:00
|
|
|
$joinAssoc = $joinName[0];
|
|
|
|
$sql .= $this->alias . $this->name($joinAssoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' ON ' . $this->name($joinAssoc);
|
2006-05-26 05:29:17 +00:00
|
|
|
$sql .= '.' . $this->name($assocData['foreignKey']) . ' = {$__cakeID__$}';
|
2006-07-04 01:29:48 +00:00
|
|
|
$sql .= ' AND ' . $this->name($joinAssoc) . '.' . $this->name($assocData['associationForeignKey']);
|
2006-05-26 05:29:17 +00:00
|
|
|
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
|
|
|
|
|
|
|
|
$sql .= $this->conditions($assocData['conditions']);
|
|
|
|
$sql .= $this->order($assocData['order']);
|
|
|
|
|
|
|
|
if (!$this->goofyLimit) {
|
|
|
|
$sql .= $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-11-01 19:27:08 +00:00
|
|
|
/**
|
|
|
|
* Private method
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function __mergeConditions(&$queryData, $assocData) {
|
|
|
|
if (isset($assocData['conditions']) && !empty($assocData['conditions'])) {
|
|
|
|
if (is_array($queryData['conditions'])) {
|
|
|
|
$queryData['conditions'] = array_merge((array)$assocData['conditions'], $queryData['conditions']);
|
|
|
|
} else {
|
|
|
|
if (!empty($queryData['conditions'])) {
|
|
|
|
$queryData['conditions'] = array($queryData['conditions']);
|
|
|
|
if (is_array($assocData['conditions'])) {
|
|
|
|
$queryData['conditions'] = array_merge($queryData['conditions'], $assocData['conditions']);
|
|
|
|
} else {
|
|
|
|
$queryData['conditions'][] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$queryData['conditions'] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Generates and executes an SQL UPDATE statement for given model, fields, and values.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $values
|
2006-10-16 20:02:34 +00:00
|
|
|
* @param mixed $conditions
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-10-18 15:38:37 +00:00
|
|
|
function update(&$model, $fields = array(), $values = null, $conditions = null) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$updates = array();
|
2006-10-18 15:38:37 +00:00
|
|
|
if ($values == null) {
|
|
|
|
$combined = $fields;
|
|
|
|
} else {
|
|
|
|
$combined = array_combine($fields, $values);
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
foreach($combined as $field => $value) {
|
2006-06-14 18:02:37 +00:00
|
|
|
if ($value === null) {
|
|
|
|
$updates[] = $this->name($field) . ' = NULL';
|
|
|
|
} else {
|
2006-10-18 15:38:37 +00:00
|
|
|
$update = $this->name($field) . ' = ';
|
|
|
|
if ($conditions == null) {
|
|
|
|
$update .= $this->value($value, $model->getColumnType($field));
|
|
|
|
} else {
|
|
|
|
$update .= $value;
|
|
|
|
}
|
|
|
|
$updates[] = $update;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
$sql = 'UPDATE ' . $this->fullTableName($model);
|
|
|
|
$sql .= ' SET ' . join(',', $updates);
|
2006-10-18 15:38:37 +00:00
|
|
|
|
|
|
|
if ($conditions == null) {
|
|
|
|
$sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey));
|
|
|
|
} else {
|
|
|
|
$sql .= $this->conditions($conditions);
|
|
|
|
}
|
2006-07-22 14:13:07 +00:00
|
|
|
if (!$this->execute($sql)) {
|
|
|
|
$model->onError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Generates and executes an SQL DELETE statement for given id on given model.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param mixed $id Primary key id number to remove.
|
2006-10-16 20:02:34 +00:00
|
|
|
* @param mixed $conditions
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return boolean Success
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-10-16 20:02:34 +00:00
|
|
|
function delete(&$model, $id = null, $conditions = null) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$_id = $model->id;
|
|
|
|
if ($id != null) {
|
|
|
|
$model->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($model->id)) {
|
|
|
|
$model->id = array($model->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($model->id as $id) {
|
|
|
|
$result = $this->execute('DELETE FROM ' . $this->fullTableName($model) . ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($id));
|
2006-07-22 14:13:07 +00:00
|
|
|
if ($result === false) {
|
|
|
|
$model->onError();
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-07-22 14:13:07 +00:00
|
|
|
return ($result !== false);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Returns a key formatted like a string Model.fieldname(i.e. Post.title, or Country.name)
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
|
|
|
* @param unknown_type $key
|
|
|
|
* @param unknown_type $assoc
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return string
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function resolveKey($model, $key, $assoc = null) {
|
|
|
|
if ($assoc == null) {
|
|
|
|
$assoc = $model->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strpos('.', $key)) {
|
|
|
|
return $this->name($model->name) . '.' . $this->name($key);
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-03-15 20:45:59 +00:00
|
|
|
* Returns the column type of a given
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-03-15 20:45:59 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param string $field
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function getColumnType(&$model, $field) {
|
|
|
|
return $model->getColumnType($field);
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Private helper method to remove query metadata in given data array.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param array $data
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __scrubQueryData(&$data) {
|
|
|
|
if (!isset($data['conditions'])) {
|
|
|
|
$data['conditions'] = ' 1 = 1 ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['fields'])) {
|
|
|
|
$data['fields'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['joins'])) {
|
|
|
|
$data['joins'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['order'])) {
|
|
|
|
$data['order'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['limit'])) {
|
|
|
|
$data['limit'] = '';
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
|
|
|
|
if (!isset($data['offset'])) {
|
|
|
|
$data['offset'] = null;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Generates the fields list of an SQL query.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param string $alias Alias tablename
|
|
|
|
* @param mixed $fields
|
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function fields(&$model, $alias, $fields) {
|
2006-09-21 03:56:57 +00:00
|
|
|
$resultMatch = null;
|
|
|
|
$build = true;
|
2006-05-26 05:29:17 +00:00
|
|
|
if (is_array($fields)) {
|
|
|
|
$fields = $fields;
|
|
|
|
} else {
|
|
|
|
if ($fields != null) {
|
2006-09-21 03:56:57 +00:00
|
|
|
preg_match_all('/(\\w*\\([\\s\\S]*?\\)[\.,\\s\\w]*?\\))([\\s\\S]*)/', $fields, $result, PREG_PATTERN_ORDER);
|
|
|
|
|
|
|
|
if(isset($result[1][0])){
|
|
|
|
$resultMatch = $result[1][0];
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-09-21 03:56:57 +00:00
|
|
|
if(isset($result[2][0])){
|
|
|
|
$fields = $result[2][0];
|
|
|
|
|
|
|
|
if (preg_match('/AS/i', $fields)) {
|
|
|
|
$build = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($build === true){
|
|
|
|
if (strpos($fields, ',')) {
|
|
|
|
$fields = explode(',', $fields);
|
|
|
|
} else {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$fields = array_map('trim', $fields);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
foreach($model->_tableInfo->value as $field) {
|
|
|
|
$fields[] = $field['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-21 03:56:57 +00:00
|
|
|
if($build === true){
|
|
|
|
$count = count($fields);
|
2006-09-19 15:17:17 +00:00
|
|
|
|
2006-09-21 03:56:57 +00:00
|
|
|
if ($count >= 1 && $fields[0] != '*') {
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
|
|
|
|
$prepend = '';
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-09-21 03:56:57 +00:00
|
|
|
if (strpos($fields[$i], 'DISTINCT') !== false) {
|
|
|
|
$prepend = 'DISTINCT ';
|
|
|
|
$fields[$i] = trim(r('DISTINCT', '', $fields[$i]));
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-09-21 03:56:57 +00:00
|
|
|
$dot = strrpos($fields[$i], '.');
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-09-21 03:56:57 +00:00
|
|
|
if ($dot === false) {
|
|
|
|
$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]);
|
|
|
|
} else {
|
|
|
|
$build = explode('.', $fields[$i]);
|
|
|
|
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-21 03:56:57 +00:00
|
|
|
|
|
|
|
if($resultMatch != null){
|
|
|
|
if(is_string($fields)) {
|
|
|
|
$fields = array($resultMatch . $fields);
|
|
|
|
} else {
|
|
|
|
$fields = array_merge(array($resultMatch), $fields);
|
|
|
|
}
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
return $fields;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Creates a WHERE clause by parsing given conditions data.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param mixed $conditions Array or string of conditions
|
|
|
|
* @return string SQL fragment
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function conditions($conditions) {
|
|
|
|
$clause = '';
|
|
|
|
if (!is_array($conditions)) {
|
|
|
|
if (!preg_match('/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i', $conditions, $match)) {
|
|
|
|
$clause = ' WHERE ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($conditions)) {
|
|
|
|
if (trim($conditions) == '') {
|
|
|
|
$conditions = ' 1 = 1';
|
|
|
|
} else {
|
|
|
|
$start = null;
|
|
|
|
$end = null;
|
|
|
|
|
|
|
|
if (!empty($this->startQuote)) {
|
|
|
|
$start = '\\\\' . $this->startQuote . '\\\\';
|
|
|
|
}
|
|
|
|
$end = $this->endQuote;
|
|
|
|
|
|
|
|
if (!empty($this->endQuote)) {
|
|
|
|
$end = '\\\\' . $this->endQuote . '\\\\';
|
|
|
|
}
|
|
|
|
preg_match_all('/(?:\'[^\'\\\]*(?:\\\.[^\'\\\]*)*\')|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', $conditions, $match, PREG_PATTERN_ORDER);
|
|
|
|
|
|
|
|
if (isset($match['1']['0'])) {
|
|
|
|
$pregCount = count($match['1']);
|
|
|
|
|
|
|
|
for($i = 0; $i < $pregCount; $i++) {
|
2006-10-09 21:42:35 +00:00
|
|
|
if (!empty($match['1'][$i]) && !is_numeric($match['1'][$i])) {
|
2006-10-09 21:03:45 +00:00
|
|
|
$conditions = preg_replace('/^' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);
|
|
|
|
if (strpos($conditions, '(' . $match['0'][$i]) === false) {
|
|
|
|
$conditions = preg_replace('/[^\w]' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);
|
|
|
|
} else {
|
|
|
|
$conditions = preg_replace('/' . $match['0'][$i] . '/', ' '.$this->name($match['1'][$i]), $conditions);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $clause . $conditions;
|
|
|
|
} else {
|
|
|
|
$clause = ' WHERE ';
|
|
|
|
$out = $this->conditionKeysToString($conditions);
|
2006-08-30 16:00:20 +00:00
|
|
|
if (empty($out)) {
|
|
|
|
return $clause . ' (1 = 1)';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
return $clause . ' (' . join(') AND (', $out) . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function conditionKeysToString($conditions) {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-08-15 19:19:39 +00:00
|
|
|
$c = 0;
|
2006-06-14 18:02:37 +00:00
|
|
|
$data = null;
|
2006-05-26 05:29:17 +00:00
|
|
|
$out = array();
|
|
|
|
$bool = array('and', 'or', 'and not', 'or not', 'xor', '||', '&&');
|
2006-08-15 19:19:39 +00:00
|
|
|
$join = ' AND ';
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
foreach($conditions as $key => $value) {
|
|
|
|
if (in_array(strtolower(trim($key)), $bool)) {
|
2006-08-15 19:19:39 +00:00
|
|
|
$join = ' ' . strtoupper($key) . ' ';
|
|
|
|
$value = $this->conditionKeysToString($value);
|
|
|
|
if (strpos($join, 'NOT')) {
|
|
|
|
$out[] = 'NOT (' . join(') ' . strtoupper($key) . ' (', $value) . ')';
|
|
|
|
} else {
|
|
|
|
$out[] = '(' . join(') ' . strtoupper($key) . ' (', $value) . ')';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2006-06-14 18:02:37 +00:00
|
|
|
if (is_array($value) && !empty($value)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
$keys = array_keys($value);
|
|
|
|
if ($keys[0] === 0) {
|
|
|
|
$data = $this->name($key) . ' IN (';
|
2006-11-01 19:19:11 +00:00
|
|
|
if (strpos($value[0], '-!') === 0){
|
|
|
|
$value[0] = str_replace('-!', '', $value[0]);
|
|
|
|
$data .= $value[0];
|
|
|
|
$data .= ')';
|
|
|
|
} else {
|
|
|
|
foreach($value as $valElement) {
|
|
|
|
$data .= $this->value($valElement) . ', ';
|
|
|
|
}
|
|
|
|
$data[strlen($data) - 2] = ')';
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-11-01 19:19:11 +00:00
|
|
|
$out[] = '(' . join(') AND (', $this->conditionKeysToString($value)) . ')';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
} elseif(is_numeric($key)) {
|
|
|
|
$data = ' ' . $value;
|
|
|
|
} elseif($value === null) {
|
2006-08-15 19:19:39 +00:00
|
|
|
$data = $this->name($key) . ' IS NULL';
|
2006-05-26 05:29:17 +00:00
|
|
|
} elseif($value === '') {
|
|
|
|
$data = $this->name($key) . " = ''";
|
2006-11-01 19:19:11 +00:00
|
|
|
} elseif(preg_match('/^([a-z]*\\([a-z0-9]*\\)\\x20?|(?:like\\x20)|(?:or\\x20)|(?:not\\x20)|(?:in\\x20)|(?:between\\x20)|(?:regexp\\x20)|[<> = !]{1,3}\\x20?)?(.*)/i', $value, $match)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (preg_match('/(\\x20[\\w]*\\x20)/', $key, $regs)) {
|
|
|
|
$clause = $regs['1'];
|
|
|
|
$key = preg_replace('/' . $regs['1'] . '/', '', $key);
|
|
|
|
}
|
|
|
|
|
2006-09-04 13:37:26 +00:00
|
|
|
$mValue = trim($match['1']);
|
2006-05-26 05:29:17 +00:00
|
|
|
if (empty($match['1'])) {
|
|
|
|
$match['1'] = ' = ';
|
2006-09-04 13:37:26 +00:00
|
|
|
} elseif (empty($mValue)) {
|
|
|
|
$match['1'] = ' = ';
|
|
|
|
$match['2'] = $match['0'];
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($match['2'], '-!') === 0) {
|
|
|
|
$match['2'] = str_replace('-!', '', $match['2']);
|
2006-06-14 18:02:37 +00:00
|
|
|
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
if ($match['2'] != '' && !is_numeric($match['2'])) {
|
|
|
|
$match['2'] = $this->value($match['2']);
|
|
|
|
$match['2'] = str_replace(' AND ', "' AND '", $match['2']);
|
|
|
|
}
|
|
|
|
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if ($data != null) {
|
|
|
|
$out[] = $data;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-08-15 19:19:39 +00:00
|
|
|
$c++;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-04-27 10:04:08 +00:00
|
|
|
* Returns a limit statement in the correct format for the particular database.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-04-27 10:04:08 +00:00
|
|
|
* @param int $limit Limit of results returned
|
|
|
|
* @param int $offset Offset from which to start results
|
|
|
|
* @return string SQL limit/offset statement
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function limit($limit, $offset = null) {
|
|
|
|
if ($limit) {
|
|
|
|
$rt = '';
|
|
|
|
if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {
|
|
|
|
$rt = ' LIMIT';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset) {
|
|
|
|
$rt .= ' ' . $offset . ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
$rt .= ' ' . $limit;
|
|
|
|
return $rt;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* Returns an ORDER BY clause as a string.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @param string $key Field reference, as a key (i.e. Post.title)
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param string $direction Direction (ASC or DESC)
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return string ORDER BY clause
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function order($keys, $direction = 'ASC') {
|
|
|
|
if (is_string($keys) && strpos($keys, ',')) {
|
|
|
|
$keys = explode(',', $keys);
|
|
|
|
array_map('trim', $keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($keys)) {
|
|
|
|
foreach($keys as $key => $val) {
|
|
|
|
if (is_numeric($key) && empty($val)) {
|
|
|
|
unset ($keys[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($keys) || (is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0]))) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($keys)) {
|
2006-11-10 18:45:46 +00:00
|
|
|
if (Set::countDim($keys) > 1) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$new = array();
|
|
|
|
|
|
|
|
foreach($keys as $val) {
|
|
|
|
$val = $this->order($val);
|
|
|
|
$new[] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = $new;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($keys as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$value = ltrim(r('ORDER BY ', '', $this->order($value)));
|
|
|
|
$key = $value;
|
|
|
|
|
|
|
|
if (!preg_match('/\\x20ASC|\\x20DESC/i', $key)) {
|
|
|
|
$value = ' ' . $direction;
|
|
|
|
} else {
|
|
|
|
$value = '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$value = ' ' . $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) {
|
|
|
|
$dir = '';
|
|
|
|
$hasDir = preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir);
|
|
|
|
|
|
|
|
if ($hasDir) {
|
|
|
|
$dir = $dir[0];
|
|
|
|
$key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key);
|
|
|
|
} else {
|
|
|
|
$dir = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = trim($this->name($key) . ' ' . $dir);
|
|
|
|
}
|
|
|
|
$order[] = $this->order($key . $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ' ORDER BY ' . trim(r('ORDER BY', '', join(',', $order)));
|
|
|
|
} else {
|
|
|
|
$keys = preg_replace('/ORDER\\x20BY/i', '', $keys);
|
|
|
|
|
|
|
|
if (strpos($keys, '.')) {
|
|
|
|
preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result,
|
|
|
|
PREG_PATTERN_ORDER);
|
|
|
|
$pregCount = count($result['0']);
|
|
|
|
|
|
|
|
for($i = 0; $i < $pregCount; $i++) {
|
|
|
|
$keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/\\x20ASC|\\x20DESC/i', $keys)) {
|
|
|
|
return ' ORDER BY ' . $keys;
|
|
|
|
} else {
|
|
|
|
return ' ORDER BY ' . $keys . ' ' . $direction;
|
|
|
|
}
|
|
|
|
} elseif(preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) {
|
|
|
|
$direction = $match['1'];
|
|
|
|
$keys = preg_replace('/' . $match['1'] . '/', '', $keys);
|
|
|
|
return ' ORDER BY ' . $keys . $direction;
|
|
|
|
} else {
|
|
|
|
$direction = ' ' . $direction;
|
|
|
|
}
|
|
|
|
return ' ORDER BY ' . $keys . $direction;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-01-17 17:52:23 +00:00
|
|
|
* Disconnects database, kills the connection and says the connection is closed,
|
|
|
|
* and if DEBUG is turned on, the log for this object is shown.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function close() {
|
|
|
|
if ($this->fullDebug) {
|
|
|
|
$this->showLog();
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function buildSchemaQuery($schema) {
|
|
|
|
die ("Implement in DBO");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Destructor. Closes connection to the database.
|
|
|
|
*
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __destruct() {
|
|
|
|
if ($this->__transactionStarted) {
|
|
|
|
$this->rollback();
|
|
|
|
}
|
|
|
|
$this->close();
|
|
|
|
parent::__destruct();
|
|
|
|
}
|
2006-01-12 22:06:11 +00:00
|
|
|
/**
|
|
|
|
* Checks if the specified table contains any record matching specified SQL
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param Model $model Model to search
|
2006-01-12 22:06:11 +00:00
|
|
|
* @param string $sql SQL WHERE clause (condition only, not the "WHERE" part)
|
|
|
|
* @return boolean True if the table has a matching record, else false
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function hasAny($model, $sql) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$sql = $this->conditions($sql);
|
2006-10-25 13:27:32 +00:00
|
|
|
$out = $this->fetchRow("SELECT COUNT(*) " . $this->alias . "count FROM " . $this->fullTableName($model) . ' ' . ($sql ? ' ' . $sql : 'WHERE 1 = 1'));
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if (is_array($out)) {
|
|
|
|
return $out[0]['count'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-03-26 09:59:40 +00:00
|
|
|
/**
|
2006-11-03 04:10:40 +00:00
|
|
|
* Gets the 'meta' definition of the given database table, where $model is either
|
|
|
|
* a model object, or the full table name.
|
|
|
|
*
|
|
|
|
* @param mixed $model
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getDefinition($model) {
|
|
|
|
if (is_string($model)) {
|
|
|
|
$table = $model;
|
|
|
|
} else {
|
|
|
|
$table = $this->fullTableName($model, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
2006-03-26 19:15:30 +00:00
|
|
|
* Translates between PHP boolean values and Database (faked) boolean values
|
2006-03-26 09:59:40 +00:00
|
|
|
*
|
|
|
|
* @param mixed $data Value to be translated
|
|
|
|
* @return mixed Converted boolean value
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function boolean($data) {
|
|
|
|
if ($data === true || $data === false) {
|
|
|
|
if ($data === true) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!empty($data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-03-28 19:46:59 +00:00
|
|
|
?>
|