From 4adc042882528a75b3a1a7f379a2cd710090bfc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Marrafa?= Date: Thu, 29 Sep 2011 15:44:05 +0100 Subject: [PATCH] Fix association lazy-loading when used with ContainableBehavior --- lib/Cake/Model/Model.php | 5 ++++ .../Behavior/ContainableBehaviorTest.php | 23 +++++++++++++++++++ lib/Cake/Test/Case/Model/models.php | 14 +++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 6eac1da47..00492be32 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -735,6 +735,11 @@ class Model extends Object { if (isset($name, $this->{$type}[$name])) { $className = empty($this->{$type}[$name]['className']) ? $name : $this->{$type}[$name]['className']; 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') { foreach ($this->{$type} as $k => $relation) { if (empty($relation['with'])) { diff --git a/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php index 8d64cae34..eb6f69862 100644 --- a/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php @@ -3588,6 +3588,29 @@ class ContainableBehaviorTest extends CakeTestCase { $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 * diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index d6a2e6bd0..d46a632cc 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -179,6 +179,20 @@ class User extends CakeTestModel { * @var array */ 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; + } } /**