Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added tips to speed up HABTM and corrected false claim.

  • Loading branch information...
commit 421f4a0abf74043c138236dd6b992743cbdf2baf 1 parent 8226422
Frank de Graaf authored

Showing 2 changed files with 61 additions and 4 deletions. Show diff stats Hide diff stats

  1. +12 4 README
  2. +49 0 tests/cases/libs/lazy_model.test.php
16 README
@@ -44,10 +44,18 @@ A: This is by design. When importing a Model using App::import() CakePHP will a
44 44 CakePHP 1.2. It all boils down to some Inflector magic which was implemented earlier.
45 45
46 46 Q: My HABTM isn't completely lazy loaded, is this a bug?
47   -A: No, full lazy loading only works when you specify your own model using the 'with' key in the
48   - association. This is because when using an auto model as join model CakePHP will need the two
49   - other models to guess the name of the join model and they still would be instantiated. So if
50   - you want to speed that up, use a 'with' model or don't use HABTM at all.
  47 +A: No, full lazy loading for HABTM never works, because CakePHP needs to fill all sorts of
  48 + settings in the associations and it needs at least the join model for that. You can optimize
  49 + this though. It will be only half loaded when you set the 'with', 'associationForeignKey' and
  50 + 'joinTable' keys in your HABTM association. In this case CakePHP will only load the join model.
  51 +
  52 + See the test cases for details on how to set it up.
  53 +
  54 + You will notice it will make a bit of a mess. So I suggest you only do this when you really
  55 + need to. It is probably not even worth it. Another way to speed up HABTM is to not use it at
  56 + all. You can split things up as hasMany and belongsTo associations. More on this can be found
  57 + in the book under heading "What to do when HABTM becomes complicated?".
  58 + (See: http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM)
51 59
52 60 Q: ACL breaks on CakePHP 1.2, is this a bug?
53 61 A: Yes, but not one of this plugin. It has to do with the PHP4 compatibility of CakePHP. The
49 tests/cases/libs/lazy_model.test.php
@@ -19,6 +19,41 @@ class Tag extends LazyAppModel {
19 19 public $hasAndBelongsToMany = array('Article');
20 20 }
21 21
  22 +class HalfLazyLoadedArticle extends LazyAppModel {
  23 + public $useTable = 'articles';
  24 + public $belongsTo = array('HalfLazyLoadedUser');
  25 + public $hasAndBelongsToMany = array(
  26 + 'HalfLazyLoadedTag' => array(
  27 + 'className' => 'HalfLazyLoadedTag',
  28 + 'with' => 'HalfLazyLoadedArticlesTag',
  29 + 'joinTable' => 'articles_tags',
  30 + 'associationForeignKey' => 'tag_id'
  31 + )
  32 + );
  33 +}
  34 +
  35 +class HalfLazyLoadedUser extends LazyAppModel {
  36 + public $useTable = 'users';
  37 + public $hasMany = array('HalfLazyLoadedArticle');
  38 +}
  39 +
  40 +class HalfLazyLoadedTag extends LazyAppModel {
  41 + public $useTable = 'Tags';
  42 + public $hasAndBelongsToMany = array(
  43 + 'HalfLazyLoadedArticle' => array(
  44 + 'className' => 'HalfLazyLoadedArticle',
  45 + 'with' => 'HalfLazyLoadedArticlesTag',
  46 + 'joinTable' => 'articles_tags',
  47 + 'associationForeignKey' => 'article_id'
  48 + )
  49 + );
  50 +}
  51 +
  52 +class HalfLazyLoadedArticlesTag extends LazyAppModel {
  53 + public $useTable = 'articles_tags';
  54 + public $belongsTo = array('HalfLazyLoadedArticle', 'HalfLazyLoadedTag');
  55 +}
  56 +
22 57 class LazyModelTestCase extends CakeTestCase {
23 58 public $fixtures = array('core.article', 'core.user', 'core.tag', 'core.articles_tag');
24 59
@@ -45,6 +80,20 @@ public function testLazyLoadingHABTM() {
45 80 $this->assertTrue(property_exists($article, 'ArticlesTag'));
46 81 $this->assertTrue(property_exists($article, 'User'));
47 82 }
  83 +
  84 + public function testOptimizedLazyLoadingHABTM() {
  85 + $article = ClassRegistry::init('HalfLazyLoadedArticle');
  86 +
  87 + $this->assertFalse(property_exists($article, 'HalfLazyLoadedTag'));
  88 + $this->assertTrue(property_exists($article, 'HalfLazyLoadedArticlesTag'));
  89 + $this->assertFalse(property_exists($article, 'HalfLazyLoadedUser'));
  90 +
  91 + $article->HalfLazyLoadedUser->create();
  92 +
  93 + $this->assertFalse(property_exists($article, 'HalfLazyLoadedTag'));
  94 + $this->assertTrue(property_exists($article, 'HalfLazyLoadedArticlesTag'));
  95 + $this->assertTrue(property_exists($article, 'HalfLazyLoadedUser'));
  96 + }
48 97
49 98 public function testNoRecursion() {
50 99 $article = ClassRegistry::init('Article');

0 comments on commit 421f4a0

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