Skip to content

Commit

Permalink
Fix generated actions.
Browse files Browse the repository at this point in the history
We need belongsTo on the list page, and all associations with the view
page. Edit and Add need the BelongsTo/BelongsToMany associations.
  • Loading branch information
markstory committed Mar 25, 2014
1 parent e44caab commit 69274ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
56 changes: 35 additions & 21 deletions src/Console/Templates/default/actions/controller_actions.ctp
@@ -1,7 +1,5 @@
<?php
/**
* Bake Template for Controller action generation.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -14,10 +12,26 @@
* @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() . "'";
}
$extractor = function ($val) {
return $val->target()->alias();
};
$stringifyList = function ($list) {
$wrapped = array_map(function ($v) {
return "'$v'";
}, $list);
return implode(', ', $wrapped);
};

$belongsTo = array_map($extractor, $modelObj->associations()->type('BelongsTo'));
$belongsToMany = array_map($extractor, $modelObj->associations()->type('BelongsToMany'));

$editAssociations = array_merge($belongsTo, $belongsToMany);

$allAssociations = array_merge(
$editAssociations,
array_map($extractor, $modelObj->associations()->type('HasOne')),
array_map($extractor, $modelObj->associations()->type('HasMany'))
);
?>

/**
Expand All @@ -26,9 +40,9 @@ foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
* @return void
*/
public function index() {
<?php if ($associations): ?>
<?php if ($belongsTo): ?>
$this->paginate = [
'contain' => [<?= implode(', ', $associations) ?>]
'contain' => [<?= $stringifyList($belongsTo) ?>]
];
<?php endif; ?>
$this->set('<?= $pluralName ?>', $this->paginate($this-><?= $currentModelName ?>));
Expand All @@ -43,7 +57,7 @@ foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
*/
public function view($id = null) {
$<?= $singularName?> = $this-><?= $currentModelName ?>->get($id, [
'contain' => [<?= implode(', ', $associations) ?>]
'contain' => [<?= $stringifyList($allAssociations) ?>]
]);
$this->set('<?= $singularName; ?>', $<?= $singularName; ?>);
}
Expand All @@ -65,15 +79,14 @@ foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
}
}
<?php
foreach (['BelongsTo', 'BelongsToMany'] as $assoc):
foreach ($modelObj->associations()->type($assoc) as $association):
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_pluralName($otherName);
echo "\t\t\${$otherPlural} = \$this->{$currentModelName}->{$otherName}->find('list');\n";
$compact[] = "'{$otherPlural}'";
endforeach;
endforeach;
echo "\t\t\$this->set(compact(" . join(', ', $compact) . "));\n";
echo "\t\t\$this->set(compact(" . join(', ', $compact) . "));\n";
?>
}

Expand All @@ -86,7 +99,9 @@ foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
* @return void
*/
public function edit($id = null) {
$<?= $singularName ?> = $this-><?= $currentModelName ?>->get($id);
$<?= $singularName ?> = $this-><?= $currentModelName ?>->get($id, [
'contain' => [<?= $stringifyList($belongsToMany) ?>]
]);
if ($this->request->is(['post', 'put'])) {
$<?= $singularName ?> = $this-><?= $currentModelName ?>->patchEntity($<?= $singularName ?>, $this->request->data);
if ($this-><?= $currentModelName; ?>->save($<?= $singularName ?>)) {
Expand All @@ -97,13 +112,12 @@ foreach ($modelObj->associations()->type('BelongsTo') as $assoc) {
}
}
<?php
foreach (['BelongsTo', 'BelongsToMany'] as $assoc):
foreach ($modelObj->associations()->type($assoc) as $association):
$otherName = $association->target()->alias();
$otherPlural = $this->_pluralName($otherName);
echo "\t\t\${$otherPlural} = \$this->{$currentModelName}->{$otherName}->find('list');\n";
$compact[] = "'{$otherPlural}'";
endforeach;
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_pluralName($otherName);
echo "\t\t\${$otherPlural} = \$this->{$currentModelName}->{$otherName}->find('list');\n";
$compact[] = "'{$otherPlural}'";
endforeach;
echo "\t\t\$this->set(compact(" . join(', ', $compact) . "));\n";
?>
Expand Down
6 changes: 4 additions & 2 deletions tests/bake_compare/Controller/Actions.ctp
Expand Up @@ -20,7 +20,7 @@
*/
public function view($id = null) {
$bakeArticle = $this->BakeArticles->get($id, [
'contain' => ['BakeUsers']
'contain' => ['BakeUsers', 'BakeTags', 'BakeComments']
]);
$this->set('bakeArticle', $bakeArticle);
}
Expand Down Expand Up @@ -53,7 +53,9 @@
* @return void
*/
public function edit($id = null) {
$bakeArticle = $this->BakeArticles->get($id);
$bakeArticle = $this->BakeArticles->get($id, [
'contain' => ['BakeTags']
]);
if ($this->request->is(['post', 'put'])) {
$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->data);
if ($this->BakeArticles->save($bakeArticle)) {
Expand Down

0 comments on commit 69274ca

Please sign in to comment.