diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index ac9891724..b9e5c1a8f 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -61,6 +61,11 @@ class ContainableBehavior extends ModelBehavior { * * - autoFields: (boolean, optional) auto-add needed fields to fetch requested * bindings. DEFAULTS TO: true + * + * - countReset: (boolean, optional) If set to false, count queries will not reset containments + * like normal queries would. Useful for using contain() and pagination. + * DEFAULTS TO: false + * * @param object $Model Model using the behavior * @param array $settings Settings to override for model. @@ -68,7 +73,7 @@ class ContainableBehavior extends ModelBehavior { */ function setup(&$Model, $settings = array()) { if (!isset($this->settings[$Model->alias])) { - $this->settings[$Model->alias] = array('recursive' => true, 'notices' => true, 'autoFields' => true); + $this->settings[$Model->alias] = array('recursive' => true, 'notices' => true, 'autoFields' => true, 'countReset' => false); } $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], ife(is_array($settings), $settings, array())); } @@ -92,7 +97,10 @@ class ContainableBehavior extends ModelBehavior { * @access public */ function beforeFind(&$Model, $query) { - $reset = (isset($query['reset']) ? $query['reset'] : true); + $reset = $this->settings[$Model->alias]['countReset'] || $Model->findQueryType != 'count'; + if (isset($query['reset'])) { + $reset = $query['reset']; + } $noContain = ((isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) || (isset($query['contain']) && empty($query['contain']))); $contain = array(); if (isset($this->runtime[$Model->alias]['contain'])) { diff --git a/cake/tests/cases/libs/model/behaviors/containable.test.php b/cake/tests/cases/libs/model/behaviors/containable.test.php index 4e6d1f959..17c5dd551 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -2931,7 +2931,7 @@ class ContainableTest extends CakeTestCase { $Controller->params['url'] = array(); $Controller->constructClasses(); $Controller->paginate = array('Article' => array('fields' => array('title'))); - $Controller->Article->contain(false, array('User(user)')); + $Controller->Article->contain(array('User(user)')); $result = $Controller->paginate('Article'); $Controller->Article->resetBindings(); $expected = array(