Fix association lazy-loading when used with ContainableBehavior

This commit is contained in:
Gonçalo Marrafa 2011-09-29 15:44:05 +01:00 committed by Jose Lorenzo Rodriguez
parent 0b6c93cf82
commit 4adc042882
3 changed files with 42 additions and 0 deletions

View file

@ -735,6 +735,11 @@ class Model extends Object {
if (isset($name, $this->{$type}[$name])) { if (isset($name, $this->{$type}[$name])) {
$className = empty($this->{$type}[$name]['className']) ? $name : $this->{$type}[$name]['className']; $className = empty($this->{$type}[$name]['className']) ? $name : $this->{$type}[$name]['className'];
break; break;
}
elseif (isset($name, $this->__backAssociation[$type][$name])) {
$className = empty($this->__backAssociation[$type][$name]['className']) ?
$name : $this->__backAssociation[$type][$name]['className'];
break;
} else if ($type == 'hasAndBelongsToMany') { } else if ($type == 'hasAndBelongsToMany') {
foreach ($this->{$type} as $k => $relation) { foreach ($this->{$type} as $k => $relation) {
if (empty($relation['with'])) { if (empty($relation['with'])) {

View file

@ -3588,6 +3588,29 @@ class ContainableBehaviorTest extends CakeTestCase {
$this->assertEmpty($result, 'Should be empty.'); $this->assertEmpty($result, 'Should be empty.');
} }
/**
* testLazyLoad method
*
* @return void
*/
public function testLazyLoad() {
// Local set up
$this->User = ClassRegistry::init('User');
$this->User->bindModel(array(
'hasMany' => array('Article', 'ArticleFeatured', 'Comment')
), false);
try {
$this->User->find('first', array(
'contain' => 'Comment',
'lazyLoad' => true
));
} catch (Exception $e) {
$exceptions = true;
}
$this->assertTrue(empty($exceptions));
}
/** /**
* containments method * containments method
* *

View file

@ -179,6 +179,20 @@ class User extends CakeTestModel {
* @var array * @var array
*/ */
public $validate = array('user' => 'notEmpty', 'password' => 'notEmpty'); public $validate = array('user' => 'notEmpty', 'password' => 'notEmpty');
/**
* beforeFind() callback used to run ContainableBehaviorTest::testLazyLoad()
*
* @return bool
*/
public function beforeFind ($queryData) {
if (!empty($queryData['lazyLoad'])) {
if (!isset($this->Article, $this->Comment, $this->ArticleFeatured)) {
throw new Exception('Unavailable associations');
}
}
return true;
}
} }
/** /**