Improving field detection in Containable. paginate() now avoids sending recursive to find calls when it is not explicitly set. Adding more tests to Containable to show how to paginate, fixes #4746

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7030 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2008-05-23 16:12:14 +00:00
parent df5fd8160f
commit 9c722b6999
3 changed files with 89 additions and 13 deletions

View file

@ -560,7 +560,7 @@ class Controller extends Object {
/**
* undocumented function
*
* @param string $status
* @param string $status
* @return void
* @access public
*/
@ -977,7 +977,9 @@ class Controller extends Object {
} elseif (is_string($scope)) {
$conditions = array($conditions, $scope);
}
$recursive = $object->recursive;
if ($recursive === null) {
$recursive = $object->recursive;
}
$type = 'all';
if (isset($defaults[0])) {
$type = array_shift($defaults);
@ -986,7 +988,11 @@ class Controller extends Object {
if (method_exists($object, 'paginateCount')) {
$count = $object->paginateCount($conditions, $recursive);
} else {
$count = $object->find('count', array_merge(compact('conditions', 'recursive'), $extra));
$parameters = compact('conditions');
if ($recursive != $object->recursive) {
$parameters['recursive'] = $recursive;
}
$count = $object->find('count', array_merge($parameters, $extra));
}
$pageCount = intval(ceil($count / $limit));
@ -999,7 +1005,11 @@ class Controller extends Object {
if (method_exists($object, 'paginate')) {
$results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive);
} else {
$results = $object->find($type, array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'), $extra));
$parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
if ($recursive != $object->recursive) {
$parameters['recursive'] = $recursive;
}
$results = $object->find($type, array_merge($parameters, $extra));
}
$paging = array(
'page' => $page,

View file

@ -346,14 +346,10 @@ class ContainableBehavior extends ModelBehavior {
foreach($map as $parent => $children) {
foreach($children as $type => $bindings) {
foreach($bindings as $dependency) {
switch($type) {
case 'belongsTo':
$fields[$parent][] = $dependency . '.--primaryKey--';
break;
case 'hasAndBelongsToMany':
$fields[$parent][] = '--primaryKey--';
$fields[$dependency][] = '--primaryKey--';
break;
if ($type == 'hasAndBelongsToMany') {
$fields[$parent][] = '--primaryKey--';
} else if ($type == 'belongsTo') {
$fields[$parent][] = $dependency . '.--primaryKey--';
}
}
}

View file

@ -105,12 +105,18 @@ class ContainableTest extends CakeTestCase {
$this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user'));
$this->assertTrue(Set::matches('/Comment/keep/Attachment', $r));
$this->assertEqual(Set::extract('/Comment/keep/Attachment/fields', $r), array('attachment'));
$r = $this->__containments($this->Article, array('Comment' => array('limit' => 1)));
$this->assertEqual(array_keys($r), array('Comment', 'Article'));
$this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array()));
$this->assertTrue(Set::matches('/Article/keep/Comment', $r));
$this->assertEqual(array_shift(Set::extract('/Article/keep/Comment/.', $r)), array('limit' => 1));
$r = $this->__containments($this->Article, array('Comment.User'));
$this->assertEqual(array_keys($r), array('User', 'Comment', 'Article'));
$this->assertEqual(array_shift(Set::extract('/User/keep', $r)), array('keep' => array()));
$this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array('User' => array())));
$this->assertEqual(array_shift(Set::extract('/Article/keep', $r)), array('keep' => array('Comment' => array())));
}
function testInvalidContainments() {
@ -2970,12 +2976,15 @@ class ContainableTest extends CakeTestCase {
3 => 'Third Article'
);
$this->assertEqual($result, $expected);
}
function testPaginate() {
$Controller =& new Controller();
$Controller->uses = array('Article');
$Controller->passedArgs[] = '1';
$Controller->params['url'] = array();
$Controller->constructClasses();
$Controller->paginate = array('Article' => array('fields' => array('title'), 'contain' => array('User(user)')));
$result = $Controller->paginate('Article');
$expected = array(
@ -2989,6 +2998,67 @@ class ContainableTest extends CakeTestCase {
$this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Tag[id=1]', $r));
$Controller->paginate = array('Article' => array('contain' => array('Comment(comment)' => 'User(user)'), 'fields' => array('title')));
$result = $Controller->paginate('Article');
$expected = array(
array(
'Article' => array('title' => 'First Article', 'id' => 1),
'Comment' => array(
array(
'comment' => 'First Comment for First Article',
'user_id' => 2,
'article_id' => 1,
'User' => array('user' => 'nate')
),
array(
'comment' => 'Second Comment for First Article',
'user_id' => 4,
'article_id' => 1,
'User' => array('user' => 'garrett')
),
array(
'comment' => 'Third Comment for First Article',
'user_id' => 1,
'article_id' => 1,
'User' => array('user' => 'mariano')
),
array(
'comment' => 'Fourth Comment for First Article',
'user_id' => 1,
'article_id' => 1,
'User' => array('user' => 'mariano')
)
)
),
array(
'Article' => array('title' => 'Second Article', 'id' => 2),
'Comment' => array(
array(
'comment' => 'First Comment for Second Article',
'user_id' => 1,
'article_id' => 2,
'User' => array('user' => 'mariano')
),
array(
'comment' => 'Second Comment for Second Article',
'user_id' => 2,
'article_id' => 2,
'User' => array('user' => 'nate')
)
)
),
array(
'Article' => array('title' => 'Third Article', 'id' => 3),
'Comment' => array()
),
);
$this->assertEqual($result, $expected);
$r = $Controller->Article->find('all');
$this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Tag[id=1]', $r));
}
function __containments(&$Model, $contain = array()) {