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-01-20 07:46:14 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
2006-01-12 02:10:47 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2006-01-20 07:46:14 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
2006-01-12 02:10:47 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DboSource
|
|
|
|
*
|
|
|
|
* Creates DBO-descendant objects from a given db connection configuration
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.datasources
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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-02-01 13:26:23 +00:00
|
|
|
var $description = "Database Data Source";
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $__bypass = false;
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2006-02-01 13:26:23 +00:00
|
|
|
* @var array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
|
|
|
var $__assocJoins = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __construct($config = null)
|
|
|
|
{
|
|
|
|
$this->debug = DEBUG > 0;
|
|
|
|
$this->fullDebug = DEBUG > 1;
|
|
|
|
parent::__construct($config);
|
|
|
|
return $this->connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function value ($data, $column = null)
|
|
|
|
{
|
|
|
|
if (is_array($data))
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
foreach ($data as $key => $item)
|
|
|
|
{
|
|
|
|
$out[$key] = $this->value($item);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-12 02:10:47 +00:00
|
|
|
function sources ()
|
|
|
|
{
|
|
|
|
return array_map('strtolower', $this->listSources());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function rawQuery ($sql)
|
|
|
|
{
|
|
|
|
$this->took = $this->error = $this->numRows = false;
|
|
|
|
return $this->execute($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function execute($sql)
|
|
|
|
{
|
|
|
|
$t = getMicrotime();
|
2006-01-15 12:18:57 +00:00
|
|
|
$this->_result = $this->_execute($sql);
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
$this->affected = $this->lastAffected();
|
|
|
|
$this->took = round((getMicrotime() - $t) * 1000, 0);
|
|
|
|
$this->error = $this->lastError();
|
|
|
|
$this->numRows = $this->lastNumRows($this->_result);
|
|
|
|
$this->logQuery($sql);
|
|
|
|
|
|
|
|
if ($this->error)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single row of results from the _last_ SQL query.
|
|
|
|
*
|
|
|
|
* @param resource $res
|
|
|
|
* @return array A single row of results
|
|
|
|
*/
|
|
|
|
function fetchArray ($assoc=false)
|
|
|
|
{
|
|
|
|
if ($assoc === false)
|
|
|
|
{
|
|
|
|
return $this->fetchRow();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->fetchRow($assoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single row of results for a _given_ SQL query.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return array A single row of results
|
|
|
|
*/
|
|
|
|
function one ($sql)
|
|
|
|
{
|
2006-01-12 22:06:11 +00:00
|
|
|
if ($this->execute($sql))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
return $this->fetchArray();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
* @return array Array of resultset rows, or false if no rows matched
|
|
|
|
*/
|
|
|
|
function fetchAll ($sql)
|
|
|
|
{
|
|
|
|
if($this->execute($sql))
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
while ($item = $this->fetchArray(null, true))
|
|
|
|
{
|
|
|
|
$out[] = $item;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function field ($name, $sql)
|
|
|
|
{
|
|
|
|
$data = $this->one($sql);
|
|
|
|
if (empty($data[$name]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $data[$name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if it's connected to the database
|
|
|
|
*
|
|
|
|
* @return boolean True if the database is connected, else false
|
|
|
|
*/
|
|
|
|
function isConnected()
|
|
|
|
{
|
|
|
|
return $this->connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function showLog($sorted=false)
|
|
|
|
{
|
|
|
|
$log = $sorted?
|
|
|
|
sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC):
|
|
|
|
$this->_queriesLog;
|
|
|
|
|
2006-02-01 13:26:23 +00:00
|
|
|
if($this->_queriesCnt > 1)
|
2006-01-13 05:56:04 +00:00
|
|
|
{
|
|
|
|
$text = 'queries';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$text = 'query';
|
|
|
|
}
|
2006-02-08 15:25:34 +00:00
|
|
|
print("<table border=\"1\">\n<tr><th colspan=\"7\">{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</th></tr>\n");
|
2006-01-12 02:10:47 +00:00
|
|
|
print("<tr><td>Nr</td><td>Query</td><td>Error</td><td>Affected</td><td>Num. rows</td><td>Took (ms)</td></tr>\n");
|
|
|
|
|
2006-02-01 13:26:23 +00:00
|
|
|
foreach($log as $k => $i)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-08 15:25:34 +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-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print("</table>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log given SQL query.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
*/
|
|
|
|
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; // shouldn't we be logging errors somehow?
|
|
|
|
// TODO: Add hook to error log
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function showQuery($sql)
|
|
|
|
{
|
|
|
|
$error = $this->error;
|
2006-01-12 03:56:59 +00:00
|
|
|
|
2006-02-01 13:26:23 +00:00
|
|
|
if (strlen($sql) > 200 && !$this->fullDebug)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$sql = substr($sql, 0, 200) .'[...]';
|
|
|
|
}
|
2006-01-12 03:56:59 +00:00
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
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>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 $fields
|
|
|
|
* @param unknown_type $values
|
2006-02-01 13:26:23 +00:00
|
|
|
* @return boolean Success
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-01-12 02:10:47 +00:00
|
|
|
function create(&$model, $fields = null, $values = null)
|
|
|
|
{
|
|
|
|
if ($fields == null)
|
|
|
|
{
|
|
|
|
unset($fields, $values);
|
|
|
|
$fields = array_keys($model->data);
|
|
|
|
$values = array_values($model->data);
|
|
|
|
}
|
|
|
|
|
2006-02-08 15:25:34 +00:00
|
|
|
foreach ($fields as $field)
|
|
|
|
{
|
|
|
|
$fieldInsert[] = $this->name($field);
|
|
|
|
}
|
|
|
|
|
2006-01-12 16:55:46 +00:00
|
|
|
foreach ($values as $value)
|
|
|
|
{
|
|
|
|
$valueInsert[] = $this->value($value);
|
|
|
|
}
|
|
|
|
|
2006-02-08 15:25:34 +00:00
|
|
|
if($this->execute('INSERT INTO '.$model->table.' ('.join(',', $fieldInsert).') VALUES ('.join(',', $valueInsert).')'))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param array $queryData
|
|
|
|
* @param integer $recursive Number of levels of association
|
2006-01-17 17:52:23 +00:00
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-01-18 06:46:28 +00:00
|
|
|
function read (&$model, $queryData = array(), $recursive = null)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$this->__scrubQueryData($queryData);
|
|
|
|
$null = null;
|
|
|
|
$array = array();
|
|
|
|
$linkedModels = array();
|
2006-01-18 04:38:21 +00:00
|
|
|
$this->__bypass = false;
|
2006-01-20 07:46:14 +00:00
|
|
|
$this->__assocJoins = null;
|
2006-01-18 06:46:28 +00:00
|
|
|
if(!is_null($recursive))
|
|
|
|
{
|
2006-02-04 07:33:20 +00:00
|
|
|
$_recursive = $model->recursive;
|
2006-01-18 06:46:28 +00:00
|
|
|
$model->recursive = $recursive;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-01-17 17:52:23 +00:00
|
|
|
if(!empty($queryData['fields']))
|
|
|
|
{
|
|
|
|
$this->__bypass = true;
|
|
|
|
}
|
|
|
|
|
2006-01-18 06:46:28 +00:00
|
|
|
if ($model->recursive > 0)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
foreach($model->__associations as $type)
|
|
|
|
{
|
|
|
|
foreach($model->{$type} as $assoc => $assocData)
|
|
|
|
{
|
|
|
|
$linkModel =& $model->{$assocData['className']};
|
2006-01-20 07:46:14 +00:00
|
|
|
if($model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany')
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-16 21:34:46 +00:00
|
|
|
if (true === $this->generateSelfAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null))
|
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
$linkedModels[] = $type.'/'.$assoc;
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null))
|
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
$linkedModels[] = $type.'/'.$assoc;
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build final query SQL
|
|
|
|
$query = $this->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null);
|
|
|
|
$resultSet = $this->fetchAll($query);
|
|
|
|
|
2006-01-18 06:46:28 +00:00
|
|
|
if ($model->recursive > 0)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
foreach($model->__associations as $type)
|
|
|
|
{
|
|
|
|
foreach($model->{$type} as $assoc => $assocData)
|
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
if (!in_array($type.'/'.$assoc, $linkedModels))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$linkModel =& $model->{$assocData['className']};
|
2006-02-16 09:29:28 +00:00
|
|
|
$this->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive);
|
|
|
|
} else {
|
|
|
|
// Fetch recursively on belongsTo and hasOne
|
|
|
|
if ($model->recursive > 1)
|
2006-01-18 06:46:28 +00:00
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
//$this->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1);
|
2006-01-18 06:46:28 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-04 07:33:20 +00:00
|
|
|
|
|
|
|
if(!is_null($recursive))
|
|
|
|
{
|
|
|
|
$model->recursive = $_recursive;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
return $resultSet;
|
|
|
|
}
|
|
|
|
|
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-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-02-16 09:29:28 +00:00
|
|
|
function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
|
|
|
if ($query)
|
|
|
|
{
|
|
|
|
foreach ($resultSet as $i => $row)
|
|
|
|
{
|
|
|
|
$q = $this->insertQueryData($query, $resultSet, $association, $assocData, $model, $linkModel, $i);
|
|
|
|
$fetch = $this->fetchAll($q);
|
|
|
|
|
|
|
|
if (!empty($fetch) && is_array($fetch))
|
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
if ($recursive > 0)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
foreach($linkModel->__associations as $type1)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
if ($recursive > 1)
|
|
|
|
{
|
|
|
|
foreach($linkModel->{$type1} as $assoc1 => $assocData1)
|
|
|
|
{
|
|
|
|
$deepModel =& $linkModel->{$assocData1['className']};
|
|
|
|
if ($deepModel->name != $model->name)
|
|
|
|
{
|
|
|
|
$this->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
$this->__mergeAssociation($resultSet[$i], $fetch, $association, $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function __mergeAssociation(&$data, $merge, $association, $type)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
$data[$association] = $merge[0][$association];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach ($merge as $i => $row)
|
|
|
|
{
|
|
|
|
if (count($row) == 1)
|
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$data[$association][] = $row[$association];
|
2006-02-16 09:29:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$tmp = array_merge($row[$association], $row);
|
|
|
|
unset($tmp[$association]);
|
|
|
|
$data[$association][] = $tmp;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-16 21:34:46 +00:00
|
|
|
function generateSelfAssociationQuery(&$model, &$linkModel, $type, $association = null, $assocData = array(), &$queryData, $external = false, &$resultSet)
|
|
|
|
{
|
|
|
|
$alias = $association;
|
|
|
|
if(!isset($queryData['selfJoin']))
|
|
|
|
{
|
|
|
|
$queryData['selfJoin'] = array();
|
|
|
|
|
|
|
|
$sql = 'SELECT ' . join(', ', $this->fields($model, $model->name, $queryData['fields'])). ', ';
|
|
|
|
$sql .= join(', ', $this->fields($linkModel, $alias, ''));
|
|
|
|
$sql .= ' FROM '.$model->table.' AS ' . $model->name;
|
|
|
|
$sql .= ' LEFT JOIN '.$linkModel->table.' AS ' . $alias;
|
|
|
|
$sql .= ' ON ';
|
|
|
|
$sql .= $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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-17 17:52:23 +00:00
|
|
|
if(isset($this->__assocJoins))
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
|
|
|
$replace = ', ';
|
2006-01-17 17:52:23 +00:00
|
|
|
$replace .= join(', ', $this->__assocJoins['fields']);
|
2006-01-16 21:34:46 +00:00
|
|
|
$replace .= ' FROM';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$replace = 'FROM';
|
|
|
|
}
|
|
|
|
$sql = $queryData['selfJoin'][0];
|
|
|
|
$sql .= ' ' . join(' ', $queryData['joins']);
|
|
|
|
$sql .= $this->conditions($queryData['conditions']).' '.$this->order($queryData['order']);
|
|
|
|
$sql .= ' '.$this->limit($queryData['limit']);
|
|
|
|
$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-01-12 02:10:47 +00:00
|
|
|
function generateAssociationQuery(&$model, &$linkModel, $type, $association = null, $assocData = array(), &$queryData, $external = false, &$resultSet)
|
|
|
|
{
|
|
|
|
$this->__scrubQueryData($queryData);
|
2006-01-17 17:52:23 +00:00
|
|
|
$joinedOnSelf = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
if ($linkModel == null)
|
|
|
|
{
|
2006-01-16 21:34:46 +00:00
|
|
|
if(array_key_exists('selfJoin', $queryData))
|
|
|
|
{
|
|
|
|
return $this->generateSelfAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-17 20:56:45 +00:00
|
|
|
if(isset($this->__assocJoins))
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
|
|
|
$joinFields = ', ';
|
2006-01-17 17:52:23 +00:00
|
|
|
$joinFields .= join(', ', $this->__assocJoins['fields']);
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$joinFields = null;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
// Generates primary query
|
2006-01-16 21:34:46 +00:00
|
|
|
$sql = 'SELECT ' . join(', ', $this->fields($model, $model->name, $queryData['fields'])) .$joinFields. ' FROM ';
|
2006-01-12 03:56:59 +00:00
|
|
|
$sql .= $this->name($model->table).' AS ';
|
2006-01-12 02:10:47 +00:00
|
|
|
$sql .= $this->name($model->name).' ' . join(' ', $queryData['joins']).' ';
|
|
|
|
$sql .= $this->conditions($queryData['conditions']).' '.$this->order($queryData['order']);
|
|
|
|
$sql .= ' '.$this->limit($queryData['limit']);
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
$alias = $association;
|
|
|
|
if($model->name == $linkModel->name)
|
|
|
|
{
|
2006-01-16 21:34:46 +00:00
|
|
|
$joinedOnSelf = true;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
switch ($type)
|
|
|
|
{
|
|
|
|
case 'hasOne':
|
|
|
|
if ($external)
|
|
|
|
{
|
2006-01-22 05:12:28 +00:00
|
|
|
if (isset($assocData['finderQuery']))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
return $assocData['finderQuery'];
|
|
|
|
}
|
2006-01-22 05:12:28 +00:00
|
|
|
if(!isset($assocData['fields']))
|
|
|
|
{
|
|
|
|
$assocData['fields'] = '';
|
|
|
|
}
|
2006-01-15 12:18:57 +00:00
|
|
|
$sql = 'SELECT '.join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM '.$this->name($linkModel->table).' AS '.$alias;
|
2006-01-12 02:10:47 +00:00
|
|
|
$conditions = $queryData['conditions'];
|
|
|
|
$condition = $model->escapeField($assocData['foreignKey']);
|
|
|
|
$condition .= '={$__cake_foreignKey__$}';
|
|
|
|
if (is_array($conditions))
|
|
|
|
{
|
|
|
|
$conditions[] = $condition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (trim($conditions) != '')
|
|
|
|
{
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
$conditions .= $condition;
|
|
|
|
}
|
|
|
|
$sql .= $this->conditions($queryData['conditions']) . $this->order($queryData['order']);
|
|
|
|
$sql .= $this->limit($queryData['limit']);
|
2006-01-17 17:52:23 +00:00
|
|
|
return $sql;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-01-17 17:52:23 +00:00
|
|
|
else if($joinedOnSelf != true)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-17 17:52:23 +00:00
|
|
|
if(!isset($assocData['fields']))
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
2006-01-17 17:52:23 +00:00
|
|
|
$assocData['fields'] = '';
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
if($this->__bypass === false)
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
|
|
|
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
2006-01-17 20:56:45 +00:00
|
|
|
$this->__assocJoins['fields'][] = $fields;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->__assocJoins = null;
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-17 17:52:23 +00:00
|
|
|
$sql = ' LEFT JOIN '.$this->name($linkModel->table);
|
|
|
|
$sql .= ' AS '.$this->name($alias).' ON '.$this->name($alias).'.';
|
|
|
|
$sql .= $this->name($assocData['foreignKey']).'='.$model->escapeField($model->primaryKey);
|
|
|
|
$sql .= $this->order($assocData['order']);
|
2006-02-04 07:33:20 +00:00
|
|
|
|
2006-02-07 02:19:53 +00:00
|
|
|
if (isset($assocData['conditions']) && !empty($assocData['conditions']))
|
2006-02-04 07:33:20 +00:00
|
|
|
{
|
|
|
|
if(is_array($queryData['conditions']))
|
|
|
|
{
|
|
|
|
$queryData['conditions'] = array_merge($assocData['conditions'], $queryData['conditions']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$queryData['conditions'] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-12 18:34:16 +00:00
|
|
|
if (!in_array($sql, $queryData['joins']))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$queryData['joins'][] = $sql;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'belongsTo':
|
|
|
|
if ($external)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$sql = 'SELECT * FROM '.$this->name($linkModel->table).' AS '.$this->name($alias);
|
2006-02-16 09:29:28 +00:00
|
|
|
$conditions = $assocData['conditions'];
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-02-16 09:29:28 +00:00
|
|
|
$condition = $linkModel->escapeField($linkModel->primaryKey);
|
2006-01-12 02:10:47 +00:00
|
|
|
$condition .= '={$__cake_id__$}';
|
2006-02-16 09:29:28 +00:00
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
if (is_array($conditions))
|
|
|
|
{
|
|
|
|
$conditions[] = $condition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (trim($conditions) != '')
|
|
|
|
{
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
$conditions .= $condition;
|
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
$sql .= $this->conditions($conditions) . $this->order($assocData['order']);
|
|
|
|
if (isset($assocData['limit']))
|
|
|
|
{
|
|
|
|
$sql .= $this->limit($assocData['limit']);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
return $sql;
|
|
|
|
}
|
2006-01-17 17:52:23 +00:00
|
|
|
else if($joinedOnSelf != true)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-17 17:52:23 +00:00
|
|
|
if(!isset($assocData['fields']))
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
2006-01-17 17:52:23 +00:00
|
|
|
$assocData['fields'] = '';
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-17 20:56:45 +00:00
|
|
|
if($this->__bypass === false)
|
2006-01-16 21:34:46 +00:00
|
|
|
{
|
|
|
|
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
2006-01-17 20:56:45 +00:00
|
|
|
$this->__assocJoins['fields'][] = $fields;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->__assocJoins = null;
|
2006-01-16 21:34:46 +00:00
|
|
|
}
|
2006-01-17 17:52:23 +00:00
|
|
|
$sql = ' LEFT JOIN '.$this->name($linkModel->table);
|
|
|
|
$sql .= ' AS ' . $this->name($alias) . ' ON ';
|
|
|
|
$sql .= $this->name($model->name).'.'.$this->name($assocData['foreignKey']);
|
|
|
|
$sql .= '='.$this->name($alias).'.'.$this->name($linkModel->primaryKey);
|
2006-02-04 07:33:20 +00:00
|
|
|
|
2006-02-07 02:19:53 +00:00
|
|
|
if (isset($assocData['conditions']) && !empty($assocData['conditions']))
|
2006-02-04 07:33:20 +00:00
|
|
|
{
|
|
|
|
if(is_array($queryData['conditions']))
|
|
|
|
{
|
|
|
|
$queryData['conditions'] = array_merge($assocData['conditions'], $queryData['conditions']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$queryData['conditions'] = $assocData['conditions'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-12 02:10:47 +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
|
|
|
|
{
|
|
|
|
$conditions = $assocData['conditions'];
|
2006-01-15 12:18:57 +00:00
|
|
|
$sql = 'SELECT '.join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM '.$this->name($linkModel->table).' AS '. $this->name($alias);
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
if (is_array($conditions))
|
|
|
|
{
|
2006-01-20 10:25:29 +00:00
|
|
|
$conditions[$alias.'.'.$assocData['foreignKey']] = '{$__cake_id__$}';
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-20 10:25:29 +00:00
|
|
|
$cond = $this->name($alias).'.'.$this->name($assocData['foreignKey']);
|
|
|
|
$cond .= '={$__cake_id__$}';
|
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
if (trim($conditions) != '')
|
|
|
|
{
|
|
|
|
$conditions .= ' AND ';
|
|
|
|
}
|
|
|
|
$conditions .= $cond;
|
|
|
|
}
|
|
|
|
$sql .= $this->conditions($conditions);
|
|
|
|
$sql .= $this->order($assocData['order']);
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
break;
|
|
|
|
case 'hasAndBelongsToMany':
|
|
|
|
if(isset($assocData['finderQuery']) && $assocData['finderQuery'] != null)
|
|
|
|
{
|
|
|
|
$sql = $assocData['finderQuery'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$joinTbl = $this->name($assocData['joinTable']);
|
|
|
|
|
2006-01-15 12:18:57 +00:00
|
|
|
$sql = 'SELECT '.join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
|
|
|
|
$sql .= ' FROM '.$this->name($linkModel->table).' AS '.$this->name($alias);
|
2006-01-12 02:10:47 +00:00
|
|
|
$sql .= ' JOIN '.$joinTbl.' ON '.$joinTbl;
|
|
|
|
$sql .= '.'.$this->name($assocData['foreignKey']).'={$__cake_id__$}';
|
|
|
|
$sql .= ' AND '.$joinTbl.'.'.$this->name($assocData['associationForeignKey']);
|
2006-01-15 12:18:57 +00:00
|
|
|
$sql .= ' = '.$this->name($alias).'.'.$this->name($linkModel->primaryKey);
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
$sql .= $this->conditions($assocData['conditions']);
|
|
|
|
$sql .= $this->order($assocData['order']);
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-01-12 02:10:47 +00:00
|
|
|
function update (&$model, $fields = null, $values = null)
|
|
|
|
{
|
|
|
|
$updates = array();
|
|
|
|
foreach (array_combine($fields, $values) as $field => $value)
|
|
|
|
{
|
|
|
|
$updates[] = $this->name($field).'='.$this->value($value);
|
|
|
|
}
|
|
|
|
|
2006-01-17 05:30:08 +00:00
|
|
|
$sql = 'UPDATE '.$this->name($model->table);
|
2006-01-12 02:10:47 +00:00
|
|
|
$sql .= ' SET '.join(',', $updates);
|
2006-01-17 05:30:08 +00:00
|
|
|
$sql .= ' WHERE '.$this->name($model->primaryKey).'='.$this->value($model->getID());
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
return $this->execute($sql);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return boolean Success
|
2006-01-17 17:52:23 +00:00
|
|
|
*/
|
2006-01-12 02:10:47 +00:00
|
|
|
function delete (&$model, $id = null)
|
|
|
|
{
|
|
|
|
$_id = $model->id;
|
|
|
|
if ($id != null)
|
|
|
|
{
|
|
|
|
$model->id = $id;
|
|
|
|
}
|
|
|
|
if (!is_array($model->id))
|
|
|
|
{
|
|
|
|
$model->id = array($model->id);
|
|
|
|
}
|
|
|
|
foreach ($model->id as $id)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$result = $this->execute('DELETE FROM '.$this->name($model->table).' WHERE '.$this->name($model->primaryKey).'='.$this->value($id));
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
if ($result)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-01-12 02:10:47 +00:00
|
|
|
function resolveKey($model, $key, $assoc = null)
|
|
|
|
{
|
|
|
|
if ($assoc == null)
|
|
|
|
{
|
|
|
|
$assoc = $model->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strpos('.', $key))
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
return $this->name($model->table).'.'.$this->name($key);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
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 $field
|
|
|
|
*/
|
2006-01-12 02:10:47 +00:00
|
|
|
function getColumnType (&$model, $field)
|
|
|
|
{
|
|
|
|
$columns = $model->loadInfo();
|
|
|
|
}
|
|
|
|
|
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-01-12 02:10:47 +00:00
|
|
|
function __scrubQueryData(&$data)
|
|
|
|
{
|
|
|
|
if (!isset($data['conditions']))
|
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$data['conditions'] = ' 1 = 1 ';
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-01-17 05:13:38 +00:00
|
|
|
if (!isset($data['fields']))
|
|
|
|
{
|
|
|
|
$data['fields'] = '';
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
if (!isset($data['joins']))
|
|
|
|
{
|
|
|
|
$data['joins'] = array();
|
|
|
|
}
|
|
|
|
if (!isset($data['order']))
|
|
|
|
{
|
|
|
|
$data['order'] = '';
|
|
|
|
}
|
|
|
|
if (!isset($data['limit']))
|
|
|
|
{
|
|
|
|
$data['limit'] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-15 12:18:57 +00:00
|
|
|
function fields (&$model, $alias, $fields)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
if (is_array($fields))
|
|
|
|
{
|
2006-01-15 12:18:57 +00:00
|
|
|
$fields = $fields;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($fields != null)
|
|
|
|
{
|
|
|
|
if (strpos($fields, ','))
|
|
|
|
{
|
|
|
|
$fields = explode(',', $fields);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$fields = array_map('trim', $fields);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-15 12:18:57 +00:00
|
|
|
foreach ($model->_tableInfo->value as $field)
|
|
|
|
{
|
|
|
|
$fields[]= $field['name'];
|
|
|
|
}
|
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-20 09:37:15 +00:00
|
|
|
$count = count($fields);
|
2006-02-16 09:29:28 +00:00
|
|
|
if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
for ($i = 0; $i < $count; $i++)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-21 00:09:27 +00:00
|
|
|
$dot = strrpos($fields[$i], '.');
|
|
|
|
if ($dot === false)
|
|
|
|
{
|
|
|
|
$fields[$i] = $this->name($alias).'.'.$this->name($fields[$i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$build = explode('.',$fields[$i]);
|
|
|
|
$fields[$i] = $this->name($build[0]).'.'.$this->name($build[1]);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function conditions ($conditions)
|
|
|
|
{
|
|
|
|
$rt = '';
|
2006-01-13 05:56:04 +00:00
|
|
|
if (!is_array($conditions) && (!strpos(low($conditions), 'where') || strpos(low($conditions), 'where') === 0))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$rt = ' WHERE ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($conditions))
|
|
|
|
{
|
|
|
|
if (trim($conditions) == '')
|
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$conditions = ' 1 = 1';
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
return $rt.$conditions;
|
|
|
|
}
|
|
|
|
elseif (is_array($conditions))
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
foreach ($conditions as $key => $value)
|
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
if (is_array($value))
|
|
|
|
{
|
|
|
|
$data = $key . ' IN (';
|
|
|
|
foreach ($value as $valElement)
|
|
|
|
{
|
|
|
|
$data .= $this->value($valElement) . ', ';
|
|
|
|
}
|
|
|
|
$data[strlen($data)-2] = ')';
|
|
|
|
}
|
|
|
|
elseif (preg_match('/(?P<expression>LIKE\\x20|=\\x20|>\\x20|<\\x20|<=\\x20|>=\\x20|<>\\x20)(?P<value>.*)/i', $value, $match))
|
|
|
|
{
|
|
|
|
$data = $this->name($key) . ' '.$match['expression'].' '. $this->value($match['value']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (($value != '{$__cake_id__$}') && ($value != '{$__cake_foreignKey__$}'))
|
2006-01-20 10:25:29 +00:00
|
|
|
{
|
|
|
|
$value = $this->value($value);
|
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
$data = $this->name($key) . '=';
|
2006-01-20 10:25:29 +00:00
|
|
|
|
2006-01-13 05:56:04 +00:00
|
|
|
if ($value === null)
|
|
|
|
{
|
|
|
|
$data .= 'null';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-20 10:25:29 +00:00
|
|
|
$data .= $value;
|
2006-01-13 05:56:04 +00:00
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
$out[] = $data;
|
|
|
|
}
|
|
|
|
return ' WHERE ' . join(' AND ', $out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $rt.' 1 ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-17 20:56:45 +00:00
|
|
|
/**
|
2006-02-01 13:26:23 +00:00
|
|
|
* To be overridden in subclasses.
|
2006-01-17 17:52:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-01-12 02:10:47 +00:00
|
|
|
function limit ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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-02-17 10:12:15 +00:00
|
|
|
function order ($keys, $direction = 'ASC')
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-16 09:29:28 +00:00
|
|
|
if (empty($keys))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
|
|
|
|
if(is_array($keys))
|
|
|
|
{
|
|
|
|
foreach($keys as $key => $value)
|
|
|
|
{
|
|
|
|
if(is_numeric($key))
|
|
|
|
{
|
|
|
|
$key = $value;
|
2006-02-17 10:12:15 +00:00
|
|
|
$value = ' '.$direction;
|
2006-02-16 09:29:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$value= ' '.$value;
|
|
|
|
}
|
|
|
|
$order[] = $this->name($key).$value;
|
|
|
|
}
|
|
|
|
return ' ORDER BY '.join(',', $order);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (preg_match('/(?P<direction>\\x20ASC|\\x20DESC)/', $keys, $match))
|
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$direction = $match['direction'];
|
2006-02-16 09:29:28 +00:00
|
|
|
$keys = preg_replace('/'.$match['direction'].'/', '', $keys);
|
|
|
|
}
|
2006-02-17 10:12:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$direction = ' '.$direction;
|
|
|
|
}
|
|
|
|
return ' ORDER BY '.$this->name($keys).$direction;
|
2006-02-16 09:29:28 +00:00
|
|
|
}
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
function close ()
|
|
|
|
{
|
|
|
|
if ($this->fullDebug)
|
|
|
|
{
|
|
|
|
$this->showLog();
|
|
|
|
}
|
|
|
|
$this->_conn = NULL;
|
|
|
|
$this->connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor. Closes connection to the database.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param string $table Name of table to look in
|
|
|
|
* @param string $sql SQL WHERE clause (condition only, not the "WHERE" part)
|
|
|
|
* @return boolean True if the table has a matching record, else false
|
|
|
|
*/
|
|
|
|
function hasAny($table, $sql)
|
|
|
|
{
|
|
|
|
$out = $this->one("SELECT COUNT(*) AS count FROM {$table}".($sql? " WHERE {$sql}":""));
|
|
|
|
return is_array($out)? $out[0]['count']: false;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-01-16 21:34:46 +00:00
|
|
|
?>
|