Correcting issue where TranslateBehavior hijacks find() calls from Model::exists(), fixes #4765

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7225 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-06-21 02:33:04 +00:00
parent df75a06756
commit 7b6ee7386b
2 changed files with 29 additions and 17 deletions

View file

@ -1696,6 +1696,10 @@ class Model extends Overloadable {
* @access public
*/
function exists($reset = false) {
if (is_array($reset)) {
extract($reset, EXTR_OVERWRITE);
}
if ($this->getID() === false || $this->useTable === false) {
return false;
}
@ -1703,9 +1707,12 @@ class Model extends Overloadable {
return $this->__exists;
}
$conditions = array($this->alias . '.' . $this->primaryKey => $this->getID());
$recursive = -1;
$query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false);
return $this->__exists = ($this->find('count', compact('conditions', 'recursive')) > 0);
if (is_array($reset)) {
$query = array_merge($query, $reset);
}
return $this->__exists = ($this->find('count', $query) > 0);
}
/**
* Returns true if a record that meets given conditions exists
@ -1762,8 +1769,8 @@ class Model extends Overloadable {
$query = array_merge(
array(
'conditions' => null, 'fields' => null, 'joins' => array(),
'limit' => null, 'offset' => null, 'order' => null, 'page' => null, 'group' => null
'conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => null,
'offset' => null, 'order' => null, 'page' => null, 'group' => null, 'callbacks' => true
),
(array)$query
);
@ -1785,18 +1792,20 @@ class Model extends Overloadable {
}
$query['order'] = array($query['order']);
$return = $this->Behaviors->trigger($this, 'beforeFind', array($query), array('break' => true, 'breakOn' => false, 'modParams' => true));
$query = ife(is_array($return), $return, $query);
if ($query['callbacks'] === true || $query['callbacks'] === 'before') {
$return = $this->Behaviors->trigger($this, 'beforeFind', array($query), array('break' => true, 'breakOn' => false, 'modParams' => true));
$query = ife(is_array($return), $return, $query);
if ($return === false) {
return null;
}
if ($return === false) {
return null;
}
$return = $this->beforeFind($query);
$query = ife(is_array($return), $return, $query);
$return = $this->beforeFind($query);
$query = ife(is_array($return), $return, $query);
if ($return === false) {
return null;
if ($return === false) {
return null;
}
}
$results = $db->read($this, $query);
@ -1804,7 +1813,10 @@ class Model extends Overloadable {
$this->findQueryType = null;
if ($type === 'all') {
return $this->__filterResults($results);
if ($query['callbacks'] === true || $query['callbacks'] === 'after') {
return $this->__filterResults($results);
}
return $results;
} else {
if ($this->__findMethods[$type] === true) {
return $this->{'_find' . ucfirst($type)}('after', $query, $results);

View file

@ -1369,10 +1369,10 @@ class NumberTreeCase extends CakeTestCase {
$result = $this->FlagTree->read();
$expected = array(
'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => null, 'lft' => 3, 'rght' => 4),
'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
'Name' => array(
array('id' => 4, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
array('id' => 5, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
),
);
$this->assertEqual($result, $expected);