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
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 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
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
2007-03-30 02:54:23 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.datasources
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2006-05-26 05:29:17 +00:00
|
|
|
* @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
|
|
|
*/
|
2007-01-24 20:04:27 +00:00
|
|
|
uses('set');
|
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
|
|
|
/**
|
2007-08-21 21:46:59 +00:00
|
|
|
* index definition, standard cake, primary, index, unique
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
2007-08-21 21:46:59 +00:00
|
|
|
* @var array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2007-08-21 21:46:59 +00:00
|
|
|
var $index = array('PRI'=> 'primary', 'MUL'=> 'index', 'UNI'=>'unique');
|
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 ';
|
2007-01-31 21:07:32 +00:00
|
|
|
/**
|
2007-08-21 21:46:59 +00:00
|
|
|
* Enter description here...
|
2007-01-31 21:07:32 +00:00
|
|
|
*
|
2007-08-21 21:46:59 +00:00
|
|
|
* @var unknown_type
|
2007-01-31 21:07:32 +00:00
|
|
|
*/
|
2007-08-21 21:46:59 +00:00
|
|
|
var $goofyLimit = false;
|
2006-04-27 10:04:08 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2007-08-21 21:46:59 +00:00
|
|
|
var $__bypass = false;
|
|
|
|
/**
|
|
|
|
* The set of valid SQL operations usable in a WHERE statement
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $__sqlOps = array('like', 'ilike', 'or', 'not', 'in', 'between', 'regexp', 'similar to');
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2006-10-09 21:03:45 +00:00
|
|
|
function __construct($config = null, $autoConnect = true) {
|
2006-12-05 09:49:59 +00:00
|
|
|
$this->debug = Configure::read() > 0;
|
|
|
|
$this->fullDebug = Configure::read() > 1;
|
2006-05-26 05:29:17 +00:00
|
|
|
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
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True on success, false on failure
|
2006-04-24 16:24:57 +00:00
|
|
|
*/
|
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);
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$out[$keys[$i]] = $this->value($data[$keys[$i]]);
|
|
|
|
}
|
|
|
|
return $out;
|
2007-02-02 02:21:44 +00:00
|
|
|
} elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
|
2006-09-11 07:09:14 +00:00
|
|
|
return $data;
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->fullDebug && Configure::read() > 1) {
|
2006-11-01 03:52:41 +00:00
|
|
|
$this->logQuery($sql);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if ($this->error) {
|
2007-02-15 03:03:17 +00:00
|
|
|
$this->showQuery($sql);
|
2006-05-26 05:29:17 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->_result;
|
|
|
|
}
|
|
|
|
}
|
2007-08-27 03:15:11 +00:00
|
|
|
/**
|
|
|
|
* DataSource Query abstraction
|
|
|
|
*
|
|
|
|
* @return resource Result resource identifier
|
|
|
|
*/
|
|
|
|
function query() {
|
|
|
|
$args = func_get_args();
|
|
|
|
$fields = null;
|
|
|
|
$order = null;
|
|
|
|
$limit = null;
|
|
|
|
$page = null;
|
|
|
|
$recursive = null;
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
$params = $args[1];
|
|
|
|
|
|
|
|
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]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$or = (strpos($field, '_or_') !== false);
|
|
|
|
if ($or) {
|
|
|
|
$field = explode('_or_', $field);
|
|
|
|
} else {
|
|
|
|
$field = explode('_and_', $field);
|
|
|
|
}
|
|
|
|
$off = count($field) - 1;
|
|
|
|
|
|
|
|
if (isset($params[1 + $off])) {
|
|
|
|
$fields = $params[1 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[2 + $off])) {
|
|
|
|
$order = $params[2 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists(0, $params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
$query = array();
|
|
|
|
foreach ($field as $f) {
|
|
|
|
if (!is_array($params[$c]) && !empty($params[$c]) && $params[$c] !== true && $params[$c] !== false) {
|
|
|
|
$query[$args[2]->name . '.' . $f] = '= ' . $params[$c];
|
|
|
|
} else {
|
|
|
|
$query[$args[2]->name . '.' . $f] = $params[$c];
|
|
|
|
}
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($or) {
|
|
|
|
$query = array('OR' => $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($all) {
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
return $args[2]->findAll($query, $fields, $order, $limit, $page, $recursive);
|
|
|
|
} else {
|
|
|
|
if (isset($params[3 + $off])) {
|
|
|
|
$recursive = $params[3 + $off];
|
|
|
|
}
|
|
|
|
return $args[2]->find($query, $fields, $order, $recursive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($args[1]) && $args[1] === true) {
|
|
|
|
return $this->fetchAll($args[0], true);
|
|
|
|
}
|
|
|
|
return $this->fetchAll($args[0], false);
|
|
|
|
}
|
|
|
|
}
|
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-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
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param bool $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])) {
|
2007-03-25 19:46:16 +00:00
|
|
|
if (preg_match('/^\s*select/i', $sql)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return $this->_queryCache[$sql];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->execute($sql)) {
|
|
|
|
$out = array();
|
|
|
|
|
2007-06-20 06:15:35 +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
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!isset($data[$name]) || empty($data[$name])) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $data[$name];
|
|
|
|
}
|
|
|
|
}
|
2007-01-24 20:04:27 +00:00
|
|
|
/**
|
2007-03-20 17:38:53 +00:00
|
|
|
* Returns a quoted name of $data for use in an SQL statement.
|
2007-01-24 20:04:27 +00:00
|
|
|
* Strips fields out of SQL functions before quoting.
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @return string SQL field
|
|
|
|
*/
|
|
|
|
function name($data) {
|
2007-07-29 12:21:41 +00:00
|
|
|
if (preg_match_all('/([^(]*)\((.*)\)(.*)/', $data, $fields)) {
|
2007-01-24 20:04:27 +00:00
|
|
|
$fields = Set::extract($fields, '{n}.0');
|
2007-07-29 12:21:41 +00:00
|
|
|
if (!empty($fields[1])) {
|
|
|
|
if (!empty($fields[2])) {
|
|
|
|
return $fields[1] . '(' . $this->name($fields[2]) . ')' . $fields[3];
|
|
|
|
} else {
|
|
|
|
return $fields[1] . '()' . $fields[3];
|
|
|
|
}
|
2007-01-24 20:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
if ($data == '*') {
|
|
|
|
return '*';
|
|
|
|
}
|
|
|
|
$data = $this->startQuote . str_replace('.', $this->endQuote . '.' . $this->startQuote, $data) . $this->endQuote;
|
|
|
|
$data = str_replace($this->startQuote . $this->startQuote, $this->startQuote, $data);
|
2007-10-06 04:37:58 +00:00
|
|
|
|
2007-10-04 05:04:44 +00:00
|
|
|
if (!empty($this->endQuote) && $this->endQuote == $this->startQuote) {
|
|
|
|
$oddMatches = substr_count($data, $this->endQuote);
|
|
|
|
if ($oddMatches % 2 == 1) {
|
|
|
|
$data = trim($data, $this->endQuote);
|
|
|
|
}
|
2007-08-29 01:02:21 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
return str_replace($this->endQuote . $this->endQuote, $this->endQuote, $data);
|
2007-01-24 20:04:27 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Checks if it's connected to the database
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the database is connected, else false
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
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
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param bool $sorted
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
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-12-22 22:49:47 +00:00
|
|
|
|
2006-11-01 15:58:52 +00:00
|
|
|
if (php_sapi_name() != 'cli') {
|
2007-10-21 18:34:23 +00:00
|
|
|
print ("<table class=\"cake-sql-log\" id=\"cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)) . "\" summary=\"Cake SQL Log\" cellspacing=\"0\" border = \"0\">\n<caption>{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</caption>\n");
|
2006-11-01 15:58:52 +00:00
|
|
|
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");
|
2006-12-22 22:49:47 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($log as $k => $i) {
|
2007-04-29 06:11:32 +00:00
|
|
|
print ("<tr><td>" . ($k + 1) . "</td><td>" . h($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
|
|
|
}
|
2007-01-04 16:13:24 +00:00
|
|
|
print ("</tbody></table>\n");
|
2006-11-01 15:58:52 +00:00
|
|
|
} else {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($log as $k => $i) {
|
2006-11-01 15:58:52 +00:00
|
|
|
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;
|
2007-01-04 16:13:24 +00:00
|
|
|
if (strlen($sql) > 200 && !$this->fullDebug && Configure::read() > 1) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$sql = substr($sql, 0, 200) . '[...]';
|
|
|
|
}
|
|
|
|
|
2007-02-15 03:03:17 +00:00
|
|
|
if (($this->debug || $error) && Configure::read() > 0) {
|
|
|
|
e("<p style = \"text-align:left\"><b>Query:</b> {$sql} ");
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($error) {
|
2007-02-15 03:03:17 +00:00
|
|
|
trigger_error("<span style = \"color:Red;text-align:left\"><b>SQL Error:</b> {$this->error}</span>", E_USER_WARNING);
|
|
|
|
} else {
|
|
|
|
e("<small>[Aff:{$this->affected} Num:{$this->numRows} Took:{$this->took}ms]</small>");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
print ('</p>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Gets full table name including prefix
|
|
|
|
*
|
|
|
|
* @param mixed $model
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param bool $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)) {
|
2007-08-28 21:44:47 +00:00
|
|
|
$table = $model->tablePrefix . $model->table;
|
2006-05-26 05:29:17 +00:00
|
|
|
} 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
|
2007-10-22 16:09:35 +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
|
2007-10-22 16:11:12 +00:00
|
|
|
* @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;
|
|
|
|
|
2007-10-21 18:34:23 +00:00
|
|
|
if ($recursive === null && isset($queryData['recursive'])) {
|
|
|
|
$recursive = $queryData['recursive'];
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!is_null($recursive)) {
|
|
|
|
$_recursive = $model->recursive;
|
|
|
|
$model->recursive = $recursive;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($queryData['fields'])) {
|
|
|
|
$this->__bypass = true;
|
2007-03-20 17:38:53 +00:00
|
|
|
$queryData['fields'] = $this->fields($model, null, $queryData['fields']);
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = $this->fields($model);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($model->__associations as $type) {
|
|
|
|
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
|
|
|
|
2006-11-30 22:26:24 +00:00
|
|
|
$external = isset($assocData['external']);
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
|
2006-11-30 22:26:24 +00:00
|
|
|
if (true === $this->generateSelfAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$linkedModels[] = $type . '/' . $assoc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($model->useDbConfig == $linkModel->useDbConfig) {
|
2006-11-30 22:26:24 +00:00
|
|
|
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$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) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($model->__associations as $type) {
|
|
|
|
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
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif ($model->recursive > 1 && ($type == 'belongsTo' || $type == 'hasOne')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
// 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
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (is_array($results[$i])) {
|
|
|
|
$keys = array_keys($results[$i]);
|
|
|
|
$count2 = count($keys);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($j = 0; $j < $count2; $j++) {
|
2007-08-29 20:47:03 +00:00
|
|
|
$className = $key = $keys[$j];
|
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
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
if (isset($data[0][$key])) {
|
|
|
|
$results[$i][$key] = $data[0][$key];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2007-10-22 16:11:12 +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
|
|
|
|
2007-08-27 03:15:11 +00:00
|
|
|
if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($resultSet) || !is_array($resultSet)) {
|
2006-12-05 09:49:59 +00:00
|
|
|
if (Configure::read() > 0) {
|
2007-09-15 20:34:27 +00:00
|
|
|
e('<div style = "font: Verdana bold 12px; color: #FF0000">' . sprintf(__('SQL Error in model %s:', true), $model->name) . ' ');
|
2006-05-26 05:29:17 +00:00
|
|
|
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);
|
2007-08-27 03:15:11 +00:00
|
|
|
|
2007-08-17 13:37:56 +00:00
|
|
|
if ($type === 'hasMany' && (!isset($assocData['limit']) || empty($assocData['limit']))) {
|
2007-08-27 03:15:11 +00:00
|
|
|
$ins = $fetch = array();
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2007-08-27 03:15:11 +00:00
|
|
|
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
|
2007-04-09 21:30:55 +00:00
|
|
|
$ins[] = $in;
|
|
|
|
}
|
2007-04-09 08:54:25 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 07:51:52 +00:00
|
|
|
if (!empty($ins)) {
|
2007-08-27 03:15:11 +00:00
|
|
|
$query = r('{$__cakeID__$}', join(', ', $ins), $query);
|
2007-04-09 08:54:25 +00:00
|
|
|
$fetch = $this->fetchAll($query, $model->cacheQueries, $model->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($fetch) && is_array($fetch)) {
|
|
|
|
if ($recursive > 0) {
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($linkModel->__associations as $type1) {
|
|
|
|
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
|
2007-04-09 08:54:25 +00:00
|
|
|
|
2007-08-29 20:47:03 +00:00
|
|
|
$deepModel =& $linkModel->{$assoc1};
|
2007-04-09 08:54:25 +00:00
|
|
|
if ($deepModel->alias != $model->name) {
|
|
|
|
$tmpStack = $stack;
|
|
|
|
$tmpStack[] = $assoc1;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive);
|
2007-09-15 20:34:27 +00:00
|
|
|
} elseif ($type === 'hasAndBelongsToMany') {
|
2007-08-29 13:33:14 +00:00
|
|
|
$ins = $fetch = array();
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
|
|
|
|
$ins[] = $in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($ins)) {
|
|
|
|
$query = r('{$__cakeID__$}', '(' .join(', ', $ins) .')', $query);
|
|
|
|
$query = r('= (', 'IN (', $query);
|
|
|
|
$query = r(' WHERE 1 = 1', '', $query);
|
|
|
|
}
|
|
|
|
|
2007-09-15 20:34:27 +00:00
|
|
|
$with = $model->hasAndBelongsToMany[$association]['with'];
|
|
|
|
$foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey'];
|
|
|
|
$habtmFields = $model->{$with}->loadInfo();
|
|
|
|
$habtmFields = $habtmFields->extract('{n}.name');
|
|
|
|
$habtmFieldsCount = count($habtmFields);
|
2007-04-09 08:54:25 +00:00
|
|
|
|
2007-09-15 20:34:27 +00:00
|
|
|
$q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack);
|
2007-06-20 07:51:52 +00:00
|
|
|
if ($q != false) {
|
2006-09-14 02:11:38 +00:00
|
|
|
$fetch = $this->fetchAll($q, $model->cacheQueries, $model->name);
|
|
|
|
} else {
|
|
|
|
$fetch = null;
|
|
|
|
}
|
2007-09-15 20:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$row =& $resultSet[$i];
|
|
|
|
|
|
|
|
if ($type !== 'hasAndBelongsToMany') {
|
|
|
|
$q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack);
|
|
|
|
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) {
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($linkModel->__associations as $type1) {
|
|
|
|
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
|
2006-09-14 02:11:38 +00:00
|
|
|
|
2007-08-29 20:47:03 +00:00
|
|
|
$deepModel =& $linkModel->{$assoc1};
|
2007-09-15 07:50:32 +00:00
|
|
|
if (($type1 === 'belongsTo') || ($deepModel->name === $model->name && $type === 'belongsTo') || ($deepModel->name != $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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 20:34:27 +00:00
|
|
|
if ($type == 'hasAndBelongsToMany') {
|
|
|
|
$merge = array();
|
|
|
|
foreach($fetch as $j => $data) {
|
|
|
|
if(isset($data[$with]) && $data[$with][$foreignKey] === $row[$model->name][$model->primaryKey]) {
|
|
|
|
if ($habtmFieldsCount > 2) {
|
|
|
|
$merge[] = $data;
|
|
|
|
} else {
|
|
|
|
$merge[] = Set::diff($data, array($with => $data[$with]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($merge) && !isset($row[$association])) {
|
|
|
|
$row[$association] = $merge;
|
|
|
|
} else {
|
|
|
|
$this->__mergeAssociation($resultSet[$i], $merge, $association, $type);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->__mergeAssociation($resultSet[$i], $fetch, $association, $type);
|
|
|
|
}
|
2007-05-21 03:54:42 +00:00
|
|
|
$resultSet[$i][$association] = $linkModel->afterfind($resultSet[$i][$association]);
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$tempArray[0][$association] = false;
|
|
|
|
$this->__mergeAssociation($resultSet[$i], $tempArray, $association, $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-09 08:54:25 +00:00
|
|
|
|
2007-06-20 07:51:52 +00:00
|
|
|
function __mergeHasMany(&$resultSet, $merge, $association, &$model, &$linkModel) {
|
2007-08-27 03:15:11 +00:00
|
|
|
foreach ($resultSet as $i => $value) {
|
2007-04-09 08:54:25 +00:00
|
|
|
$count = 0;
|
2007-08-27 03:15:11 +00:00
|
|
|
$merged[$association] = array();
|
|
|
|
foreach ($merge as $j => $data) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($value[$model->name]) && $value[$model->name][$model->primaryKey] === $data[$association][$model->hasMany[$association]['foreignKey']]) {
|
|
|
|
if (count($data) > 1) {
|
2007-08-27 03:15:11 +00:00
|
|
|
$data = am($data[$association], $data);
|
|
|
|
unset($data[$association]);
|
|
|
|
$merged[$association][] = $data;
|
2007-04-09 08:54:25 +00:00
|
|
|
} else {
|
|
|
|
$merged[$association][] = $data[$association];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count++;
|
|
|
|
}
|
2007-06-20 07:51:52 +00:00
|
|
|
if (isset($value[$model->name])) {
|
2007-08-27 03:15:11 +00:00
|
|
|
$resultSet[$i] = Set::pushDiff($resultSet[$i], $merged);
|
2007-04-09 09:40:56 +00:00
|
|
|
unset($temp);
|
|
|
|
}
|
2007-04-09 08:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($merge[0] as $assoc => $data2) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($assoc != $association) {
|
|
|
|
$merge[0][$association][$assoc] = $data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!isset($data[$association])) {
|
|
|
|
if ($merge[0][$association] != null) {
|
2007-04-04 19:55:57 +00:00
|
|
|
$data[$association] = $merge[0][$association];
|
|
|
|
} else {
|
|
|
|
$data[$association] = array();
|
|
|
|
}
|
2006-09-20 03:26:52 +00:00
|
|
|
} else {
|
2007-06-20 07:51:52 +00:00
|
|
|
if (is_array($merge[0][$association])) {
|
2007-03-30 00:08:10 +00:00
|
|
|
foreach ($data[$association] as $k => $v) {
|
2007-06-20 07:51:52 +00:00
|
|
|
if (!is_array($v)) {
|
2007-03-30 00:08:10 +00:00
|
|
|
$dataAssocTmp[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($merge[0][$association] as $k => $v) {
|
2007-06-20 07:51:52 +00:00
|
|
|
if (!is_array($v)) {
|
2007-03-30 00:08:10 +00:00
|
|
|
$mergeAssocTmp[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (array_keys($merge[0]) === array_keys($data)) {
|
2007-03-30 00:08:10 +00:00
|
|
|
$data[$association][$association] = $merge[0][$association];
|
2007-06-20 01:37:23 +00:00
|
|
|
} else {
|
|
|
|
$diff = Set::diff($dataAssocTmp, $mergeAssocTmp);
|
|
|
|
$data[$association] = array_merge($merge[0][$association], $diff);
|
2007-03-30 00:08:10 +00:00
|
|
|
}
|
2006-09-20 03:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($merge[0][$association] === false) {
|
2007-06-20 07:51:52 +00:00
|
|
|
if (!isset($data[$association])) {
|
2006-09-19 02:08:54 +00:00
|
|
|
$data[$association] = array();
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($merge as $i => $row) {
|
2006-05-26 05:29:17 +00:00
|
|
|
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;
|
2007-03-26 17:16:43 +00:00
|
|
|
if (empty($alias) && !empty($linkModel)) {
|
|
|
|
$alias = $linkModel->name;
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($queryData['selfJoin'])) {
|
|
|
|
$queryData['selfJoin'] = array();
|
2007-03-30 00:08:10 +00:00
|
|
|
|
2007-08-27 03:15:11 +00:00
|
|
|
$self = array(
|
|
|
|
'fields' => $this->fields($model, null, $queryData['fields']),
|
|
|
|
'joins' => array(array(
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'alias' => $alias,
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'conditions' => array(
|
|
|
|
$model->escapeField($assocData['foreignKey']) => '{$__cakeIdentifier[' . "{$alias}.{$linkModel->primaryKey}" . ']__$}'))
|
|
|
|
),
|
|
|
|
'table' => $this->fullTableName($model),
|
|
|
|
'alias' => $model->name,
|
|
|
|
'limit' => $queryData['limit'],
|
|
|
|
'offset' => $queryData['offset'],
|
|
|
|
'conditions'=> $queryData['conditions'],
|
|
|
|
'order' => $queryData['order']
|
|
|
|
);
|
2007-03-30 00:08:10 +00:00
|
|
|
|
2007-04-05 00:47:17 +00:00
|
|
|
if (!empty($assocData['conditions'])) {
|
2007-04-05 01:08:39 +00:00
|
|
|
$self['joins'][0]['conditions'] = trim($this->conditions(am($self['joins'][0]['conditions'], $assocData['conditions']), true, false));
|
2007-04-05 00:47:17 +00:00
|
|
|
}
|
|
|
|
|
2007-03-27 01:59:39 +00:00
|
|
|
if (!empty($queryData['joins'])) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($queryData['joins'] as $join) {
|
2007-03-27 01:59:39 +00:00
|
|
|
$self['joins'][] = $join;
|
|
|
|
}
|
|
|
|
}
|
2007-03-26 17:16:43 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->__bypass === false) {
|
2007-04-05 00:47:17 +00:00
|
|
|
$self['fields'] = am($self['fields'], $this->fields($linkModel, $alias, (isset($assocData['fields']) ? $assocData['fields'] : '')));
|
2006-09-19 15:24:15 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-04-05 03:31:46 +00:00
|
|
|
if (!in_array($self, $queryData['selfJoin'])) {
|
|
|
|
$queryData['selfJoin'][] = $self;
|
2006-05-26 05:29:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-05 03:31:46 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
} elseif (isset($linkModel)) {
|
|
|
|
return $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
2007-04-05 03:31:46 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2007-03-26 17:16:43 +00:00
|
|
|
$result = $queryData['selfJoin'][0];
|
2007-04-05 03:31:46 +00:00
|
|
|
if (!empty($queryData['joins'])) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($queryData['joins'] as $join) {
|
|
|
|
if (!in_array($join, $result['joins'])) {
|
2007-04-05 19:00:15 +00:00
|
|
|
$result['joins'][] = $join;
|
|
|
|
}
|
2007-04-05 03:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($queryData['conditions'])) {
|
|
|
|
$result['conditions'] = trim($this->conditions(am($result['conditions'], $assocData['conditions']), true, false));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-04-05 03:31:46 +00:00
|
|
|
if (!empty($queryData['fields'])) {
|
|
|
|
$result['fields'] = array_unique(am($result['fields'], $queryData['fields']));
|
|
|
|
}
|
|
|
|
$sql = $this->buildStatement($result, $model);
|
|
|
|
return $sql;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if (empty($queryData['fields'])) {
|
|
|
|
$queryData['fields'] = $this->fields($model, $model->name);
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (!empty($model->hasMany) && $model->recursive > -1) {
|
2007-04-09 21:30:55 +00:00
|
|
|
$assocFields = $this->fields($model, $model->name, array("{$model->name}.{$model->primaryKey}"));
|
|
|
|
$passedFields = $this->fields($model, $model->name, $queryData['fields']);
|
2007-08-27 03:15:11 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (count($passedFields) === 1) {
|
2007-04-09 22:41:17 +00:00
|
|
|
$match = strpos($passedFields[0], $assocFields[0]);
|
|
|
|
$match1 = strpos($passedFields[0], 'COUNT(');
|
2007-06-20 07:51:52 +00:00
|
|
|
if ($match === false && $match1 === false) {
|
2007-04-09 21:30:55 +00:00
|
|
|
$queryData['fields'] = array_unique(array_merge($passedFields, $assocFields));
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = $passedFields;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = array_unique(array_merge($passedFields, $assocFields));
|
|
|
|
}
|
2007-08-27 03:15:11 +00:00
|
|
|
unset($assocFields, $passedFields);
|
2007-03-20 17:38:53 +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 {
|
2007-08-27 03:15:11 +00:00
|
|
|
return $this->buildStatement(array(
|
|
|
|
'fields' => array_unique($queryData['fields']),
|
|
|
|
'table' => $this->fullTableName($model),
|
|
|
|
'alias' => $model->name,
|
|
|
|
'limit' => $queryData['limit'],
|
|
|
|
'offset' => $queryData['offset'],
|
|
|
|
'joins' => $queryData['joins'],
|
|
|
|
'conditions' => $queryData['conditions'],
|
|
|
|
'order' => $queryData['order']), $model
|
|
|
|
);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$alias = $association;
|
|
|
|
|
|
|
|
if ($model->name == $linkModel->name) {
|
|
|
|
$joinedOnSelf = true;
|
|
|
|
}
|
|
|
|
|
2006-11-30 22:26:24 +00:00
|
|
|
if ($external && isset($assocData['finderQuery'])) {
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!empty($assocData['finderQuery'])) {
|
2006-11-30 22:26:24 +00:00
|
|
|
return $assocData['finderQuery'];
|
|
|
|
}
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if ((!$external && in_array($type, array('hasOne', 'belongsTo')) && $this->__bypass === false) || $external) {
|
|
|
|
$fields = $this->fields($linkModel, $alias, $assocData['fields']);
|
|
|
|
} else {
|
|
|
|
$fields = array();
|
2006-11-30 22:26:24 +00:00
|
|
|
}
|
|
|
|
$limit = '';
|
2006-07-22 16:45:18 +00:00
|
|
|
|
2006-11-30 22:26:24 +00:00
|
|
|
if (isset($assocData['limit'])) {
|
2007-05-20 05:20:18 +00:00
|
|
|
if ((!isset($assocData['offset']) || (empty($assocData['offset']))) && isset($assocData['page'])) {
|
2006-11-30 22:26:24 +00:00
|
|
|
$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-11-30 22:26:24 +00:00
|
|
|
switch($type) {
|
|
|
|
case 'hasOne':
|
2007-03-20 17:38:53 +00:00
|
|
|
case 'belongsTo':
|
2006-11-30 22:26:24 +00:00
|
|
|
if ($external) {
|
2007-03-20 17:38:53 +00:00
|
|
|
if ($type == 'hasOne') {
|
2007-04-07 20:12:06 +00:00
|
|
|
$conditions = $this->__mergeConditions($assocData['conditions'], array("{$alias}.{$assocData['foreignKey']}" => '{$__cakeID__$}'));
|
2007-03-20 17:38:53 +00:00
|
|
|
} elseif ($type == 'belongsTo') {
|
|
|
|
$conditions = $this->__mergeConditions($assocData['conditions'], array("{$alias}.{$linkModel->primaryKey}" => '{$__cakeForeignKey__$}'));
|
2007-02-01 02:49:19 +00:00
|
|
|
}
|
2007-08-27 03:15:11 +00:00
|
|
|
$query = am($assocData, array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'fields' => $fields,
|
|
|
|
'alias' => $alias
|
|
|
|
));
|
2007-03-20 17:38:53 +00:00
|
|
|
|
|
|
|
if ($type == 'belongsTo') {
|
|
|
|
// Dunno if we should be doing this for hasOne also...?
|
|
|
|
// Or maybe not doing it at all...?
|
2007-08-27 03:15:11 +00:00
|
|
|
$query = am($query, array('order' => $assocData['order'], 'limit' => $limit));
|
2007-02-01 02:49:19 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2007-03-20 17:38:53 +00:00
|
|
|
if ($type == 'hasOne') {
|
2007-04-07 20:12:06 +00:00
|
|
|
$conditions = $this->__mergeConditions($assocData['conditions'], array("{$alias}.{$assocData['foreignKey']}" => '{$__cakeIdentifier[' . "{$model->name}.{$model->primaryKey}" . ']__$}'));
|
2007-03-20 17:38:53 +00:00
|
|
|
} elseif ($type == 'belongsTo') {
|
2007-04-07 20:12:06 +00:00
|
|
|
$conditions = $this->__mergeConditions($assocData['conditions'], array("{$model->name}.{$assocData['foreignKey']}" => '{$__cakeIdentifier[' . "{$alias}.{$linkModel->primaryKey}" . ']__$}'));
|
2007-03-20 17:38:53 +00:00
|
|
|
}
|
2007-02-01 02:49:19 +00:00
|
|
|
|
2007-08-27 03:15:11 +00:00
|
|
|
$join = array(
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'alias' => $alias,
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'conditions' => trim($this->conditions($conditions, true, false))
|
|
|
|
);
|
2007-04-05 03:31:46 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
$queryData['fields'] = am($queryData['fields'], $fields);
|
2007-02-01 02:49:19 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!empty($assocData['order'])) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$queryData['order'][] = $assocData['order'];
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!in_array($join, $queryData['joins'])) {
|
|
|
|
$queryData['joins'][] = $join;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hasMany':
|
2007-08-27 03:15:11 +00:00
|
|
|
$assocData['fields'] = array_unique(array_merge(
|
|
|
|
$this->fields($linkModel, $alias, $assocData['fields']),
|
|
|
|
$this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}"))
|
|
|
|
));
|
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
$query = array(
|
2007-04-09 08:54:25 +00:00
|
|
|
'conditions' => $this->__mergeConditions(array("{$alias}.{$assocData['foreignKey']}" => array('{$__cakeID__$}')), $assocData['conditions']),
|
2007-04-09 21:30:55 +00:00
|
|
|
'fields' => $assocData['fields'],
|
2007-03-20 17:38:53 +00:00
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'alias' => $alias,
|
|
|
|
'order' => $assocData['order'],
|
|
|
|
'limit' => $limit
|
|
|
|
);
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
case 'hasAndBelongsToMany':
|
2006-11-30 22:26:24 +00:00
|
|
|
$joinTbl = $this->fullTableName($assocData['joinTable']);
|
|
|
|
$joinFields = array();
|
2007-03-20 17:38:53 +00:00
|
|
|
$joinAssoc = null;
|
|
|
|
$joinAlias = $joinTbl;
|
2006-07-04 01:29:48 +00:00
|
|
|
|
2007-01-28 01:53:35 +00:00
|
|
|
if (isset($assocData['with']) && !empty($assocData['with'])) {
|
|
|
|
$joinFields = $model->{$assocData['with']}->loadInfo();
|
|
|
|
$joinFields = $joinFields->extract('{n}.name');
|
2007-08-27 03:15:11 +00:00
|
|
|
|
2007-08-29 13:33:14 +00:00
|
|
|
if (is_array($joinFields) && !empty($joinFields)) {
|
2007-01-28 01:53:35 +00:00
|
|
|
$joinFields = $this->fields($model->{$assocData['with']}, $model->{$assocData['with']}->name, $joinFields);
|
2007-08-29 13:33:14 +00:00
|
|
|
$joinAssoc = $joinAlias = $model->{$assocData['with']}->name;
|
|
|
|
|
2007-08-29 10:14:14 +00:00
|
|
|
} else {
|
|
|
|
$joinFields = array();
|
2006-07-04 01:29:48 +00:00
|
|
|
}
|
2006-11-30 22:26:24 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
$query = array(
|
|
|
|
'conditions' => $assocData['conditions'],
|
|
|
|
'limit' => $limit,
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'alias' => $alias,
|
|
|
|
'fields' => am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields),
|
|
|
|
'order' => $assocData['order'],
|
|
|
|
'joins' => array(array(
|
|
|
|
'table' => $joinTbl,
|
|
|
|
'alias' => $joinAssoc,
|
|
|
|
'conditions' => array(
|
|
|
|
array("{$joinAlias}.{$assocData['foreignKey']}" => '{$__cakeID__$}'),
|
|
|
|
array("{$joinAlias}.{$assocData['associationForeignKey']}" => '{$__cakeIdentifier['."{$alias}.{$linkModel->primaryKey}".']__$}')
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
2007-02-01 02:49:19 +00:00
|
|
|
break;
|
2007-01-31 23:25:05 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
if (isset($query)) {
|
|
|
|
return $this->buildStatement($query, $model);
|
|
|
|
}
|
2007-02-01 02:49:19 +00:00
|
|
|
return null;
|
2007-01-31 23:25:05 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
|
|
|
|
function buildJoinStatement($join) {
|
|
|
|
$data = am(array(
|
|
|
|
'type' => null,
|
|
|
|
'alias' => null,
|
|
|
|
'table' => 'join_table',
|
|
|
|
'conditions' => array()
|
|
|
|
), $join);
|
|
|
|
|
|
|
|
if (!empty($data['alias'])) {
|
|
|
|
$data['alias'] = $this->alias . $this->name($data['alias']);
|
|
|
|
}
|
|
|
|
if (!empty($data['conditions'])) {
|
2007-04-05 01:08:39 +00:00
|
|
|
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
|
2007-03-20 17:38:53 +00:00
|
|
|
}
|
|
|
|
return $this->renderJoinStatement($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildStatement($query, $model) {
|
|
|
|
$query = am(array('offset' => null, 'joins' => array()), $query);
|
|
|
|
if (!empty($query['joins'])) {
|
|
|
|
for ($i = 0; $i < count($query['joins']); $i++) {
|
|
|
|
if (is_array($query['joins'][$i])) {
|
|
|
|
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->renderStatement(array(
|
|
|
|
'conditions' => $this->conditions($query['conditions']),
|
|
|
|
'fields' => join(', ', $query['fields']),
|
|
|
|
'table' => $query['table'],
|
|
|
|
'alias' => $this->alias . $this->name($query['alias']),
|
|
|
|
'order' => $this->order($query['order']),
|
|
|
|
'limit' => $this->limit($query['limit'], $query['offset']),
|
|
|
|
'joins' => join(' ', $query['joins'])
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderJoinStatement($data) {
|
|
|
|
extract($data);
|
2007-04-04 21:40:59 +00:00
|
|
|
return trim("{$type} JOIN {$table} {$alias} ON ({$conditions})");
|
2007-03-20 17:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderStatement($data) {
|
|
|
|
extract($data);
|
|
|
|
return "SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order} {$limit}";
|
|
|
|
}
|
2006-11-01 19:27:08 +00:00
|
|
|
/**
|
|
|
|
* Private method
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-03-20 17:38:53 +00:00
|
|
|
function __mergeConditions($query, $assoc) {
|
|
|
|
if (!empty($assoc)) {
|
|
|
|
if (is_array($query)) {
|
|
|
|
return array_merge((array)$assoc, $query);
|
2006-11-01 19:27:08 +00:00
|
|
|
} else {
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!empty($query)) {
|
|
|
|
$query = array($query);
|
|
|
|
if (is_array($assoc)) {
|
|
|
|
$query = array_merge($query, $assoc);
|
2006-11-01 19:27:08 +00:00
|
|
|
} else {
|
2007-03-20 17:38:53 +00:00
|
|
|
$query[] = $assoc;
|
2006-11-01 19:27:08 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
return $query;
|
2006-11-01 19:27:08 +00:00
|
|
|
} else {
|
2007-03-20 17:38:53 +00:00
|
|
|
return $assoc;
|
2006-11-01 19:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
return $query;
|
2006-11-01 19:27:08 +00:00
|
|
|
}
|
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();
|
2007-03-20 17:38:53 +00:00
|
|
|
|
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
|
|
|
|
2007-06-20 06:15:35 +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
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
$conditions = $this->defaultConditions($model, $conditions);
|
|
|
|
|
|
|
|
if ($conditions === false) {
|
|
|
|
return false;
|
2006-10-18 15:38:37 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
$fields = join(',', $updates);
|
|
|
|
$table = $this->fullTableName($model);
|
|
|
|
$conditions = $this->conditions($conditions);
|
2006-11-25 07:53:31 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!$this->execute("UPDATE {$table} SET {$fields} {$conditions}")) {
|
2006-07-22 14:13:07 +00:00
|
|
|
$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
|
2006-10-16 20:02:34 +00:00
|
|
|
* @param mixed $conditions
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-11-25 07:53:31 +00:00
|
|
|
function delete(&$model, $conditions = null) {
|
2007-03-20 17:38:53 +00:00
|
|
|
$query = $this->defaultConditions($model, $conditions);
|
|
|
|
|
|
|
|
if ($query === false) {
|
2006-11-27 05:32:18 +00:00
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-06-27 19:13:44 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
$table = $this->fullTableName($model);
|
|
|
|
$conditions = $this->conditions($query);
|
|
|
|
|
2007-06-27 19:13:44 +00:00
|
|
|
if ($this->execute("DELETE FROM {$table} {$conditions}") === false) {
|
2006-11-27 05:32:18 +00:00
|
|
|
$model->onError();
|
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-11-27 05:32:18 +00:00
|
|
|
return true;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
/**
|
|
|
|
* Creates a default set of conditions from the model if $conditions is null/empty.
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @param mixed $conditions
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function defaultConditions(&$model, $conditions) {
|
|
|
|
if (!empty($conditions)) {
|
|
|
|
return $conditions;
|
|
|
|
}
|
|
|
|
if (!$model->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return array($model->primaryKey => (array)$model->getID());
|
|
|
|
}
|
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) {
|
2007-03-20 17:38:53 +00:00
|
|
|
if (empty($assoc)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$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) {
|
2007-03-20 17:38:53 +00:00
|
|
|
foreach (array('conditions', 'fields', 'joins', 'order', 'limit', 'offset') as $key) {
|
|
|
|
if (!isset($data[$key]) || empty($data[$key])) {
|
|
|
|
$data[$key] = array();
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
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
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param bool $quote If false, returns fields array unquoted
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2007-03-20 17:38:53 +00:00
|
|
|
function fields(&$model, $alias = null, $fields = array(), $quote = true) {
|
|
|
|
if (empty($alias)) {
|
|
|
|
$alias = $model->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($fields)) {
|
|
|
|
if (!empty($fields)) {
|
|
|
|
$depth = 0;
|
|
|
|
$offset = 0;
|
|
|
|
$buffer = '';
|
|
|
|
$results = array();
|
|
|
|
$length = strlen($fields);
|
|
|
|
|
2007-07-08 02:29:37 +00:00
|
|
|
while ($offset <= $length) {
|
2007-03-20 17:38:53 +00:00
|
|
|
$tmpOffset = -1;
|
|
|
|
$offsets = array(strpos($fields, ',', $offset), strpos($fields, '(', $offset), strpos($fields, ')', $offset));
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
|
|
|
if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
|
|
|
|
$tmpOffset = $offsets[$i];
|
2006-09-21 03:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
if ($tmpOffset !== -1) {
|
|
|
|
$buffer .= substr($fields, $offset, ($tmpOffset - $offset));
|
|
|
|
if ($fields{$tmpOffset} == ',' && $depth == 0) {
|
|
|
|
$results[] = $buffer;
|
|
|
|
$buffer = '';
|
|
|
|
} else {
|
|
|
|
$buffer .= $fields{$tmpOffset};
|
|
|
|
}
|
|
|
|
if ($fields{$tmpOffset} == '(') {
|
|
|
|
$depth++;
|
|
|
|
}
|
|
|
|
if ($fields{$tmpOffset} == ')') {
|
|
|
|
$depth--;
|
|
|
|
}
|
|
|
|
$offset = ++$tmpOffset;
|
2006-09-21 03:56:57 +00:00
|
|
|
} else {
|
2007-03-20 17:38:53 +00:00
|
|
|
$results[] = $buffer . substr($fields, $offset);
|
2007-07-08 02:29:37 +00:00
|
|
|
$offset = $length + 1;
|
2006-09-21 03:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:34:59 +00:00
|
|
|
if (empty($results) && !empty($buffer)) {
|
|
|
|
$results[] = $buffer;
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
|
|
|
|
if (!empty($results)) {
|
|
|
|
$fields = array_map('trim', $results);
|
|
|
|
} else {
|
|
|
|
$fields = array();
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
if (empty($fields)) {
|
|
|
|
$fieldData = $model->loadInfo();
|
|
|
|
$fields = $fieldData->extract('{n}.name');
|
|
|
|
} else {
|
|
|
|
$fields = array_filter($fields);
|
|
|
|
}
|
|
|
|
if (!$quote) {
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
$count = count($fields);
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-03-27 01:34:59 +00:00
|
|
|
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2007-03-20 17:38:53 +00:00
|
|
|
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
|
|
|
|
$prepend = '';
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if (strpos($fields[$i], 'DISTINCT') !== false) {
|
|
|
|
$prepend = 'DISTINCT ';
|
|
|
|
$fields[$i] = trim(r('DISTINCT', '', $fields[$i]));
|
|
|
|
}
|
2007-04-09 22:41:17 +00:00
|
|
|
$dot = strpos($fields[$i], '.');
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-03-20 17:38:53 +00:00
|
|
|
if ($dot === false) {
|
|
|
|
$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]);
|
|
|
|
} else {
|
2007-04-09 22:41:17 +00:00
|
|
|
$comma = strpos($fields[$i], ',');
|
2007-04-09 21:30:55 +00:00
|
|
|
if ($comma === false) {
|
|
|
|
$build = explode('.', $fields[$i]);
|
|
|
|
if (!Set::numeric($build)) {
|
|
|
|
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$comma = explode(',', $fields[$i]);
|
|
|
|
foreach ($comma as $string) {
|
|
|
|
$build = explode('.', $string);
|
|
|
|
if (!Set::numeric($build)) {
|
|
|
|
$value[] = $prepend . $this->name(trim($build[0])) . '.' . $this->name(trim($build[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fields[$i] = implode(', ', $value);
|
2007-01-31 23:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
} elseif (preg_match('/\(([\.\w]+)\)/', $fields[$i], $field)) {
|
|
|
|
if (isset($field[1])) {
|
2007-04-09 22:41:17 +00:00
|
|
|
if (strpos($field[1], '.') === false) {
|
2007-03-20 17:38:53 +00:00
|
|
|
$field[1] = $this->name($alias) . '.' . $this->name($field[1]);
|
|
|
|
} else {
|
|
|
|
$field[0] = explode('.', $field[1]);
|
|
|
|
if (!Set::numeric($field[0])) {
|
|
|
|
$field[0] = join('.', array_map(array($this, 'name'), $field[0]));
|
|
|
|
$fields[$i] = preg_replace('/\(' . $field[1] . '\)/', '(' . $field[0] . ')', $fields[$i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2007-04-05 01:08:39 +00:00
|
|
|
function conditions($conditions, $quoteValues = true, $where = true) {
|
2007-03-20 17:38:53 +00:00
|
|
|
$clause = $out = '';
|
|
|
|
if (is_string($conditions) || empty($conditions) || $conditions === true) {
|
|
|
|
if (empty($conditions) || trim($conditions) == '' || $conditions === true) {
|
|
|
|
return ' WHERE 1 = 1';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!preg_match('/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i', $conditions, $match)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($where) {
|
2007-04-05 01:08:39 +00:00
|
|
|
$clause = ' WHERE ';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-02-08 17:58:19 +00:00
|
|
|
if (trim($conditions) == '') {
|
2006-05-26 05:29:17 +00:00
|
|
|
$conditions = ' 1 = 1';
|
|
|
|
} else {
|
2007-06-16 23:44:56 +00:00
|
|
|
$conditions = $this->__quoteFields($conditions);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return $clause . $conditions;
|
|
|
|
} else {
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($where) {
|
2007-04-05 01:08:39 +00:00
|
|
|
$clause = ' WHERE ';
|
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
if (!empty($conditions)) {
|
2007-03-20 17:38:53 +00:00
|
|
|
$out = $this->conditionKeysToString($conditions, $quoteValues);
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
if (empty($out) || empty($conditions)) {
|
2007-02-08 18:13:14 +00:00
|
|
|
return $clause . ' 1 = 1';
|
2006-08-30 16:00:20 +00:00
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
return $clause . join(' AND ', $out);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-24 20:04:27 +00:00
|
|
|
/**
|
|
|
|
* Creates a WHERE clause by parsing given conditions array. Used by DboSource::conditions().
|
|
|
|
*
|
|
|
|
* @param array $conditions Array or string of conditions
|
|
|
|
* @return string SQL fragment
|
|
|
|
*/
|
2007-07-09 04:03:51 +00:00
|
|
|
function conditionKeysToString($conditions, $quoteValues = true) {
|
|
|
|
$c = 0;
|
|
|
|
$data = $not = null;
|
|
|
|
$out = array();
|
|
|
|
$bool = array('and', 'or', 'not', 'and not', 'or not', 'xor', '||', '&&');
|
|
|
|
$join = ' AND ';
|
|
|
|
|
|
|
|
foreach ($conditions as $key => $value) {
|
|
|
|
if (is_numeric($key) && empty($value)) {
|
|
|
|
continue;
|
|
|
|
} elseif (is_numeric($key) && is_string($value)) {
|
|
|
|
$out[] = $not . $this->__quoteFields($value);
|
|
|
|
} elseif (in_array(strtolower(trim($key)), $bool)) {
|
|
|
|
$join = ' ' . strtoupper($key) . ' ';
|
|
|
|
$value = $this->conditionKeysToString($value, $quoteValues);
|
|
|
|
if (strpos($join, 'NOT') !== false) {
|
|
|
|
if (up(trim($key)) == 'NOT') {
|
|
|
|
$key = 'AND ' . $key;
|
|
|
|
}
|
|
|
|
$not = 'NOT ';
|
|
|
|
} else {
|
|
|
|
$not = null;
|
2006-08-15 19:19:39 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
$out[] = $not . '((' . join(') ' . strtoupper($key) . ' (', $value) . '))';
|
2006-05-26 05:29:17 +00:00
|
|
|
} else {
|
2007-07-09 04:03:51 +00:00
|
|
|
if (is_string($value) && preg_match('/^\{\$__cakeIdentifier\[(.*)\]__\$}$/', $value, $identifier) && isset($identifier[1])) {
|
|
|
|
$data .= $this->name($key) . ' = ' . $this->name($identifier[1]);
|
|
|
|
} elseif (is_array($value) && !empty($value)) {
|
|
|
|
$keys = array_keys($value);
|
|
|
|
if ($keys[0] === 0) {
|
|
|
|
$data = $this->name($key) . ' IN (';
|
|
|
|
if (strpos($value[0], '-!') === 0) {
|
|
|
|
$value[0] = str_replace('-!', '', $value[0]);
|
|
|
|
$data .= $value[0];
|
|
|
|
$data .= ')';
|
|
|
|
} else {
|
|
|
|
if ($quoteValues) {
|
|
|
|
foreach ($value as $valElement) {
|
|
|
|
$data .= $this->value($valElement) . ', ';
|
|
|
|
}
|
2007-06-16 23:44:56 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
$data[strlen($data) - 2] = ')';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ret = $this->conditionKeysToString($value, $quoteValues);
|
|
|
|
if (count($ret) > 1) {
|
|
|
|
$out[] = '(' . join(') AND (', $ret) . ')';
|
|
|
|
} elseif (isset($ret[0])) {
|
|
|
|
$out[] = $ret[0];
|
2007-03-22 00:26:42 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
} elseif (is_numeric($key) && !empty($value)) {
|
|
|
|
$data = $this->__quoteFields($value);
|
|
|
|
} elseif ($value === null || (is_array($value) && empty($value))) {
|
|
|
|
$data = $this->name($key) . ' IS NULL';
|
|
|
|
} elseif ($value === false || $value === true) {
|
|
|
|
$data = $this->name($key) . " = " . $this->value($value, 'boolean');
|
|
|
|
} elseif ($value === '') {
|
|
|
|
$data = $this->name($key) . " = ''";
|
2007-10-19 08:46:22 +00:00
|
|
|
} elseif (preg_match('/^([a-z]+\\([a-z0-9]*\\)\\x20+|(?:' . join('\\x20)|(?:', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)?(.*)/is', $value, $match)) {
|
2007-07-09 04:03:51 +00:00
|
|
|
if (preg_match('/(\\x20[\\w]*\\x20)/', $key, $regs)) {
|
|
|
|
$clause = $regs['1'];
|
|
|
|
$key = preg_replace('/' . $regs['1'] . '/', '', $key);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 04:03:51 +00:00
|
|
|
$not = false;
|
|
|
|
$mValue = trim($match['1']);
|
|
|
|
if (empty($match['1'])) {
|
|
|
|
$match['1'] = ' = ';
|
|
|
|
} elseif (empty($mValue)) {
|
|
|
|
$match['1'] = ' = ';
|
|
|
|
$match['2'] = $match['0'];
|
|
|
|
} elseif (!isset($match['2'])) {
|
|
|
|
$match['1'] = ' = ';
|
|
|
|
$match['2'] = $match['0'];
|
2007-07-25 04:38:28 +00:00
|
|
|
} elseif (low($mValue) == 'not') {
|
2007-07-09 04:03:51 +00:00
|
|
|
$not = $this->conditionKeysToString(array($mValue => array($key => $match[2])), $quoteValues);
|
2007-06-16 23:44:56 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
|
2007-07-25 04:38:28 +00:00
|
|
|
if ($not) {
|
2007-07-09 04:03:51 +00:00
|
|
|
$data = $not[0];
|
|
|
|
} elseif (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 {
|
2007-07-09 04:03:51 +00:00
|
|
|
if (!empty($match['2']) && $quoteValues) {
|
|
|
|
if (!preg_match('/[A-Za-z]+\\([a-z0-9]*\\),?\\x20+/', $match['2'])) {
|
|
|
|
$match['2'] = $this->value($match['2']);
|
|
|
|
}
|
|
|
|
$match['2'] = str_replace(' AND ', "' AND '", $match['2']);
|
|
|
|
}
|
|
|
|
$data = $this->__quoteFields($key);
|
|
|
|
if ($data === $key) {
|
|
|
|
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
|
|
|
|
} else {
|
|
|
|
$data = $data . ' ' . $match['1'] . ' ' . $match['2'];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-09 04:03:51 +00:00
|
|
|
if ($data != null) {
|
|
|
|
$out[] = $data;
|
|
|
|
$data = null;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
$c++;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-07-09 04:03:51 +00:00
|
|
|
return $out;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-04-06 12:19:23 +00:00
|
|
|
/**
|
|
|
|
* Quotes Model.fields
|
|
|
|
*
|
|
|
|
* @param string $conditions
|
|
|
|
* @return string or false if no match
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __quoteFields($conditions) {
|
|
|
|
$start = null;
|
|
|
|
$end = null;
|
2007-06-16 23:44:56 +00:00
|
|
|
$original = $conditions;
|
2007-04-06 12:19:23 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($this->startQuote)) {
|
2007-04-06 12:19:23 +00:00
|
|
|
$start = preg_quote($this->startQuote);
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($this->endQuote)) {
|
2007-04-06 12:19:23 +00:00
|
|
|
$end = preg_quote($this->endQuote);
|
|
|
|
}
|
|
|
|
$conditions = str_replace(array($start, $end), '', $conditions);
|
2007-04-07 06:29:46 +00:00
|
|
|
preg_match_all('/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', $conditions, $replace, PREG_PATTERN_ORDER);
|
2007-04-06 12:19:23 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($replace['1']['0'])) {
|
2007-04-06 12:19:23 +00:00
|
|
|
$pregCount = count($replace['1']);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $pregCount; $i++) {
|
|
|
|
if (!empty($replace['1'][$i]) && !is_numeric($replace['1'][$i])) {
|
2007-06-12 15:13:53 +00:00
|
|
|
$conditions = preg_replace('/\b' . preg_quote($replace['1'][$i]) . '\b/', $this->name($replace['1'][$i]), $conditions);
|
2007-04-06 12:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $conditions;
|
|
|
|
}
|
2007-06-16 23:44:56 +00:00
|
|
|
return $original;
|
2007-04-06 12:19:23 +00:00
|
|
|
}
|
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
|
|
|
*
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $limit Limit of results returned
|
|
|
|
* @param integer $offset Offset from which to start results
|
2006-04-27 10:04:08 +00:00
|
|
|
* @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') {
|
2006-11-28 08:18:53 +00:00
|
|
|
if (is_string($keys) && strpos($keys, ',') && !preg_match('/\(.+\,.+\)/', $keys)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$keys = explode(',', $keys);
|
|
|
|
array_map('trim', $keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($keys)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($keys as $key => $val) {
|
2006-05-26 05:29:17 +00:00
|
|
|
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();
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($keys as $val) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$val = $this->order($val);
|
|
|
|
$new[] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = $new;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($keys as $key => $value) {
|
2006-05-26 05:29:17 +00:00
|
|
|
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 = '';
|
|
|
|
}
|
2007-03-22 00:26:42 +00:00
|
|
|
$key = trim($this->name(trim($key)) . ' ' . trim($dir));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
$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']);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $pregCount; $i++) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$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;
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$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() {
|
2007-01-04 16:13:24 +00:00
|
|
|
if ($this->fullDebug && Configure::read() > 1) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$this->showLog();
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
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)
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the table has a matching record, else false
|
2006-01-12 22:06:11 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function hasAny($model, $sql) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$sql = $this->conditions($sql);
|
2007-03-20 17:38:53 +00:00
|
|
|
$out = $this->fetchRow("SELECT COUNT(" . $model->primaryKey . ") " . $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-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;
|
|
|
|
}
|
|
|
|
}
|
2007-03-20 17:38:53 +00:00
|
|
|
/**
|
|
|
|
* Destructor. Closes connection to the database.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __destruct() {
|
|
|
|
if ($this->_transactionStarted) {
|
2007-03-29 00:49:53 +00:00
|
|
|
$null = null;
|
|
|
|
$this->rollback($null);
|
2007-03-20 17:38:53 +00:00
|
|
|
}
|
|
|
|
parent::__destruct();
|
|
|
|
}
|
2007-07-25 04:38:28 +00:00
|
|
|
/**
|
|
|
|
* Inserts multiple values into a join table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param string $fields
|
|
|
|
* @param array $values
|
|
|
|
*/
|
|
|
|
function insertMulti($table, $fields, $values) {
|
|
|
|
$values = implode(', ', $values);
|
|
|
|
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
|
|
|
|
}
|
2007-08-21 21:46:59 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of the indexes in given datasource name.
|
|
|
|
*
|
|
|
|
* @param string $model Name of model to inspect
|
|
|
|
* @return array Fields in table. Keys are column and unique
|
|
|
|
*/
|
|
|
|
function index($model) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generate a create syntax from CakeSchema
|
|
|
|
*
|
|
|
|
* @param object $schema An instance of a subclass of CakeSchema
|
|
|
|
* @param string $table Optional. If specified only the table name given will be generated.
|
|
|
|
* Otherwise, all tables defined in the schema are generated.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function createSchema($schema, $table = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generate a alter syntax from CakeSchema::compare()
|
|
|
|
*
|
|
|
|
* @param unknown_type $schema
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-08-21 23:22:15 +00:00
|
|
|
function alterSchema($compare, $table = null) {
|
2007-08-21 21:46:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generate a drop syntax from CakeSchema
|
|
|
|
*
|
|
|
|
* @param object $schema An instance of a subclass of CakeSchema
|
|
|
|
* @param string $table Optional. If specified only the table name given will be generated.
|
|
|
|
* Otherwise, all tables defined in the schema are generated.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function dropSchema($schema, $table = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generate a column schema string
|
|
|
|
*
|
|
|
|
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
|
|
|
|
* where options can be 'default', 'length', or 'key'.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function buildColumn($column) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Format indexes for create table
|
|
|
|
*
|
|
|
|
* @param array $indexes
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function buildIndex($indexes) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2007-09-15 20:34:27 +00:00
|
|
|
?>
|