Refactored and updated postConditions to new operator syntax, fixes #4860

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7145 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-06-09 08:29:56 +00:00
parent 2f761ccba1
commit 2d8e63ae7b
2 changed files with 21 additions and 88 deletions

View file

@ -818,21 +818,26 @@ class Controller extends Object {
foreach ($data as $model => $fields) {
foreach ($fields as $field => $value) {
$key = $model . '.' . $field;
if (is_string($op)) {
$cond[$key] = $this->__postConditionMatch($op, $value);
$key = $model.'.'.$field;
$fieldOp = $op;
if (is_array($op) && array_key_exists($key, $op)) {
$fieldOp = $op[$key];
} elseif (is_array($op) && array_key_exists($field, $op)) {
$fieldOp = $op[$field];
} elseif (is_array($op)) {
$opFields = array_keys($op);
if (in_array($key, $opFields) || in_array($field, $opFields)) {
if (in_array($key, $opFields)) {
$cond[$key] = $this->__postConditionMatch($op[$key], $value);
} else {
$cond[$key] = $this->__postConditionMatch($op[$field], $value);
}
} elseif (!$exclusive) {
$cond[$key] = $this->__postConditionMatch(null, $value);
}
$fieldOp = false;
}
if ($exclusive && $fieldOp === false) {
continue;
}
$fieldOp = strtoupper(trim($fieldOp));
if ($fieldOp == 'LIKE') {
$key = $key.' LIKE';
$value = '%'.$value.'%';
} elseif ($fieldOp && $fieldOp != '=') {
$key = $key.' '.$fieldOp;
}
$cond[$key] = $value;
}
}
if ($bool != null && strtoupper($bool) != 'AND') {
@ -840,32 +845,6 @@ class Controller extends Object {
}
return $cond;
}
/**
* Builds a matching condition using the specified operator and value, used by postConditions
*
* @param mixed $op A string containing an SQL comparison operator, or an array matching operators to fields
* @param string $value Value to check against
* @access private
*/
function __postConditionMatch($op, $value) {
if (is_string($op)) {
$op = strtoupper(trim($op));
}
switch($op) {
case '':
case '=':
case null:
return $value;
break;
case 'LIKE':
return 'LIKE %' . $value . '%';
break;
default:
return $op . ' ' . $value;
break;
}
}
/**
* Deprecated, see Model::deconstruct();
*

View file

@ -446,52 +446,6 @@ class ControllerTest extends CakeTestCase {
$this->assertTrue($Controller->_afterScaffoldSaveError(''));
$this->assertFalse($Controller->_scaffoldError(''));
}
/**
* test__postConditionMatch method
*
* @access public
* @return void
*/
function test__postConditionMatch() {
$Controller =& new Controller();
$value = 'val';
$result = $Controller->__postConditionMatch('=', $value);
$expected = $value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('', $value);
$expected = $value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch(null, $value);
$expected = $value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('LIKE', $value);
$expected = 'LIKE %'.$value.'%';
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('>', $value);
$expected = '> '.$value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('<', $value);
$expected = '< '.$value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('>=', $value);
$expected = '>= '.$value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('<=', $value);
$expected = '<= '.$value;
$this->assertIdentical($result, $expected);
$result = $Controller->__postConditionMatch('<>', $value);
$expected = '<> '.$value;
$this->assertIdentical($result, $expected);
}
/**
* testCleanUpFields method
*
@ -737,9 +691,9 @@ class ControllerTest extends CakeTestCase {
'Model3.field3' => '<=',
);
$expected = array(
'Model1.field1' => '> 23',
'Model2.field2' => "LIKE %string%",
'Model3.field3' => '<= 23',
'Model1.field1 >' => '23',
'Model2.field2 LIKE' => "%string%",
'Model3.field3 <=' => '23',
);
$result = $Controller->postConditions($data, $ops);
$this->assertIdentical($result, $expected);