Merge pull request #1592 from kimegede/model-after-find-if-condition-remove

Reduce if condtion for finds
This commit is contained in:
José Lorenzo Rodríguez 2013-09-01 11:24:23 -07:00
commit 64cf40558f

View file

@ -2818,9 +2818,9 @@ class Model extends Object implements CakeEventListener {
protected function _findAll($state, $query, $results = array()) { protected function _findAll($state, $query, $results = array()) {
if ($state === 'before') { if ($state === 'before') {
return $query; return $query;
} elseif ($state === 'after') {
return $results;
} }
return $results;
} }
/** /**
@ -2836,12 +2836,12 @@ class Model extends Object implements CakeEventListener {
if ($state === 'before') { if ($state === 'before') {
$query['limit'] = 1; $query['limit'] = 1;
return $query; return $query;
} elseif ($state === 'after') {
if (empty($results[0])) {
return array();
}
return $results[0];
} }
if (empty($results[0])) {
return array();
}
return $results[0];
} }
/** /**
@ -2877,17 +2877,17 @@ class Model extends Object implements CakeEventListener {
)); ));
} }
return $query; return $query;
} elseif ($state === 'after') {
foreach (array(0, $this->alias) as $key) {
if (isset($results[0][$key]['count'])) {
if ($query['group']) {
return count($results);
}
return intval($results[0][$key]['count']);
}
}
return false;
} }
foreach (array(0, $this->alias) as $key) {
if (isset($results[0][$key]['count'])) {
if ($query['group']) {
return count($results);
}
return intval($results[0][$key]['count']);
}
}
return false;
} }
/** /**
@ -2939,12 +2939,12 @@ class Model extends Object implements CakeEventListener {
} }
list($query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']) = $list; list($query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']) = $list;
return $query; return $query;
} elseif ($state === 'after') {
if (empty($results)) {
return array();
}
return Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']);
} }
if (empty($results)) {
return array();
}
return Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']);
} }
/** /**
@ -2974,34 +2974,34 @@ class Model extends Object implements CakeEventListener {
$query['field'] = $field; $query['field'] = $field;
$query['value'] = $value; $query['value'] = $value;
return $query; return $query;
} elseif ($state === 'after') {
extract($query);
unset($query['conditions'][$field . ' <']);
$return = array();
if (isset($results[0])) {
$prevVal = Hash::get($results[0], $field);
$query['conditions'][$field . ' >='] = $prevVal;
$query['conditions'][$field . ' !='] = $value;
$query['limit'] = 2;
} else {
$return['prev'] = null;
$query['conditions'][$field . ' >'] = $value;
$query['limit'] = 1;
}
$query['order'] = $field . ' ASC';
$neighbors = $this->find('all', $query);
if (!array_key_exists('prev', $return)) {
$return['prev'] = isset($neighbors[0]) ? $neighbors[0] : null;
}
if (count($neighbors) === 2) {
$return['next'] = $neighbors[1];
} elseif (count($neighbors) === 1 && !$return['prev']) {
$return['next'] = $neighbors[0];
} else {
$return['next'] = null;
}
return $return;
} }
extract($query);
unset($query['conditions'][$field . ' <']);
$return = array();
if (isset($results[0])) {
$prevVal = Hash::get($results[0], $field);
$query['conditions'][$field . ' >='] = $prevVal;
$query['conditions'][$field . ' !='] = $value;
$query['limit'] = 2;
} else {
$return['prev'] = null;
$query['conditions'][$field . ' >'] = $value;
$query['limit'] = 1;
}
$query['order'] = $field . ' ASC';
$neighbors = $this->find('all', $query);
if (!array_key_exists('prev', $return)) {
$return['prev'] = isset($neighbors[0]) ? $neighbors[0] : null;
}
if (count($neighbors) === 2) {
$return['next'] = $neighbors[1];
} elseif (count($neighbors) === 1 && !$return['prev']) {
$return['next'] = $neighbors[0];
} else {
$return['next'] = null;
}
return $return;
} }
/** /**
@ -3016,16 +3016,16 @@ class Model extends Object implements CakeEventListener {
protected function _findThreaded($state, $query, $results = array()) { protected function _findThreaded($state, $query, $results = array()) {
if ($state === 'before') { if ($state === 'before') {
return $query; return $query;
} elseif ($state === 'after') {
$parent = 'parent_id';
if (isset($query['parent'])) {
$parent = $query['parent'];
}
return Hash::nest($results, array(
'idPath' => '{n}.' . $this->alias . '.' . $this->primaryKey,
'parentPath' => '{n}.' . $this->alias . '.' . $parent
));
} }
$parent = 'parent_id';
if (isset($query['parent'])) {
$parent = $query['parent'];
}
return Hash::nest($results, array(
'idPath' => '{n}.' . $this->alias . '.' . $this->primaryKey,
'parentPath' => '{n}.' . $this->alias . '.' . $parent
));
} }
/** /**