Skip to content

Commit

Permalink
Include belongsTo associations in paginated data.
Browse files Browse the repository at this point in the history
eagerload associated data when showing lists of things. This gets us
closer to the behavior in 2.x.
  • Loading branch information
markstory committed Mar 21, 2014
1 parent 891e3fb commit 1785a74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/Console/Templates/default/actions/controller_actions.ctp
Expand Up @@ -14,6 +14,10 @@
* @since 1.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
$associations = [];
foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
$associations[] = "'" . $assoc->target()->alias() . "'";
}
?>

/**
Expand All @@ -22,6 +26,11 @@
* @return void
*/
public function index() {
<?php if ($associations): ?>
$this->paginate = [
'contain' => [<?= implode(', ', $associations) ?>]
];
<?php endif; ?>
$this->set('<?= $pluralName ?>', $this->paginate($this-><?= $currentModelName ?>));
}

Expand All @@ -33,7 +42,9 @@
* @return void
*/
public function view($id = null) {
$<?= $singularName?> = $this-><?= $currentModelName ?>->get($id);
$<?= $singularName?> = $this-><?= $currentModelName ?>->get($id, [
'contain' => [<?= implode(', ', $associations) ?>]
]);
$this->set('<?= $singularName; ?>', $<?= $singularName; ?>);
}

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Console/Command/Task/ControllerTaskTest.php
Expand Up @@ -30,6 +30,7 @@
class BakeArticlesTable extends Table {

public function initialize(array $config) {
$this->belongsTo('BakeUsers');
$this->hasMany('BakeComments');
$this->belongsToMany('BakeTags');
}
Expand Down
13 changes: 10 additions & 3 deletions tests/bake_compare/Controller/Actions.ctp
Expand Up @@ -5,6 +5,9 @@
* @return void
*/
public function index() {
$this->paginate = [
'contain' => ['BakeUsers']
];
$this->set('bakeArticles', $this->paginate($this->BakeArticles));
}

Expand All @@ -16,7 +19,9 @@
* @return void
*/
public function view($id = null) {
$bakeArticle = $this->BakeArticles->get($id);
$bakeArticle = $this->BakeArticles->get($id, [
'contain' => ['BakeUsers']
]);
$this->set('bakeArticle', $bakeArticle);
}

Expand All @@ -36,8 +41,9 @@
$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));
}
}
$bakeUsers = $this->BakeArticles->association('BakeUsers')->find('list');
$bakeTags = $this->BakeArticles->association('BakeTags')->find('list');
$this->set(compact('bakeTags'));
$this->set(compact('bakeUsers', 'bakeTags'));
}

/**
Expand All @@ -58,8 +64,9 @@
$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));
}
}
$bakeUsers = $this->BakeArticles->association('BakeUsers')->find('list');
$bakeTags = $this->BakeArticles->association('BakeTags')->find('list');
$this->set(compact('bakeTags'));
$this->set(compact('bakeUsers', 'bakeTags'));
}

/**
Expand Down

0 comments on commit 1785a74

Please sign in to comment.