Skip to content

Commit

Permalink
Merge pull request #1592 from kimegede/model-after-find-if-condition-…
Browse files Browse the repository at this point in the history
…remove

Reduce if condtion for finds
  • Loading branch information
lorenzo committed Sep 1, 2013
2 parents 1c080b2 + b64c7e3 commit 64cf405
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions lib/Cake/Model/Model.php
Expand Up @@ -2818,9 +2818,9 @@ public function buildQuery($type = 'first', $query = array()) {
protected function _findAll($state, $query, $results = array()) {
if ($state === 'before') {
return $query;
} elseif ($state === 'after') {
return $results;
}

return $results;
}

/**
Expand All @@ -2836,12 +2836,12 @@ protected function _findFirst($state, $query, $results = array()) {
if ($state === 'before') {
$query['limit'] = 1;
return $query;
} elseif ($state === 'after') {
if (empty($results[0])) {
return array();
}
return $results[0];
}

if (empty($results[0])) {
return array();
}
return $results[0];
}

/**
Expand Down Expand Up @@ -2877,17 +2877,17 @@ protected function _findCount($state, $query, $results = array()) {
));
}
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']);
}

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;
}
return false;
}

/**
Expand Down Expand Up @@ -2939,12 +2939,12 @@ protected function _findList($state, $query, $results = array()) {
}
list($query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']) = $list;
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']);
}

/**
Expand Down Expand Up @@ -2974,34 +2974,34 @@ protected function _findNeighbors($state, $query, $results = array()) {
$query['field'] = $field;
$query['value'] = $value;
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;
}

/**
Expand All @@ -3016,16 +3016,16 @@ protected function _findNeighbors($state, $query, $results = array()) {
protected function _findThreaded($state, $query, $results = array()) {
if ($state === 'before') {
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
));
}

/**
Expand Down

0 comments on commit 64cf405

Please sign in to comment.