mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #7680 from chinpei215/magic-find
Magic call with custom finders
This commit is contained in:
commit
5ac7b875e9
3 changed files with 180 additions and 11 deletions
|
@ -538,16 +538,11 @@ class DboSource extends DataSource {
|
|||
|
||||
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 @@ class DboSource extends DataSource {
|
|||
$conditions = array('OR' => $conditions);
|
||||
}
|
||||
|
||||
if ($all) {
|
||||
if ($findType !== 'first' && $findType !== '') {
|
||||
if (isset($params[3 + $off])) {
|
||||
$limit = $params[3 + $off];
|
||||
}
|
||||
|
@ -592,7 +587,7 @@ class DboSource extends DataSource {
|
|||
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 @@ class DboSourceTest extends CakeTestCase {
|
|||
$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 @@ class ModelReadTest extends BaseModelTest {
|
|||
* @return void
|
||||
*/
|
||||
public function testFindMagic() {
|
||||
$this->loadFixtures('User');
|
||||
$this->loadFixtures('User', 'Comment', 'Article');
|
||||
$TestModel = new User();
|
||||
|
||||
$result = $TestModel->findByUser('mariano');
|
||||
|
@ -7064,6 +7064,113 @@ class ModelReadTest extends BaseModelTest {
|
|||
'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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue