mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Refactoring DboSource. Model array conditions must now have comparison operators on the left side, fixes misc bugs, updated core to reflect changes
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7075 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
1aa6e5c4f4
commit
39feb3f7c1
17 changed files with 453 additions and 394 deletions
|
@ -254,14 +254,6 @@ class AuthComponent extends Object {
|
|||
* @access public
|
||||
*/
|
||||
function startup(&$controller) {
|
||||
if (!$this->loginError) {
|
||||
$this->loginError = __('Login failed. Invalid username or password.', true);
|
||||
}
|
||||
|
||||
if (!$this->authError) {
|
||||
$this->authError = __('You are not authorized to access that location.', true);
|
||||
}
|
||||
|
||||
if (strtolower($controller->name) == 'app' || (strtolower($controller->name) == 'tests' && Configure::read() > 0)) {
|
||||
return;
|
||||
}
|
||||
|
@ -277,6 +269,7 @@ class AuthComponent extends Object {
|
|||
$params = $controller->params;
|
||||
$keys = array('pass', 'named', 'controller', 'action', 'plugin');
|
||||
$url = array();
|
||||
|
||||
foreach($keys as $key) {
|
||||
if (!empty($params[$key])) {
|
||||
if (is_array($params[$key])) {
|
||||
|
@ -293,6 +286,7 @@ class AuthComponent extends Object {
|
|||
}
|
||||
$url = Router::normalize($url);
|
||||
$loginAction = Router::normalize($this->loginAction);
|
||||
|
||||
if ($loginAction != $url && ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions))) {
|
||||
return false;
|
||||
}
|
||||
|
@ -336,42 +330,42 @@ class AuthComponent extends Object {
|
|||
}
|
||||
}
|
||||
|
||||
if ($this->authorize) {
|
||||
extract($this->__authType());
|
||||
switch ($type) {
|
||||
case 'controller':
|
||||
$this->object =& $controller;
|
||||
break;
|
||||
case 'crud':
|
||||
case 'actions':
|
||||
if (isset($controller->Acl)) {
|
||||
$this->Acl =& $controller->Acl;
|
||||
} else {
|
||||
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING);
|
||||
}
|
||||
break;
|
||||
case 'model':
|
||||
if (!isset($object)) {
|
||||
if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) {
|
||||
$object = $controller->modelClass;
|
||||
} elseif (!empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]})) {
|
||||
$object = $controller->uses[0];
|
||||
}
|
||||
}
|
||||
$type = array('model' => $object);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($this->isAuthorized($type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->Session->setFlash($this->authError, 'default', array(), 'auth');
|
||||
$controller->redirect($controller->referer(), null, true);
|
||||
return false;
|
||||
} else {
|
||||
if (!$this->authorize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
extract($this->__authType());
|
||||
switch ($type) {
|
||||
case 'controller':
|
||||
$this->object =& $controller;
|
||||
break;
|
||||
case 'crud':
|
||||
case 'actions':
|
||||
if (isset($controller->Acl)) {
|
||||
$this->Acl =& $controller->Acl;
|
||||
} else {
|
||||
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING);
|
||||
}
|
||||
break;
|
||||
case 'model':
|
||||
if (!isset($object)) {
|
||||
if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) {
|
||||
$object = $controller->modelClass;
|
||||
} elseif (!empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]})) {
|
||||
$object = $controller->uses[0];
|
||||
}
|
||||
}
|
||||
$type = array('model' => $object);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($this->isAuthorized($type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->Session->setFlash($this->authError, 'default', array(), 'auth');
|
||||
$controller->redirect($controller->referer(), null, true);
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Attempts to introspect the correct values for object properties including
|
||||
|
@ -385,14 +379,20 @@ class AuthComponent extends Object {
|
|||
trigger_error(__("Could not find \$userModel. Please set AuthComponent::\$userModel in beforeFilter().", true), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
if (empty($this->loginAction)) {
|
||||
$this->loginAction = Router::normalize(array('controller'=> Inflector::underscore(Inflector::pluralize($this->userModel)), 'action'=>'login'));
|
||||
}
|
||||
if (empty($this->sessionKey)) {
|
||||
$this->sessionKey = 'Auth.' . $this->userModel;
|
||||
}
|
||||
if (empty($this->logoutRedirect)) {
|
||||
$this->logoutRedirect = $this->loginAction;
|
||||
$defaults = array(
|
||||
'loginAction' => Router::normalize(array(
|
||||
'controller'=> Inflector::underscore(Inflector::pluralize($this->userModel)),
|
||||
'action' => 'login'
|
||||
)),
|
||||
'sessionKey' => 'Auth.' . $this->userModel,
|
||||
'logoutRedirect' => $this->loginAction,
|
||||
'loginError' => __('Login failed. Invalid username or password.', true),
|
||||
'authError' => __('You are not authorized to access that location.', true)
|
||||
);
|
||||
foreach ($defaults as $key => $value) {
|
||||
if (empty($this->{$key})) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -752,8 +752,8 @@ class AuthComponent extends Object {
|
|||
return false;
|
||||
}
|
||||
$find = array(
|
||||
$this->userModel.'.'.$this->fields['username'] => '= ' . $user[$this->userModel . '.' . $this->fields['username']],
|
||||
$this->userModel.'.'.$this->fields['password'] => '= ' . $user[$this->userModel . '.' . $this->fields['password']]
|
||||
$this->userModel.'.'.$this->fields['username'] => $user[$this->userModel . '.' . $this->fields['username']],
|
||||
$this->userModel.'.'.$this->fields['password'] => $user[$this->userModel . '.' . $this->fields['password']]
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -280,7 +280,11 @@ class BehaviorCollection extends Object {
|
|||
$this->{$name} =& new $class;
|
||||
}
|
||||
} elseif (isset($this->{$name}->settings) && isset($this->{$name}->settings[$this->modelName])) {
|
||||
$config = array_merge($this->{$name}->settings[$this->modelName], $config);
|
||||
if ($config !== null && $config !== false) {
|
||||
$config = array_merge($this->{$name}->settings[$this->modelName], $config);
|
||||
} else {
|
||||
$config = array();
|
||||
}
|
||||
}
|
||||
$this->{$name}->setup(ClassRegistry::getObject($this->modelName), $config);
|
||||
|
||||
|
|
|
@ -91,14 +91,13 @@ class TranslateBehavior extends ModelBehavior {
|
|||
'alias' => $RuntimeModel->alias,
|
||||
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
||||
'conditions' => array(
|
||||
$model->alias.'.id' => '{$__cakeIdentifier['.$RuntimeModel->alias.'.foreign_key]__$}',
|
||||
$model->alias.'.id' => $db->identifier($RuntimeModel->alias.'.foreign_key'),
|
||||
$RuntimeModel->alias.'.model' => $model->name,
|
||||
$RuntimeModel->alias.'.locale' => $locale
|
||||
)
|
||||
);
|
||||
return $query;
|
||||
}
|
||||
|
||||
$autoFields = false;
|
||||
|
||||
if (empty($query['fields'])) {
|
||||
|
@ -148,7 +147,7 @@ class TranslateBehavior extends ModelBehavior {
|
|||
'alias' => 'I18n__'.$field.'__'.$_locale,
|
||||
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
||||
'conditions' => array(
|
||||
$model->alias.'.id' => '{$__cakeIdentifier[I18n__'.$field.'__'.$_locale.'.foreign_key]__$}',
|
||||
$model->alias.'.id' => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
|
||||
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
|
||||
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field,
|
||||
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
|
||||
|
@ -162,7 +161,7 @@ class TranslateBehavior extends ModelBehavior {
|
|||
'alias' => 'I18n__'.$field,
|
||||
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
||||
'conditions' => array(
|
||||
$model->alias.'.id' => '{$__cakeIdentifier[I18n__'.$field.'.foreign_key]__$}',
|
||||
$model->alias.'.id' => $db->identifier("I18n__{$field}.foreign_key"),
|
||||
'I18n__'.$field.'.model' => $model->name,
|
||||
'I18n__'.$field.'.'.$RuntimeModel->displayField => $field
|
||||
)
|
||||
|
|
|
@ -110,7 +110,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
if (is_string($scope)) {
|
||||
$scope = array($scope);
|
||||
}
|
||||
$scope[][$model->alias . '.' . $left] = 'BETWEEN ' . ($data[$left] + 1) . ' AND ' . ($data[$right] - 1);
|
||||
$scope[]["{$model->alias}.{$left} BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
|
||||
$model->deleteAll($scope);
|
||||
}
|
||||
$this->__sync($model, $diff, '-', '> ' . $data[$right]);
|
||||
|
@ -246,6 +246,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
extract (array_merge(array('id' => null), $id));
|
||||
}
|
||||
$overrideRecursive = $recursive;
|
||||
|
||||
if ($id === null && $model->id) {
|
||||
$id = $model->id;
|
||||
} elseif (!$id) {
|
||||
|
@ -253,6 +254,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
}
|
||||
$name = $model->alias;
|
||||
extract($this->settings[$model->alias]);
|
||||
|
||||
if (!is_null($overrideRecursive)) {
|
||||
$recursive = $overrideRecursive;
|
||||
}
|
||||
|
@ -272,7 +274,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
} else {
|
||||
return array();
|
||||
}
|
||||
$constraint = array($scope, $model->escapeField($right) . '< ' . $item[$right], $model->escapeField($left) => '> ' . $item[$left]);
|
||||
$constraint = array($scope, $model->escapeField($right) . ' <' . $item[$right], $model->escapeField($left) . ' >' => $item[$left]);
|
||||
}
|
||||
return $model->find('all', array('conditions' => $constraint, 'fields' => $fields, 'order' => $order, 'limit' => $limit, 'page' => $page, 'recursive' => $recursive));
|
||||
}
|
||||
|
@ -395,7 +397,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
}
|
||||
$item = $result[0];
|
||||
$results = $model->find('all', array(
|
||||
'conditions' => array($scope, $model->escapeField($left) => '<= ' . $item[$left], $model->escapeField($right) => '>= ' . $item[$right]),
|
||||
'conditions' => array($scope, $model->escapeField($left) . ' <=' => $item[$left], $model->escapeField($right) . ' >=' => $item[$right]),
|
||||
'fields' => $fields, 'order' => array($model->escapeField($left) => 'asc'), 'recursive' => $recursive
|
||||
));
|
||||
return $results;
|
||||
|
@ -435,8 +437,10 @@ class TreeBehavior extends ModelBehavior {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
$nextNode = $model->find('first', array('conditions' => array($scope, $model->escapeField($left) => ($node[$right] + 1)),
|
||||
'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive));
|
||||
$nextNode = $model->find('first', array(
|
||||
'conditions' => array($scope, $model->escapeField($left) => ($node[$right] + 1)),
|
||||
'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive)
|
||||
);
|
||||
if ($nextNode) {
|
||||
list($nextNode)= array_values($nextNode);
|
||||
} else {
|
||||
|
@ -888,19 +892,16 @@ class TreeBehavior extends ModelBehavior {
|
|||
$modelRecursive = $model->recursive;
|
||||
extract($this->settings[$model->alias]);
|
||||
$model->recursive = $recursive;
|
||||
|
||||
if ($field == 'both') {
|
||||
$this->__sync($model, $shift, $dir, $conditions, $created, $left);
|
||||
$field = $right;
|
||||
}
|
||||
if (is_string($conditions)) {
|
||||
$conditions = array($model->escapeField($field) => $conditions);
|
||||
$conditions = array("{$model->alias}.{$field} {$conditions}");
|
||||
}
|
||||
if ($scope != '1 = 1' && $scope) {
|
||||
if (is_string($scope)) {
|
||||
$conditions[]= $scope;
|
||||
} else {
|
||||
$conditions= array_merge($conditions, $scope);
|
||||
}
|
||||
if (($scope != '1 = 1' && $scope !== true) && $scope) {
|
||||
$conditions[] = $scope;
|
||||
}
|
||||
if ($created) {
|
||||
$conditions['NOT'][$model->alias . '.' . $model->primaryKey] = $model->id;
|
||||
|
@ -909,4 +910,5 @@ class TreeBehavior extends ModelBehavior {
|
|||
$model->recursive = $modelRecursive;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -229,10 +229,10 @@ class DboMysql extends DboSource {
|
|||
break;
|
||||
case 'integer':
|
||||
case 'float':
|
||||
if (
|
||||
(is_int($data) || is_float($data)) ||
|
||||
(is_string($data) && strpos($data, ',') === false && $data[0] != '0' && strpos($data, 'e') === false)
|
||||
) {
|
||||
if ((is_int($data) || is_float($data)) || (
|
||||
is_numeric($data) && strpos($data, ',') === false &&
|
||||
$data[0] != '0' && strpos($data, 'e') === false
|
||||
)) {
|
||||
return $data;
|
||||
}
|
||||
default:
|
||||
|
|
|
@ -602,7 +602,18 @@ class DboPostgres extends DboSource {
|
|||
*/
|
||||
function buildColumn($column) {
|
||||
$out = preg_replace('/integer\([0-9]+\)/', 'integer', parent::buildColumn($column));
|
||||
return str_replace('integer serial', 'serial', $out);
|
||||
$out = str_replace('integer serial', 'serial', $out);
|
||||
|
||||
if (strpos($column, 'DEFAULT DEFAULT')) {
|
||||
if ($column['null']) {
|
||||
$out = str_replace('DEFAULT DEFAULT', 'DEFAULT NULL', $out);
|
||||
} elseif (in_array($column['type'], array('integer', 'float'))) {
|
||||
$out = str_replace('DEFAULT DEFAULT', 'DEFAULT 0', $out);
|
||||
} elseif ($column['type'] == 'boolean') {
|
||||
$out = str_replace('DEFAULT DEFAULT', 'DEFAULT FALSE', $out);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
/**
|
||||
* Format indexes for create table
|
||||
|
|
|
@ -133,19 +133,41 @@ class DboSource extends DataSource {
|
|||
*/
|
||||
function value($data, $column = null) {
|
||||
if (is_array($data)) {
|
||||
$out = array();
|
||||
$keys = array_keys($data);
|
||||
$count = count($data);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$out[$keys[$i]] = $this->value($data[$keys[$i]]);
|
||||
return array_map(array(&$this, 'value'), $data, array_fill(0, count($data), $column));
|
||||
} elseif (is_object($data)) {
|
||||
if (isset($data->type) && $data->type == 'identifier') {
|
||||
return $this->name($data->value);
|
||||
}
|
||||
return $out;
|
||||
} elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
|
||||
return $data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns an object to represent a database identifier in a query
|
||||
*
|
||||
* @param string $identifier
|
||||
* @return object An object representing a database identifier to be used in a query
|
||||
*/
|
||||
function identifier($identifier) {
|
||||
$obj = new stdClass();
|
||||
$obj->type = 'identifier';
|
||||
$obj->value = $identifier;
|
||||
return $obj;
|
||||
}
|
||||
/**
|
||||
* Returns an object to represent a database expression in a query
|
||||
*
|
||||
* @param string $expression
|
||||
* @return object An object representing a database expression to be used in a query
|
||||
*/
|
||||
function expression($expression) {
|
||||
$obj = new stdClass();
|
||||
$obj->type = 'expression';
|
||||
$obj->value = $expression;
|
||||
return $obj;
|
||||
}
|
||||
/**
|
||||
* Executes given SQL statement.
|
||||
*
|
||||
|
@ -162,7 +184,7 @@ class DboSource extends DataSource {
|
|||
* If DEBUG is set, the log is shown all the time, else it is only shown on errors.
|
||||
*
|
||||
* @param string $sql
|
||||
* @return unknown
|
||||
* @return mixed Resource or object representing the result set, or false on failure
|
||||
*/
|
||||
function execute($sql) {
|
||||
$t = getMicrotime();
|
||||
|
@ -233,11 +255,7 @@ class DboSource extends DataSource {
|
|||
$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]->alias . '.' . $f] = '= ' . $params[$c];
|
||||
} else {
|
||||
$query[$args[2]->alias . '.' . $f] = $params[$c];
|
||||
}
|
||||
$query[$args[2]->alias . '.' . $f] = $params[$c];
|
||||
$c++;
|
||||
}
|
||||
|
||||
|
@ -277,12 +295,8 @@ class DboSource extends DataSource {
|
|||
} else {
|
||||
$cache = true;
|
||||
}
|
||||
while ($pos = strpos($args[0], '?', $offset)) {
|
||||
$offset = $pos;
|
||||
$value = $this->value(array_shift($args[1]));
|
||||
$args[0] = substr_replace($args[0], $value, $pos, 1);
|
||||
}
|
||||
return $this->fetchAll($args[0], $cache);
|
||||
$args[1] = array_map(array(&$this, 'value'), $args[1]);
|
||||
return $this->fetchAll(String::insert($args[0], $args[1]), $cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1029,7 +1043,6 @@ class DboSource extends DataSource {
|
|||
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
|
||||
}
|
||||
$assocData['limit'] = $this->limit($assocData['limit'], $assocData['offset']);
|
||||
|
||||
|
||||
switch($type) {
|
||||
case 'hasOne':
|
||||
|
@ -1077,7 +1090,7 @@ class DboSource extends DataSource {
|
|||
'alias' => $alias,
|
||||
'order' => $assocData['order'],
|
||||
'limit' => $assocData['limit'],
|
||||
'group' => null,
|
||||
'group' => null
|
||||
);
|
||||
break;
|
||||
case 'hasAndBelongsToMany':
|
||||
|
@ -1112,7 +1125,9 @@ class DboSource extends DataSource {
|
|||
'joins' => array(array(
|
||||
'table' => $joinTbl,
|
||||
'alias' => $joinAssoc,
|
||||
'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $alias))));
|
||||
'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $alias)
|
||||
))
|
||||
);
|
||||
break;
|
||||
}
|
||||
if (isset($query)) {
|
||||
|
@ -1143,10 +1158,10 @@ class DboSource extends DataSource {
|
|||
return array("{$alias}.{$linkModel->primaryKey}" => '{$__cakeForeignKey__$}');
|
||||
break;
|
||||
case (!$assoc['external'] && $type == 'hasOne'):
|
||||
return array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeIdentifier[' . "{$model->alias}.{$model->primaryKey}" . ']__$}');
|
||||
return array("{$alias}.{$assoc['foreignKey']}" => $this->identifier("{$model->alias}.{$model->primaryKey}"));
|
||||
break;
|
||||
case (!$assoc['external'] && $type == 'belongsTo'):
|
||||
return array("{$model->alias}.{$assoc['foreignKey']}" => '{$__cakeIdentifier[' . "{$alias}.{$linkModel->primaryKey}" . ']__$}');
|
||||
return array("{$model->alias}.{$assoc['foreignKey']}" => $this->identifier("{$alias}.{$linkModel->primaryKey}"));
|
||||
break;
|
||||
case ($type == 'hasMany'):
|
||||
return array("{$alias}.{$assoc['foreignKey']}" => array('{$__cakeID__$}'));
|
||||
|
@ -1154,7 +1169,7 @@ class DboSource extends DataSource {
|
|||
case ($type == 'hasAndBelongsToMany'):
|
||||
return array(
|
||||
array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeID__$}'),
|
||||
array("{$alias}.{$assoc['associationForeignKey']}" => '{$__cakeIdentifier['."{$alias2}.{$linkModel->primaryKey}".']__$}')
|
||||
array("{$alias}.{$assoc['associationForeignKey']}" => $this->identifier("{$alias2}.{$linkModel->primaryKey}"))
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
@ -1708,133 +1723,80 @@ class DboSource extends DataSource {
|
|||
function conditionKeysToString($conditions, $quoteValues = true, $model = null) {
|
||||
$c = 0;
|
||||
$out = array();
|
||||
$data = $not = $columnType = null;
|
||||
$data = $columnType = null;
|
||||
$bool = array('and', 'or', 'not', 'and not', 'or not', 'xor', '||', '&&');
|
||||
$join = ' AND ';
|
||||
|
||||
foreach ($conditions as $key => $value) {
|
||||
$join = ' AND ';
|
||||
$not = null;
|
||||
|
||||
if (is_array($value)) {
|
||||
$valueInsert = (
|
||||
!empty($value) &&
|
||||
(substr_count($key, '?') == count($value) || substr_count($key, ':') == count($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) . ' ';
|
||||
} elseif ((is_numeric($key) && is_array($value)) || in_array(strtolower(trim($key)), $bool)) {
|
||||
if (in_array(strtolower(trim($key)), $bool)) {
|
||||
$join = ' ' . strtoupper($key) . ' ';
|
||||
} else {
|
||||
$key = $join;
|
||||
}
|
||||
$value = $this->conditionKeysToString($value, $quoteValues, $model);
|
||||
|
||||
if (strpos($join, 'NOT') !== false) {
|
||||
if (strtoupper(trim($key)) == 'NOT') {
|
||||
$key = 'AND ' . $key;
|
||||
$key = 'AND ' . trim($key);
|
||||
}
|
||||
$not = 'NOT ';
|
||||
} else {
|
||||
$not = null;
|
||||
}
|
||||
$out[] = $not . '((' . join(') ' . strtoupper($key) . ' (', $value) . '))';
|
||||
$out[] = $not . '(' . join(') ' . strtoupper($key) . ' (', $value) . ')';
|
||||
} else {
|
||||
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)) {
|
||||
if (is_object($value) && isset($value->type)) {
|
||||
if ($value->type == 'identifier') {
|
||||
$data .= $this->name($key) . ' = ' . $this->name($value->value);
|
||||
} elseif ($value->type == 'identifier') {
|
||||
$data .= $this->name($key) . ' = ' . $value->value;
|
||||
}
|
||||
} elseif (is_array($value) && !empty($value) && !$valueInsert) {
|
||||
$keys = array_keys($value);
|
||||
if ($keys[0] === 0) {
|
||||
if (array_keys($value) === array_values(array_keys($value))) {
|
||||
$data = $this->name($key) . ' IN (';
|
||||
if (strpos($value[0], '-!') === 0) {
|
||||
$value[0] = str_replace('-!', '', $value[0]);
|
||||
$data .= $value[0];
|
||||
$data .= ')';
|
||||
} else {
|
||||
if ($quoteValues) {
|
||||
if (is_object($model)) {
|
||||
$columnType = $model->getColumnType($key);
|
||||
}
|
||||
foreach ($value as $key => $valElement) {
|
||||
$data .= $this->value($valElement, $columnType) . ', ';
|
||||
}
|
||||
if ($quoteValues || strpos($value[0], '-!') !== 0) {
|
||||
if (is_object($model)) {
|
||||
$columnType = $model->getColumnType($key);
|
||||
}
|
||||
$data[strlen($data) - 2] = ')';
|
||||
$data .= join(', ', $this->value($value, $columnType));
|
||||
} elseif (strpos($value[0], '-!') === 0) {
|
||||
trigger_error(__('Escape flag (-!) deprecated, use Model::query()', true), E_USER_WARNING);
|
||||
$data .= $this->value(str_replace('-!', '', $value[0]));
|
||||
}
|
||||
$data .= ')';
|
||||
} else {
|
||||
$ret = $this->conditionKeysToString($value, $quoteValues, $model);
|
||||
if (count($ret) > 1) {
|
||||
$out[] = '(' . join(') AND (', $ret) . ')';
|
||||
$data = '(' . join(') AND (', $ret) . ')';
|
||||
} elseif (isset($ret[0])) {
|
||||
$out[] = $ret[0];
|
||||
$data = $ret[0];
|
||||
}
|
||||
}
|
||||
} 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) . " = ''";
|
||||
} elseif (preg_match('/^([a-z]+\\([a-z0-9]*\\)\\x20+|(?:' . join('\\x20)|(?:', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)?(.*)/is', $value, $match)) {
|
||||
if (preg_match('/(\\x20[\\w]*\\x20)/', $key, $regs)) {
|
||||
$clause = $regs['1'];
|
||||
$key = preg_replace('/' . $regs['1'] . '/', '', $key);
|
||||
}
|
||||
|
||||
$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'];
|
||||
} elseif (strtolower($mValue) == 'not') {
|
||||
$not = $this->conditionKeysToString(array($mValue => array($key => $match[2])), $quoteValues, $model);
|
||||
}
|
||||
|
||||
if ($not) {
|
||||
$data = $not[0];
|
||||
} elseif (strpos($match['2'], '-!') === 0) {
|
||||
$match['2'] = str_replace('-!', '', $match['2']);
|
||||
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
|
||||
} else {
|
||||
$op = substr(trim($match[1]), 0, 1);
|
||||
$isOp = (
|
||||
strpos('!=<>', trim($op)) !== false ||
|
||||
strpos('!=<>', trim($match[1])) !== false ||
|
||||
in_array(strtolower(trim($match[1])), $this->__sqlOps)
|
||||
);
|
||||
if (is_object($model)) {
|
||||
$columnType = $model->getColumnType($key);
|
||||
}
|
||||
|
||||
if (!empty($match['2']) && $quoteValues) {
|
||||
if (!$isOp) {
|
||||
$match['1'] = ' = ';
|
||||
$match['2'] = $this->value($match['0'], $columnType);
|
||||
} else {
|
||||
$match['2'] = $this->value($match['2'], $columnType);
|
||||
if (
|
||||
preg_match('/^(?:' . join('\\x20)|(?:', $this->__sqlOps) . '\s*\\x20)/i', $match['1']) &&
|
||||
(strpos($match['2'], "'") === 0)
|
||||
) {
|
||||
$match['2'] = str_replace(' AND ', "' AND '", $match['2']);
|
||||
}
|
||||
}
|
||||
} elseif ($isOp) {
|
||||
$match['1'] = trim($match['1']);
|
||||
$match['2'] = $this->value($match['2'], $columnType);
|
||||
} else {
|
||||
$match['2'] = $this->value($match['1'], $columnType);
|
||||
$match['1'] = ' = ';
|
||||
}
|
||||
$data = $this->__quoteFields($key);
|
||||
|
||||
if ($data === $key) {
|
||||
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
|
||||
} else {
|
||||
$data = $data . ' ' . $match['1'] . ' ' . $match['2'];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$data = $this->__parseKey($model, trim($key), $value);
|
||||
}
|
||||
|
||||
if ($data != null) {
|
||||
if (preg_match('/^\(\(\((.+)\)\)\)$/', $data)) {
|
||||
$data = substr($data, 1, strlen($data) - 2);
|
||||
}
|
||||
$out[] = $data;
|
||||
$data = null;
|
||||
}
|
||||
|
@ -1843,6 +1805,76 @@ class DboSource extends DataSource {
|
|||
}
|
||||
return $out;
|
||||
}
|
||||
/**
|
||||
* Extracts a Model.field identifier and an SQL condition operator from a string, formats and inserts values,
|
||||
* and composes them into an SQL snippet.
|
||||
*
|
||||
* @param Model $model Model object initiating the query
|
||||
* @param string $key An SQL key snippet containing a field and optional SQL operator
|
||||
* @param mixed $value The value(s) to be inserted in the string
|
||||
* @return string
|
||||
* @access private
|
||||
*/
|
||||
function __parseKey($model, $key, $value) {
|
||||
if (!strpos($key, ' ')) {
|
||||
$operator = '=';
|
||||
} else {
|
||||
list($key, $operator) = explode(' ', $key, 2);
|
||||
}
|
||||
$type = (is_object($model) ? $model->getColumnType($key) : null);
|
||||
$null = ($value === null || (is_array($value) && empty($value)));
|
||||
|
||||
if (strtolower($operator) === 'not') {
|
||||
$data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $model);
|
||||
return $data[0];
|
||||
}
|
||||
if (!preg_match('/^((' . join(')|(', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is', trim($operator))) {
|
||||
$operator .= ' =';
|
||||
}
|
||||
|
||||
if (
|
||||
(is_array($value) && is_string($value[0]) && strpos($value[0], '-!') === 0) ||
|
||||
(is_string($value) && strpos($value, '-!') === 0)
|
||||
) {
|
||||
trigger_error(__('Escape flag (-!) deprecated, use Model::query()', true), E_USER_WARNING);
|
||||
$value = str_replace('-!', '', $value);
|
||||
} else {
|
||||
$value = $this->value($value, $type);
|
||||
}
|
||||
$operator = trim($operator);
|
||||
|
||||
$key = (strpos($key, '(') !== false || strpos($key, ')') !== false) ?
|
||||
$this->__quoteFields($key) :
|
||||
$key = $this->name($key);
|
||||
|
||||
if (strpos($operator, '?') !== false || (is_array($value) && strpos($operator, ':') !== false)) {
|
||||
return "{$key} " . String::insert($operator, $value);
|
||||
} elseif (is_array($value)) {
|
||||
$value = join(', ', $value);
|
||||
|
||||
switch ($operator) {
|
||||
case '=':
|
||||
$operator = 'IN';
|
||||
break;
|
||||
case '!=':
|
||||
case '<>':
|
||||
$operator = 'NOT IN';
|
||||
break;
|
||||
}
|
||||
$value = "({$value})";
|
||||
} elseif ($null) {
|
||||
switch ($operator) {
|
||||
case '=':
|
||||
$operator = 'IS';
|
||||
break;
|
||||
case '!=':
|
||||
case '<>':
|
||||
$operator = 'NOT IS';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return "{$key} {$operator} {$value}";
|
||||
}
|
||||
/**
|
||||
* Quotes Model.fields
|
||||
*
|
||||
|
@ -1995,7 +2027,7 @@ class DboSource extends DataSource {
|
|||
*
|
||||
* @param string $group Group By Condition
|
||||
* @return mixed string condition or null
|
||||
**/
|
||||
*/
|
||||
function group($group) {
|
||||
if ($group) {
|
||||
return ' GROUP BY ' . $this->__quoteFields($group);
|
||||
|
@ -2022,9 +2054,11 @@ class DboSource extends DataSource {
|
|||
*/
|
||||
function hasAny(&$Model, $sql) {
|
||||
$sql = $this->conditions($sql);
|
||||
$out = $this->fetchRow(
|
||||
"SELECT COUNT({$Model->primaryKey}) {$this->alias}count FROM " . $this->fullTableName($Model) . ' ' . ($sql ? ' ' . $sql : 'WHERE 1 = 1')
|
||||
);
|
||||
$table = $this->fullTableName($Model);
|
||||
$where = $sql ? "WHERE {$sql}" : 'WHERE 1 = 1';
|
||||
$id = $Model->primaryKey;
|
||||
|
||||
$out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$where}");
|
||||
|
||||
if (is_array($out)) {
|
||||
return $out[0]['count'];
|
||||
|
@ -2053,12 +2087,7 @@ class DboSource extends DataSource {
|
|||
}
|
||||
|
||||
$types = array(
|
||||
'int' => 1,
|
||||
'tinyint' => 1,
|
||||
'smallint' => 1,
|
||||
'mediumint' => 1,
|
||||
'integer' => 1,
|
||||
'bigint' => 1
|
||||
'int' => 1, 'tinyint' => 1, 'smallint' => 1, 'mediumint' => 1, 'integer' => 1, 'bigint' => 1
|
||||
);
|
||||
|
||||
list($real, $type, $length, $offset, $sign, $zerofill) = $result;
|
||||
|
@ -2349,7 +2378,6 @@ class DboSource extends DataSource {
|
|||
if ($containsInt && !$containsString) {
|
||||
return 'integer';
|
||||
}
|
||||
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -166,14 +166,18 @@ class AclNode extends AppModel {
|
|||
}
|
||||
}
|
||||
if (is_array($ref)) {
|
||||
if (is_array(current($ref)) && is_string(key($ref))) {
|
||||
$name = key($ref);
|
||||
$ref = current($ref);
|
||||
}
|
||||
foreach ($ref as $key => $val) {
|
||||
if (strpos($key, $type) !== 0) {
|
||||
if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
|
||||
unset($ref[$key]);
|
||||
$ref["{$type}0.{$key}"] = $val;
|
||||
}
|
||||
}
|
||||
$queryData = array(
|
||||
'conditions' => $ref,
|
||||
'conditions' => $ref,
|
||||
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
|
||||
'joins' => array(array(
|
||||
'table' => $db->fullTableName($table),
|
||||
|
|
|
@ -2007,7 +2007,7 @@ class Model extends Overloadable {
|
|||
$fields = array('or' => $fields);
|
||||
}
|
||||
if (!empty($this->id)) {
|
||||
$fields[$this->alias . '.' . $this->primaryKey] = '!= ' . $this->id;
|
||||
$fields[$this->alias . '.' . $this->primaryKey . ' !='] = $this->id;
|
||||
}
|
||||
return ($this->find('count', array('conditions' => $fields)) == 0);
|
||||
}
|
||||
|
@ -2076,11 +2076,11 @@ class Model extends Overloadable {
|
|||
|
||||
$prev = $next = null;
|
||||
|
||||
$result = $this->findAll(array_filter(array_merge($conditions, array($field => '< ' . $value))), $fields, $field . ' DESC', 1, null, 0);
|
||||
$result = $this->findAll(array_filter(array_merge($conditions, array($field . ' <' => $value))), $fields, $field . ' DESC', 1, null, 0);
|
||||
if (isset($result[0])) {
|
||||
$prev = $result[0];
|
||||
}
|
||||
$result = $this->findAll(array_filter(array_merge($conditions, array($field => '> ' . $value))), $fields, $field . ' ASC', 1, null, 0);
|
||||
$result = $this->findAll(array_filter(array_merge($conditions, array($field . ' >' => $value))), $fields, $field . ' ASC', 1, null, 0);
|
||||
if (isset($result[0])) {
|
||||
$next = $result[0];
|
||||
}
|
||||
|
|
|
@ -212,14 +212,12 @@ class String extends Object {
|
|||
* @access public
|
||||
*/
|
||||
function insert($str, $data, $options = array()) {
|
||||
$options = array_merge(array(
|
||||
'before' => ':',
|
||||
'after' => null,
|
||||
'escape' => '\\',
|
||||
'format' => null,
|
||||
'clean' => false), $options);
|
||||
|
||||
$options = array_merge(
|
||||
array('before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false),
|
||||
$options
|
||||
);
|
||||
$format = $options['format'];
|
||||
|
||||
if (!isset($format)) {
|
||||
$format = sprintf(
|
||||
'/(?<!%s)%s%%s%s/',
|
||||
|
@ -228,11 +226,24 @@ class String extends Object {
|
|||
str_replace('%', '%%', preg_quote($options['after'], '/'))
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
$key = sprintf($format, preg_quote($key, '/'));
|
||||
$str = preg_replace($key, $val, $str);
|
||||
if (!is_array($data)) {
|
||||
$data = array($data);
|
||||
}
|
||||
|
||||
if (array_keys($data) === array_keys(array_values($data))) {
|
||||
$offset = 0;
|
||||
while ($pos = strpos($str, '?', $offset)) {
|
||||
$offset = $pos;
|
||||
$val = array_shift($data);
|
||||
$str = substr_replace($str, $val, $pos, 1);
|
||||
}
|
||||
} else {
|
||||
foreach ($data as $key => $val) {
|
||||
$key = sprintf($format, preg_quote($key, '/'));
|
||||
$str = preg_replace($key, $val, $str);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($options['format']) && isset($options['before'])) {
|
||||
$str = str_replace($options['escape'].$options['before'], $options['before'], $str);
|
||||
}
|
||||
|
|
|
@ -202,7 +202,10 @@ class AuthTest extends CakeTestCase {
|
|||
|
||||
$this->Controller->Auth->startup($this->Controller);
|
||||
$user = $this->Controller->Auth->user();
|
||||
$this->assertEqual($user, array('AuthUser'=>array('id'=>1, 'username'=>'mariano', 'created'=> '2007-03-17 01:16:23', 'updated'=> date('Y-m-d H:i:s'))));
|
||||
$expected = array('AuthUser' => array(
|
||||
'id' => 1, 'username' => 'mariano', 'created' => '2007-03-17 01:16:23', 'updated' => date('Y-m-d H:i:s')
|
||||
));
|
||||
$this->assertEqual($user, $expected);
|
||||
$this->Controller->Session->del('Auth');
|
||||
|
||||
$this->Controller->data['AuthUser']['username'] = 'blah';
|
||||
|
|
|
@ -55,11 +55,11 @@ class TestBehavior extends ModelBehavior {
|
|||
return null;
|
||||
break;
|
||||
case 'modify':
|
||||
return Set::extract($results, '{n}.' . $model->alias);
|
||||
return Set::extract($results, "{n}.{$model->alias}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function beforeSave(&$model) {
|
||||
$settings = $this->settings[$model->alias];
|
||||
if (!isset($settings['beforeSave']) || $settings['beforeSave'] == 'off') {
|
||||
|
@ -103,7 +103,7 @@ class TestBehavior extends ModelBehavior {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function beforeValidate(&$model) {
|
||||
$settings = $this->settings[$model->alias];
|
||||
if (!isset($settings['validate']) || $settings['validate'] == 'off') {
|
||||
|
@ -127,7 +127,7 @@ class TestBehavior extends ModelBehavior {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function beforeDelete(&$model, $cascade = true) {
|
||||
$settings =& $this->settings[$model->alias];
|
||||
if (!isset($settings['beforeDelete']) || $settings['beforeDelete'] == 'off') {
|
||||
|
@ -160,7 +160,7 @@ class TestBehavior extends ModelBehavior {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function onError(&$model) {
|
||||
$settings = $this->settings[$model->alias];
|
||||
if (!isset($settings['onError']) || $settings['onError'] == 'off') {
|
||||
|
@ -256,8 +256,10 @@ class BehaviorTest extends CakeTestCase {
|
|||
$Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
|
||||
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
|
||||
$Apple->Behaviors->attach('Test');
|
||||
$expected = array_merge($current, array('mangle' => 'trigger mangled mangled'));
|
||||
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
|
||||
$Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
|
||||
$expected = array_merge($current, array('mangle' => 'trigger mangled'));
|
||||
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
|
||||
}
|
||||
|
||||
|
@ -309,7 +311,7 @@ class BehaviorTest extends CakeTestCase {
|
|||
array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
|
||||
array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
|
||||
);
|
||||
$result = $Apple->find('all', array('conditions' => array('Apple.id' => '< 4')));
|
||||
$result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
|
||||
$this->assertEqual($result, $expected2);
|
||||
|
||||
$Apple->Behaviors->disable('Test');
|
||||
|
@ -348,7 +350,7 @@ class BehaviorTest extends CakeTestCase {
|
|||
|
||||
$Apple->unbindModel(array('hasMany' => array('Child')));
|
||||
$wellBehaved = $Apple->find('all');
|
||||
$Apple->Child->Behaviors->attach('Test');
|
||||
$Apple->Child->Behaviors->attach('Test', array('afterFind' => 'modify'));
|
||||
$this->assertIdentical($Apple->find('all'), $wellBehaved);
|
||||
|
||||
$Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
|
||||
|
@ -498,7 +500,7 @@ class BehaviorTest extends CakeTestCase {
|
|||
'Apple' => array('id' => 3),
|
||||
'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
|
||||
);
|
||||
$result = $Apple->find('all', array('fields' => array('Apple.id', 'Parent.*'), 'conditions' => array('Apple.id' => '< 4')));
|
||||
$result = $Apple->find('all', array('fields' => array('Apple.id', 'Parent.*'), 'conditions' => array('Apple.id <' => '4')));
|
||||
$this->assertEqual($result, $expected2);
|
||||
|
||||
$Apple->Parent->Behaviors->disable('Test');
|
||||
|
|
|
@ -2821,7 +2821,7 @@ class ContainableTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
function testFindConditionalBinding() {
|
||||
$this->Article->contain(array('User(user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created' => '>= 2007-03-18 12:24'))));
|
||||
$this->Article->contain(array('User(user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created >=' => '2007-03-18 12:24'))));
|
||||
$result = $this->Article->find('all', array('fields' => array('title')));
|
||||
$expected = array(
|
||||
array(
|
||||
|
@ -2898,7 +2898,7 @@ class ContainableTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->Article->contain(array('User(id,user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created' => '>= 2007-03-18 12:24'))));
|
||||
$this->Article->contain(array('User(id,user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created >=' => '2007-03-18 12:24'))));
|
||||
$result = $this->Article->find('all', array('fields' => array('title')));
|
||||
$expected = array(
|
||||
array(
|
||||
|
@ -2925,7 +2925,7 @@ class ContainableTest extends CakeTestCase {
|
|||
$expected = 3;
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->Article->find('count', array('conditions' => array('Article.id' => '> 1')));
|
||||
$result = $this->Article->find('count', array('conditions' => array('Article.id >' => '1')));
|
||||
$expected = 2;
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
|
@ -2933,7 +2933,7 @@ class ContainableTest extends CakeTestCase {
|
|||
$expected = 3;
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->Article->contain(array('User(id,user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created' => '>= 2007-03-18 12:24'))));
|
||||
$this->Article->contain(array('User(id,user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created >=' => '2007-03-18 12:24'))));
|
||||
$result = $this->Article->find('first', array('fields' => array('title')));
|
||||
$expected = array(
|
||||
'Article' => array('id' => 1, 'title' => 'First Article'),
|
||||
|
|
|
@ -46,10 +46,11 @@ class NumberTree extends CakeTestModel {
|
|||
$this->initialize($levelLimit, $childLimit, 1, $this->id, '1', $hierachial);
|
||||
$this->create(array());
|
||||
}
|
||||
|
||||
|
||||
if (!$currentLevel || $currentLevel > $levelLimit) {
|
||||
return;
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $childLimit; $i++) {
|
||||
$name = $prefix . '.' . $i;
|
||||
$data = array($this->name => array('name' => $name));
|
||||
|
@ -70,8 +71,8 @@ class FlagTree extends NumberTree {
|
|||
|
||||
class Campaign extends CakeTestModel {
|
||||
var $name = 'Campaign';
|
||||
var $hasMany = array('Ad' => array('fields'=>array('id','campaign_id','name') ));
|
||||
}
|
||||
var $hasMany = array('Ad' => array('fields' => array('id','campaign_id','name') ));
|
||||
}
|
||||
class Ad extends CakeTestModel {
|
||||
var $name = 'Ad';
|
||||
var $actsAs = array('Tree');
|
||||
|
@ -85,14 +86,14 @@ class NumberTreeCase extends CakeTestCase {
|
|||
function testInitialize() {
|
||||
$this->NumberTree =& new NumberTree();
|
||||
$this->NumberTree->initialize(2, 2);
|
||||
|
||||
|
||||
$result = $this->NumberTree->find('count');
|
||||
$this->assertEqual($result, 7);
|
||||
|
||||
|
||||
$validTree = $this->NumberTree->verify();
|
||||
$this->assertIdentical($validTree, true);
|
||||
}
|
||||
|
||||
|
||||
function testStringScope() {
|
||||
$this->FlagTree =& new FlagTree();
|
||||
$this->FlagTree->initialize(2, 3);
|
||||
|
@ -151,7 +152,7 @@ class NumberTreeCase extends CakeTestCase {
|
|||
$expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->assertTrue($this->FlagTree->delete());
|
||||
$this->assertTrue($this->FlagTree->delete());
|
||||
$this->assertEqual($this->FlagTree->find('count'), 11);
|
||||
}
|
||||
|
||||
|
|
|
@ -689,7 +689,8 @@ class DboSourceTest extends CakeTestCase {
|
|||
'limit' => array(),
|
||||
'offset' => array(),
|
||||
'conditions' => array(),
|
||||
'order' => array()
|
||||
'order' => array(),
|
||||
'group' => null
|
||||
);
|
||||
$this->assertEqual($queryData, $expected);
|
||||
|
||||
|
@ -773,7 +774,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->_buildRelatedModels($this->Model);
|
||||
|
||||
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
|
||||
$queryData = array('conditions' => array('TestModel4Parent.name' => '!= mariano'));
|
||||
$queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
|
||||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
|
@ -858,7 +859,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
|
||||
$binding = array('type' => 'hasOne', 'model' => 'TestModel5');
|
||||
|
||||
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
|
||||
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
|
||||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
|
@ -907,7 +908,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->_buildRelatedModels($this->Model);
|
||||
|
||||
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
|
||||
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
|
||||
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
|
||||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
|
@ -924,7 +925,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result);
|
||||
$this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result);
|
||||
$this->assertPattern('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result);
|
||||
}
|
||||
|
||||
function testGenerateAssociationQueryHasMany() {
|
||||
|
@ -987,7 +988,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->_buildRelatedModels($this->Model);
|
||||
|
||||
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
|
||||
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
|
||||
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
|
||||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
|
@ -1197,13 +1198,13 @@ class DboSourceTest extends CakeTestCase {
|
|||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
|
||||
$params =& $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
|
||||
$this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`/', $result);
|
||||
$this->assertPattern('/\s+ON\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
|
||||
$this->assertPattern('/\s+AND\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`(?:\))?/', $result);
|
||||
$this->assertPattern('/\s+ON\s+\(\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\)\s+AND/', $result);
|
||||
$this->assertPattern('/\s+AND\s+\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\)/', $result);
|
||||
$this->assertPattern('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
|
||||
|
@ -1218,17 +1219,17 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->_buildRelatedModels($this->Model);
|
||||
|
||||
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
|
||||
$queryData = array('conditions' => array('TestModel4.name' => '!= mariano'));
|
||||
$queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
|
||||
$resultSet = null;
|
||||
$null = null;
|
||||
|
||||
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
|
||||
$params =& $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
|
||||
$this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
|
||||
$this->assertPattern('/\s+AND\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`(?:\))?\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+\(\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\)/', $result);
|
||||
$this->assertPattern('/\s+AND\s+\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`\)\)\s+WHERE\s+/', $result);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
|
||||
$this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
|
||||
|
@ -1256,8 +1257,8 @@ class DboSourceTest extends CakeTestCase {
|
|||
|
||||
$this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
|
||||
$this->assertPattern('/\s+AND\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`(?:\))?\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+\(\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\)\s+/', $result);
|
||||
$this->assertPattern('/\s+AND\s+\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`\)\)\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
|
||||
|
@ -1288,8 +1289,8 @@ class DboSourceTest extends CakeTestCase {
|
|||
|
||||
$this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
|
||||
$this->assertPattern('/\s+AND\s+(?:\()?`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`(?:\))?\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+ON\s+\(\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\)/', $result);
|
||||
$this->assertPattern('/\s+AND\s+\(`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`\.`test_model7_id`\s+=\s+`TestModel7`.`id`\)\)\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
|
||||
|
@ -1424,19 +1425,19 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
|
||||
$letter = $letter = 'd.a';
|
||||
$conditions = array('Company.name' => 'like '.$letter.'%');
|
||||
$conditions = array('Company.name like ' => $letter . '%');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `Company`.`name` like 'd.a%'";
|
||||
$expected = " WHERE `Company`.`name` like 'd.a%'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('Artist.name' => 'JUDY and MARY');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
|
||||
$expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('Artist.name' => 'JUDY AND MARY');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
|
||||
$expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
|
@ -1548,98 +1549,106 @@ class DboSourceTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
function testArrayConditionsParsing() {
|
||||
$result = $this->testDb->conditions(array('Candy.name' => 'LIKE a', 'HardCandy.name' => 'LIKE c'));
|
||||
$result = $this->testDb->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c'));
|
||||
$this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('HardCandy.name' => 'LIKE a', 'Candy.name' => 'LIKE c'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
|
||||
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('HardCandy.name' => 'LIKE a%', 'Candy.name' => 'LIKE %c%'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
|
||||
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('HardCandy.name' => 'LIKE to be or%', 'Candy.name' => 'LIKE %not to be%'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
|
||||
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%'));
|
||||
$expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('score' => 'BETWEEN 90.1 AND 95.7'));
|
||||
$expected = " WHERE `score` BETWEEN '90.1' AND '95.7'";
|
||||
$result = $this->testDb->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7)));
|
||||
$expected = " WHERE `score` BETWEEN 90.1 AND 95.7";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('Post.title' => 1.1));
|
||||
$expected = " WHERE `Post`.`title` = '1.1'";
|
||||
$expected = " WHERE `Post`.`title` = 1.1";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('Post.title' => 1.1), true, true, new Post());
|
||||
$expected = " WHERE `Post`.`title` = '1.1'";
|
||||
$expected = " WHERE `Post`.`title` = '1.1'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('SUM(Post.comments_count)' => '> 500'));
|
||||
$expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
|
||||
$result = $this->testDb->conditions(array('SUM(Post.comments_count) >' => '500'));
|
||||
$expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('MAX(Post.rating)' => '> 50'));
|
||||
$expected = " WHERE MAX(`Post`.`rating`) > '50'";
|
||||
$result = $this->testDb->conditions(array('MAX(Post.rating) >' => '50'));
|
||||
$expected = " WHERE MAX(`Post`.`rating`) > '50'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('title' => 'LIKE %hello'));
|
||||
$expected = " WHERE `title` LIKE '%hello'";
|
||||
$result = $this->testDb->conditions(array('title LIKE' => '%hello'));
|
||||
$expected = " WHERE `title` LIKE '%hello'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('Post.name' => '= mad(g)ik'));
|
||||
$expected = " WHERE `Post`.`name` = 'mad(g)ik'";
|
||||
$result = $this->testDb->conditions(array('Post.name' => 'mad(g)ik'));
|
||||
$expected = " WHERE `Post`.`name` = 'mad(g)ik'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('score' => array(1, 2, 10)));
|
||||
$expected = " WHERE `score` IN (1, 2, 10) ";
|
||||
$expected = " WHERE `score` IN (1, 2, 10)";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('score' => '!= 20'));
|
||||
$expected = " WHERE `score` != '20'";
|
||||
$result = $this->testDb->conditions(array('score !=' => '20'));
|
||||
$expected = " WHERE `score` != '20'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('score' => '> 20'));
|
||||
$expected = " WHERE `score` > '20'";
|
||||
$result = $this->testDb->conditions(array('score >' => '20'));
|
||||
$expected = " WHERE `score` > '20'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('client_id' => '> 20'), true, true, new TestModel());
|
||||
$expected = " WHERE `client_id` > 20";
|
||||
$result = $this->testDb->conditions(array('client_id >' => '20'), true, true, new TestModel());
|
||||
$expected = " WHERE `client_id` > 20";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('or' => array( 'score' => 'BETWEEN 4 AND 5', 'rating' => '> 20')));
|
||||
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
|
||||
$result = $this->testDb->conditions(array('or' => array(
|
||||
'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
|
||||
)));
|
||||
$expected = " WHERE (`score` BETWEEN '4' AND '5') OR (`rating` > '20')";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('or' => array('score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
|
||||
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
|
||||
$result = $this->testDb->conditions(array('or' => array(
|
||||
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
|
||||
)));
|
||||
$expected = " WHERE (`score` BETWEEN '4' AND '5') OR ((`score` > '20'))";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('and' => array( 'score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
|
||||
$expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
|
||||
$result = $this->testDb->conditions(array('and' => array(
|
||||
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
|
||||
)));
|
||||
$expected = " WHERE (`score` BETWEEN '4' AND '5') AND ((`score` > '20'))";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('published' => 1, 'or' => array('score' => '< 2', array('score' => '> 20')) ));
|
||||
$expected = " WHERE `published` = '1' AND ((`score` < '2') OR (`score` > '20'))";
|
||||
$result = $this->testDb->conditions(array(
|
||||
'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
|
||||
));
|
||||
$expected = " WHERE `published` = 1 AND (`score` > '2') OR ((`score` > '20'))";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array(array('Project.removed' => false)));
|
||||
$this->assertPattern('/^\s*WHERE\s+`Project`.`removed`\s+=\s+0\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+\(`Project`.`removed`\s+=\s+0\)\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array(array('Project.removed' => true)));
|
||||
$this->assertPattern('/^\s*WHERE\s+`Project`.`removed`\s+=\s+1\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+\(`Project`.`removed`\s+=\s+1\)\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array(array('Project.removed' => null)));
|
||||
$this->assertPattern('/^\s*WHERE\s+`Project`.`removed`\s+IS\s+NULL\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+\(`Project`.`removed`\s+IS\s+NULL\)\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('(Usergroup.permissions) & 4' => 4));
|
||||
$this->assertPattern('/^\s*WHERE\s+\(`Usergroup`\.`permissions`\)\s+& 4\s+=\s+\'4\'\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+\(`Usergroup`\.`permissions`\)\s+&\s+4\s+=\s+4\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('((Usergroup.permissions) & 4)' => 4));
|
||||
$this->assertPattern('/^\s*WHERE\s+\(\(`Usergroup`\.`permissions`\)\s+& 4\)\s+=\s+\'4\'\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+\(\(`Usergroup`\.`permissions`\)\s+& 4\)\s+=\s+4\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('Post.modified' => '>= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
|
||||
$expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
|
||||
$result = $this->testDb->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)'));
|
||||
$expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
|
||||
|
@ -1648,60 +1657,60 @@ class DboSourceTest extends CakeTestCase {
|
|||
|
||||
$result = $this->testDb->conditions(array(
|
||||
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
|
||||
'Enrollment.yearcompleted' => '> 0')
|
||||
'Enrollment.yearcompleted >' => '0')
|
||||
);
|
||||
$this->assertPattern('/^\s*WHERE\s+NOT\s+\(\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\s*\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('id' => '<> 8'));
|
||||
$result = $this->testDb->conditions(array('id <>' => '8'));
|
||||
$this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->conditions(array('TestModel.field' => '= gribe$@()lu'));
|
||||
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
|
||||
$result = $this->testDb->conditions(array('TestModel.field =' => 'gribe$@()lu'));
|
||||
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions['NOT'] = array('Listing.expiration' => "BETWEEN 1 AND 100");
|
||||
$conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100"));
|
||||
$conditions[0]['OR'] = array(
|
||||
"Listing.title" => "LIKE %term%",
|
||||
"Listing.description" => "LIKE %term%"
|
||||
"Listing.title LIKE" => "%term%",
|
||||
"Listing.description LIKE" => "%term%"
|
||||
);
|
||||
$conditions[1]['OR'] = array(
|
||||
"Listing.title" => "LIKE %term_2%",
|
||||
"Listing.description" => "LIKE %term_2%"
|
||||
"Listing.title LIKE" => "%term_2%",
|
||||
"Listing.description LIKE" => "%term_2%"
|
||||
);
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE NOT ((`Listing`.`expiration` BETWEEN '1' AND '100')) AND ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
|
||||
$expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah'));
|
||||
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
|
||||
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76) ";
|
||||
$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('title' => 'user(s)');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `title` = 'user(s)'";
|
||||
$expected = " WHERE `title` = 'user(s)'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('title' => 'user(s) data');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `title` = 'user(s) data'";
|
||||
$expected = " WHERE `title` = 'user(s) data'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array('title' => 'user(s,arg) data');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `title` = 'user(s,arg) data'";
|
||||
$expected = " WHERE `title` = 'user(s,arg) data'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM)'));
|
||||
$expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
|
||||
$expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM) '));
|
||||
$expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
|
||||
$expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
|
@ -1709,7 +1718,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$conditions[] = 'User.first_name = \'Firstname\'';
|
||||
$conditions[] = array('User.last_name' => 'Lastname');
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'";
|
||||
$expected = " WHERE `User`.`first_name` = 'Firstname' AND (`User`.`last_name` = 'Lastname')";
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$conditions = array(
|
||||
|
@ -1718,7 +1727,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
'1=1 GROUP BY Thread.project_id'
|
||||
);
|
||||
$result = $this->testDb->conditions($conditions);
|
||||
$this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*\'5\'\s+AND\s+`Thread`.`buyer_id`\s*=\s*\'14\'\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
|
||||
$this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
|
||||
}
|
||||
|
||||
function testConditionsOptionalArguments() {
|
||||
|
@ -2088,7 +2097,10 @@ class DboSourceTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
function testRenderStatement() {
|
||||
$result = $this->testDb->renderStatement('select', array('fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => '', 'joins' => '', 'order' => '', 'limit' => ''));
|
||||
$result = $this->testDb->renderStatement('select', array(
|
||||
'fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1',
|
||||
'alias' => '', 'joins' => '', 'order' => '', 'limit' => '', 'group' => ''
|
||||
));
|
||||
$this->assertPattern('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
|
||||
|
||||
$result = $this->testDb->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => ''));
|
||||
|
@ -2161,15 +2173,15 @@ class DboSourceTest extends CakeTestCase {
|
|||
|
||||
function testMagicMethodQuerying() {
|
||||
$result = $this->testDb->query('findByFieldName', array('value'), $this->Model);
|
||||
$expected = array('TestModel.field_name' => '= value');
|
||||
$expected = array('TestModel.field_name' => 'value');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model);
|
||||
$expected = array('TestModel.field_name' => '= value');
|
||||
$expected = array('TestModel.field_name' => 'value');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->query('findAllById', array('a'), $this->Model);
|
||||
$expected = array('TestModel.id' => '= a');
|
||||
$expected = array('TestModel.id' => 'a');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
|
||||
|
@ -2181,7 +2193,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->query('findByFieldName', array('= a'), $this->Model);
|
||||
$expected = array('TestModel.field_name' => '= = a');
|
||||
$expected = array('TestModel.field_name' => '= a');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->testDb->query('findByFieldName', array(), $this->Model);
|
||||
|
|
|
@ -49,7 +49,7 @@ class ModelTest extends CakeTestCase {
|
|||
'core.document', 'core.device', 'core.document_directory', 'core.primary_model', 'core.secondary_model', 'core.something',
|
||||
'core.something_else', 'core.join_thing', 'core.join_a', 'core.join_b', 'core.join_c', 'core.join_a_b', 'core.join_a_c',
|
||||
'core.uuid', 'core.data_test', 'core.posts_tag', 'core.the_paper_monkies', 'core.person', 'core.underscore_field',
|
||||
'core.node', 'core.dependency', 'core.product'
|
||||
'core.node', 'core.dependency'
|
||||
);
|
||||
|
||||
function start() {
|
||||
|
@ -158,17 +158,17 @@ class ModelTest extends CakeTestCase {
|
|||
'alias' => 'Tag',
|
||||
'limit' => null,
|
||||
'offset' => null,
|
||||
'group' => null,
|
||||
'joins' => array(array(
|
||||
'alias' => 'ArticlesTag',
|
||||
'table' => $this->db->fullTableName('articles_tags'),
|
||||
'conditions' => array(
|
||||
array("ArticlesTag.article_id" => '{$__cakeID__$}'),
|
||||
array("ArticlesTag.tag_id" => '{$__cakeIdentifier[Tag.id]__$}')
|
||||
array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id'))
|
||||
)
|
||||
)),
|
||||
'conditions' => array(),
|
||||
'order' => null,
|
||||
'group' => null
|
||||
'order' => null
|
||||
),
|
||||
$Article
|
||||
);
|
||||
|
@ -302,7 +302,7 @@ class ModelTest extends CakeTestCase {
|
|||
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
|
||||
$TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
|
||||
'foreignKey' => false,
|
||||
'conditions' => array('Comment.user_id' => '= 2')
|
||||
'conditions' => array('Comment.user_id =' => '2')
|
||||
));
|
||||
$result = $TestModel->find('all');
|
||||
$expected = array(
|
||||
|
@ -426,64 +426,8 @@ class ModelTest extends CakeTestCase {
|
|||
'Mother' => array(),
|
||||
'Father' => array())));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testGroupByFind() {
|
||||
$this->loadFixtures('Product');
|
||||
$Product =& new Product();
|
||||
|
||||
$result = $Product->find('all',array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group'=> 'Product.type'));
|
||||
|
||||
$expected = array(
|
||||
0 => array(
|
||||
'Product' => array(
|
||||
'type' => 'Clothing',
|
||||
'price' => 32)
|
||||
),
|
||||
1 => array(
|
||||
'Product' => array(
|
||||
'type' => 'Food',
|
||||
'price' => 9)
|
||||
),
|
||||
2 => array(
|
||||
'Product' => array(
|
||||
'type' => 'Music',
|
||||
'price' => 4)
|
||||
),
|
||||
3 => array(
|
||||
'Product' => array(
|
||||
'type' => 'Toy',
|
||||
'price' => 3)
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testGroupByFindAssociations() {
|
||||
$this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
|
||||
$Thread =& new Thread();
|
||||
$result = $Thread->find('all', array('conditions' => array('Thread.project_id' => 1 ), 'group' => 'Thread.project_id'));
|
||||
$expected = array(
|
||||
array(
|
||||
'Thread' => array(
|
||||
'id' => '1',
|
||||
'project_id' => 1,
|
||||
'name' => 'Project 1, Thread 1',
|
||||
),
|
||||
'Message' => array(
|
||||
array( 'id' => 1,
|
||||
'thread_id' => 1,
|
||||
'name' => 'Thread 1, Message 1'
|
||||
)
|
||||
|
||||
)
|
||||
),
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testIdentity() {
|
||||
$TestModel =& new Test();
|
||||
$result = $TestModel->alias;
|
||||
|
@ -778,7 +722,7 @@ class ModelTest extends CakeTestCase {
|
|||
array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->findAll(array('User.id' => '!= 0', 'User.user' => 'LIKE %arr%'));
|
||||
$result = $TestModel->findAll(array('User.id !=' => '0', 'User.user LIKE' => '%arr%'));
|
||||
$expected = array(
|
||||
array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
|
||||
array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
|
||||
|
@ -788,7 +732,7 @@ class ModelTest extends CakeTestCase {
|
|||
$expected = array();
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->findAll(array('or' => array('User.id' => '0', 'User.user' => 'LIKE %a%')));
|
||||
$result = $TestModel->findAll(array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%')));
|
||||
$expected = array(
|
||||
array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')),
|
||||
array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31')),
|
||||
|
@ -2828,7 +2772,7 @@ class ModelTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->findAllThreaded(array('Category.name' => 'LIKE Category 1%'));
|
||||
$result = $TestModel->findAllThreaded(array('Category.name LIKE' => 'Category 1%'));
|
||||
$expected = array(
|
||||
array(
|
||||
'Category' => array('id' => '1', 'parent_id' => '0', 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
|
||||
|
@ -4147,12 +4091,15 @@ class ModelTest extends CakeTestCase {
|
|||
|
||||
$query = 'SELECT * FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?';
|
||||
$params = array('Y');
|
||||
$this->expectError();
|
||||
ob_start();
|
||||
$result = $Article->query($query, $params);
|
||||
ob_end_clean();
|
||||
$this->assertEqual($result, null);
|
||||
}
|
||||
|
||||
function testVeryStrangeUseCase() {
|
||||
if ($this->db->config['driver'] != 'mssql') {
|
||||
if ($this->db->config['driver'] == 'mssql') {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -4187,6 +4134,37 @@ class ModelTest extends CakeTestCase {
|
|||
$this->assertEqual($currentCount, 4);
|
||||
}
|
||||
|
||||
// function testGroupByFind() {
|
||||
// $this->loadFixtures('Product');
|
||||
// $Product =& new Product();
|
||||
//
|
||||
// $result = $Product->find('all',array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group'=> 'Product.type'));
|
||||
// $expected = array(
|
||||
// array('Product' => array('type' => 'Clothing', 'price' => 32)),
|
||||
// array('Product' => array('type' => 'Food', 'price' => 9)),
|
||||
// array('Product' => array('type' => 'Music', 'price' => 4)),
|
||||
// array('Product' => array('type' => 'Toy', 'price' => 3))
|
||||
// );
|
||||
// $this->assertEqual($result, $expected);
|
||||
// }
|
||||
|
||||
function testGroupByFindAssociations() {
|
||||
$this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
|
||||
$Thread =& new Thread();
|
||||
|
||||
$result = $Thread->find('all', array(
|
||||
'conditions' => array('Thread.project_id' => 1), 'group' => 'Thread.project_id')
|
||||
);
|
||||
$expected = array(
|
||||
array(
|
||||
'Thread' => array('id' => '1', 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
|
||||
'Message' => array(array('id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1'))
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
|
||||
function endTest() {
|
||||
ClassRegistry::flush();
|
||||
}
|
||||
|
|
|
@ -141,6 +141,10 @@ class StringTest extends UnitTestCase {
|
|||
$expected = '<img src="foo" class="bar"/>';
|
||||
$result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = String::insert("this is a ? string", "test");
|
||||
$expected = "this is a test string";
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testTokenize() {
|
||||
|
|
Loading…
Add table
Reference in a new issue