Skip to content
Permalink
Browse files

Magic call with custom finders

Now we can use findListBy, etc - closes #7674
  • Loading branch information...
chinpei215 committed Nov 11, 2015
1 parent aa66293 commit 726699ee7a5323ff8a55876bcefcb8bc83bde1de
@@ -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) {
@@ -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];
}
@@ -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];
@@ -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);
}
/**
@@ -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');
@@ -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);
}
/**

0 comments on commit 726699e

Please sign in to comment.
You can’t perform that action at this time.