Skip to content

Commit

Permalink
Magic call with custom finders
Browse files Browse the repository at this point in the history
Now we can use findListBy, etc - closes #7674
  • Loading branch information
chinpei215 committed Nov 11, 2015
1 parent aa66293 commit 726699e
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 11 deletions.
15 changes: 5 additions & 10 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -538,16 +538,11 @@ public function query() {

if (count($args) === 1) {
return $this->fetchAll($args[0]);
} elseif (count($args) > 1 && (strpos($args[0], 'findBy') === 0 || strpos($args[0], 'findAllBy') === 0)) {
} elseif (count($args) > 1 && preg_match('/^find(\w*)By(.+)/', $args[0], $matches)) {
$params = $args[1];

if (substr($args[0], 0, 6) === 'findBy') {
$all = false;
$field = Inflector::underscore(substr($args[0], 6));
} else {
$all = true;
$field = Inflector::underscore(substr($args[0], 9));
}
$findType = lcfirst($matches[1]);
$field = Inflector::underscore($matches[2]);

$or = (strpos($field, '_or_') !== false);
if ($or) {
Expand Down Expand Up @@ -580,7 +575,7 @@ public function query() {
$conditions = array('OR' => $conditions);
}

if ($all) {
if ($findType !== 'first' && $findType !== '') {
if (isset($params[3 + $off])) {
$limit = $params[3 + $off];
}
Expand All @@ -592,7 +587,7 @@ public function query() {
if (isset($params[5 + $off])) {
$recursive = $params[5 + $off];
}
return $args[2]->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
return $args[2]->find($findType, compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
}
if (isset($params[3 + $off])) {
$recursive = $params[3 + $off];
Expand Down
67 changes: 67 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -542,6 +542,73 @@ public function testMagicMethodQuerying() {
$result = $this->db->query('findByFieldName', array(), $this->Model);
$expected = false;
$this->assertEquals($expected, $result);

// findBy<X>And<Y>
$result = $this->db->query('findByFieldXAndFieldY', array('x', 'y'), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_x' => 'x', 'TestModel.field_y' => 'y'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEquals($expected, $result);

// findBy<X>Or<Y>
$result = $this->db->query('findByFieldXOrFieldY', array('x', 'y'), $this->Model);
$expected = array('first', array(
'conditions' => array('OR' => array('TestModel.field_x' => 'x', 'TestModel.field_y' => 'y')),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEquals($expected, $result);

// findMyFancySearchBy<X>
$result = $this->db->query('findMyFancySearchByFieldX', array('x'), $this->Model);
$expected = array('myFancySearch', array(
'conditions' => array('TestModel.field_x' => 'x'),
'fields' => null, 'order' => null, 'limit' => null,
'page' => null, 'recursive' => null
));
$this->assertEquals($expected, $result);

// findFirstBy<X>
$result = $this->db->query('findFirstByFieldX', array('x'), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_x' => 'x'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEquals($expected, $result);

// findBy<X> with optional parameters
$result = $this->db->query('findByFieldX', array('x', 'y', 'priority', -1), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_x' => 'x'),
'fields' => 'y', 'order' => 'priority', 'recursive' => -1
));
$this->assertEquals($expected, $result);

// findBy<X>And<Y> with optional parameters
$result = $this->db->query('findByFieldXAndFieldY', array('x', 'y', 'z', 'priority', -1), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_x' => 'x', 'TestModel.field_y' => 'y'),
'fields' => 'z', 'order' => 'priority', 'recursive' => -1
));
$this->assertEquals($expected, $result);

// findAllBy<X> with optional parameters
$result = $this->db->query('findAllByFieldX', array('x', 'y', 'priority', 10, 2, -1), $this->Model);
$expected = array('all', array(
'conditions' => array('TestModel.field_x' => 'x'),
'fields' => 'y', 'order' => 'priority', 'limit' => 10,
'page' => 2, 'recursive' => -1
));
$this->assertEquals($expected, $result);

// findAllBy<X>And<Y> with optional parameters
$result = $this->db->query('findAllByFieldXAndFieldY', array('x', 'y', 'z', 'priority', 10, 2, -1), $this->Model);
$expected = array('all', array(
'conditions' => array('TestModel.field_x' => 'x', 'TestModel.field_y' => 'y'),
'fields' => 'z', 'order' => 'priority', 'limit' => 10,
'page' => 2, 'recursive' => -1
));
$this->assertEquals($expected, $result);
}

/**
Expand Down
109 changes: 108 additions & 1 deletion lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -7041,7 +7041,7 @@ public function testFindCountWithDbExpressions() {
* @return void
*/
public function testFindMagic() {
$this->loadFixtures('User');
$this->loadFixtures('User', 'Comment', 'Article');
$TestModel = new User();

$result = $TestModel->findByUser('mariano');
Expand All @@ -7064,6 +7064,113 @@ public function testFindMagic() {
'updated' => '2007-03-17 01:18:31'
));
$this->assertEquals($expected, $result);

$Comment = new Comment();
$Comment->recursive = -1;
$results = $Comment->findAllByUserId(1);
$expected = array(
array(
'Comment' => array(
'id' => 3,
'article_id' => 1,
'user_id' => 1,
'comment' => 'Third Comment for First Article',
'published' => 'Y',
'created' => '2007-03-18 10:49:23',
'updated' => '2007-03-18 10:51:31'
)
),
array(
'Comment' => array(
'id' => 4,
'article_id' => 1,
'user_id' => 1,
'comment' => 'Fourth Comment for First Article',
'published' => 'N',
'created' => '2007-03-18 10:51:23',
'updated' => '2007-03-18 10:53:31'
)
),
array(
'Comment' => array(
'id' => 5,
'article_id' => 2,
'user_id' => 1,
'comment' => 'First Comment for Second Article',
'published' => 'Y',
'created' => '2007-03-18 10:53:23',
'updated' => '2007-03-18 10:55:31'
)
)
);
$this->assertEquals($expected, $results);

$results = $Comment->findAllByUserIdAndPublished(1, 'Y');
$expected = array(
array(
'Comment' => array(
'id' => 3,
'article_id' => 1,
'user_id' => 1,
'comment' => 'Third Comment for First Article',
'published' => 'Y',
'created' => '2007-03-18 10:49:23',
'updated' => '2007-03-18 10:51:31'
)
),
array(
'Comment' => array(
'id' => 5,
'article_id' => 2,
'user_id' => 1,
'comment' => 'First Comment for Second Article',
'published' => 'Y',
'created' => '2007-03-18 10:53:23',
'updated' => '2007-03-18 10:55:31'
)
)
);
$this->assertEquals($expected, $results);

$Article = new CustomArticle();
$Article->recursive = -1;
$results = $Article->findListByUserId(1);
$expected = array(
1 => 'First Article',
3 => 'Third Article'
);
$this->assertEquals($expected, $results);

$results = $Article->findPublishedByUserId(1);
$expected = array(
array(
'CustomArticle' => array(
'id' => 1,
'user_id' => 1,
'title' => 'First Article',
'body' => 'First Article Body',
'published' => 'Y',
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31'
)
),
array(
'CustomArticle' => array(
'id' => 3,
'user_id' => 1,
'title' => 'Third Article',
'body' => 'Third Article Body',
'published' => 'Y',
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31'
)
)
);
$this->assertEquals($expected, $results);

$results = $Article->findUnPublishedByUserId(1);
$expected = array();
$this->assertEquals($expected, $results);
}

/**
Expand Down

0 comments on commit 726699e

Please sign in to comment.