Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tips to speed up HABTM and corrected false claim.

  • Loading branch information...
commit 421f4a0abf74043c138236dd6b992743cbdf2baf 1 parent 8226422
@Phally authored
Showing with 61 additions and 4 deletions.
  1. +12 −4 README
  2. +49 −0 tests/cases/libs/lazy_model.test.php
View
16 README
@@ -44,10 +44,18 @@ A: This is by design. When importing a Model using App::import() CakePHP will a
CakePHP 1.2. It all boils down to some Inflector magic which was implemented earlier.
Q: My HABTM isn't completely lazy loaded, is this a bug?
-A: No, full lazy loading only works when you specify your own model using the 'with' key in the
- association. This is because when using an auto model as join model CakePHP will need the two
- other models to guess the name of the join model and they still would be instantiated. So if
- you want to speed that up, use a 'with' model or don't use HABTM at all.
+A: No, full lazy loading for HABTM never works, because CakePHP needs to fill all sorts of
+ settings in the associations and it needs at least the join model for that. You can optimize
+ this though. It will be only half loaded when you set the 'with', 'associationForeignKey' and
+ 'joinTable' keys in your HABTM association. In this case CakePHP will only load the join model.
+
+ See the test cases for details on how to set it up.
+
+ You will notice it will make a bit of a mess. So I suggest you only do this when you really
+ need to. It is probably not even worth it. Another way to speed up HABTM is to not use it at
+ all. You can split things up as hasMany and belongsTo associations. More on this can be found
+ in the book under heading "What to do when HABTM becomes complicated?".
+ (See: http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM)
Q: ACL breaks on CakePHP 1.2, is this a bug?
A: Yes, but not one of this plugin. It has to do with the PHP4 compatibility of CakePHP. The
View
49 tests/cases/libs/lazy_model.test.php
@@ -19,6 +19,41 @@ class Tag extends LazyAppModel {
public $hasAndBelongsToMany = array('Article');
}
+class HalfLazyLoadedArticle extends LazyAppModel {
+ public $useTable = 'articles';
+ public $belongsTo = array('HalfLazyLoadedUser');
+ public $hasAndBelongsToMany = array(
+ 'HalfLazyLoadedTag' => array(
+ 'className' => 'HalfLazyLoadedTag',
+ 'with' => 'HalfLazyLoadedArticlesTag',
+ 'joinTable' => 'articles_tags',
+ 'associationForeignKey' => 'tag_id'
+ )
+ );
+}
+
+class HalfLazyLoadedUser extends LazyAppModel {
+ public $useTable = 'users';
+ public $hasMany = array('HalfLazyLoadedArticle');
+}
+
+class HalfLazyLoadedTag extends LazyAppModel {
+ public $useTable = 'Tags';
+ public $hasAndBelongsToMany = array(
+ 'HalfLazyLoadedArticle' => array(
+ 'className' => 'HalfLazyLoadedArticle',
+ 'with' => 'HalfLazyLoadedArticlesTag',
+ 'joinTable' => 'articles_tags',
+ 'associationForeignKey' => 'article_id'
+ )
+ );
+}
+
+class HalfLazyLoadedArticlesTag extends LazyAppModel {
+ public $useTable = 'articles_tags';
+ public $belongsTo = array('HalfLazyLoadedArticle', 'HalfLazyLoadedTag');
+}
+
class LazyModelTestCase extends CakeTestCase {
public $fixtures = array('core.article', 'core.user', 'core.tag', 'core.articles_tag');
@@ -45,6 +80,20 @@ public function testLazyLoadingHABTM() {
$this->assertTrue(property_exists($article, 'ArticlesTag'));
$this->assertTrue(property_exists($article, 'User'));
}
+
+ public function testOptimizedLazyLoadingHABTM() {
+ $article = ClassRegistry::init('HalfLazyLoadedArticle');
+
+ $this->assertFalse(property_exists($article, 'HalfLazyLoadedTag'));
+ $this->assertTrue(property_exists($article, 'HalfLazyLoadedArticlesTag'));
+ $this->assertFalse(property_exists($article, 'HalfLazyLoadedUser'));
+
+ $article->HalfLazyLoadedUser->create();
+
+ $this->assertFalse(property_exists($article, 'HalfLazyLoadedTag'));
+ $this->assertTrue(property_exists($article, 'HalfLazyLoadedArticlesTag'));
+ $this->assertTrue(property_exists($article, 'HalfLazyLoadedUser'));
+ }
public function testNoRecursion() {
$article = ClassRegistry::init('Article');

0 comments on commit 421f4a0

Please sign in to comment.
Something went wrong with that request. Please try again.