Skip to content

Commit

Permalink
Remove ordering parameter from dynamic finder.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 2, 2013
1 parent 58b51ce commit a4ab6a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
12 changes: 4 additions & 8 deletions Cake/ORM/Table.php
Expand Up @@ -1251,7 +1251,6 @@ public function _dynamicFinder($method, $args) {
$hasAnd = strpos($fields, '_and_');

$makeConditions = function($fields, $args) {
$order = null;
$conditions = [];
if (count($args) < count($fields)) {
throw new \Cake\Error\Exception(__d(
Expand All @@ -1264,8 +1263,7 @@ public function _dynamicFinder($method, $args) {
foreach ($fields as $field) {
$conditions[$field] = array_shift($args);
}
$order = array_shift($args);
return [$conditions, $order];
return $conditions;
};

if ($hasOr !== false && $hasAnd !== false) {
Expand All @@ -1276,16 +1274,15 @@ public function _dynamicFinder($method, $args) {
}

if ($hasOr === false && $hasAnd === false) {
list($conditions, $order) = $makeConditions([$fields], $args);
$conditions = $makeConditions([$fields], $args);
} elseif ($hasOr !== false) {
$fields = explode('_or_', $fields);
list($conditions, $order) = $makeConditions($fields, $args);
$conditions = [
'OR' => $conditions
'OR' => $makeConditions($fields, $args)
];
} elseif ($hasAnd !== false) {
$fields = explode('_and_', $fields);
list($conditions, $order) = $makeConditions($fields, $args);
$conditions = $makeConditions($fields, $args);
}

if ($findType === 'first') {
Expand All @@ -1295,7 +1292,6 @@ public function _dynamicFinder($method, $args) {

return $this->find($findType, [
'conditions' => $conditions,
'order' => $order,
'limit' => $limit,
]);
}
Expand Down
49 changes: 9 additions & 40 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1947,7 +1947,7 @@ public function testAfterValidate() {
*
* @return void
*/
public function testDynamicFindFirst() {
public function testMagicFindFirst() {
$table = TableRegistry::get('Users');

$result = $table->findByUsername('garrett');
Expand All @@ -1964,7 +1964,7 @@ public function testDynamicFindFirst() {
* @expectedExceptionMessage Not enough arguments to magic finder. Got 0 required 1
* @return void
*/
public function testDynamicFindError() {
public function testMagicFindError() {
$table = TableRegistry::get('Users');

$table->findByUsername();
Expand All @@ -1977,7 +1977,7 @@ public function testDynamicFindError() {
* @expectedExceptionMessage Not enough arguments to magic finder. Got 1 required 2
* @return void
*/
public function testDynamicFindErrorMissingField() {
public function testMagicFindErrorMissingField() {
$table = TableRegistry::get('Users');

$table->findByUsernameAndId('garrett');
Expand All @@ -1990,7 +1990,7 @@ public function testDynamicFindErrorMissingField() {
* @expectedExceptionMessage Cannot mix "and" & "or" in a magic finder. Use find() instead.
* @return void
*/
public function testDynamicFindErrorMixOfOperators() {
public function testMagicFindErrorMixOfOperators() {
$table = TableRegistry::get('Users');

$table->findByUsernameAndIdOrPassword('garrett', 1, 'sekret');
Expand All @@ -2001,7 +2001,7 @@ public function testDynamicFindErrorMixOfOperators() {
*
* @return void
*/
public function testDynamicFindFirstAnd() {
public function testMagicFindFirstAnd() {
$table = TableRegistry::get('Users');

$result = $table->findByUsernameAndId('garrett', 4);
Expand All @@ -2020,7 +2020,7 @@ public function testDynamicFindFirstAnd() {
*
* @return void
*/
public function testDynamicFindFirstOr() {
public function testMagicFindFirstOr() {
$table = TableRegistry::get('Users');

$result = $table->findByUsernameOrId('garrett', 4);
Expand All @@ -2037,28 +2037,12 @@ public function testDynamicFindFirstOr() {
$this->assertEquals($expected, $result->clause('where'));
}

/**
* Test setting order by clauses on magic finders.
*
* @return void
*/
public function testDynamicFindFirstOrderBy() {
$table = TableRegistry::get('Users');

$result = $table->findByAuthorIdOrPublished(1, 'Y', ['id' => 'DESC']);
$this->assertInstanceOf('Cake\ORM\Query', $result);
$this->assertEquals(1, $result->clause('limit'));
$expected = new OrderByExpression(['id' => 'DESC']);
$this->assertEquals($expected, $result->clause('order'));
}


/**
* Test magic findAllByXX method.
*
* @return void
*/
public function testDynamicFindAll() {
public function testMagicFindAll() {
$table = TableRegistry::get('Articles');

$result = $table->findAllByAuthorId(1);
Expand All @@ -2077,7 +2061,7 @@ public function testDynamicFindAll() {
*
* @return void
*/
public function testDynamicFindAllAnd() {
public function testMagicFindAllAnd() {
$table = TableRegistry::get('Users');

$result = $table->findAllByAuthorIdAndPublished(1, 'Y');
Expand All @@ -2094,7 +2078,7 @@ public function testDynamicFindAllAnd() {
*
* @return void
*/
public function testDynamicFindAllOr() {
public function testMagicFindAllOr() {
$table = TableRegistry::get('Users');

$result = $table->findAllByAuthorIdOrPublished(1, 'Y');
Expand All @@ -2108,19 +2092,4 @@ public function testDynamicFindAllOr() {
$this->assertNull($result->clause('order'));
}

/**
* Test setting order by clauses on magic finders.
*
* @return void
*/
public function testDynamicFindAllOrderBy() {
$table = TableRegistry::get('Users');

$result = $table->findAllByAuthorIdOrPublished(1, 'Y', ['id' => 'DESC']);
$this->assertInstanceOf('Cake\ORM\Query', $result);
$this->assertNull($result->clause('limit'));
$expected = new OrderByExpression(['id' => 'DESC']);
$this->assertEquals($expected, $result->clause('order'));
}

}

0 comments on commit a4ab6a9

Please sign in to comment.