Skip to content

Commit

Permalink
Merge pull request #5032 from cakephp/issue-5030
Browse files Browse the repository at this point in the history
Fix controller names not reflecting table names.
  • Loading branch information
lorenzo committed Nov 1, 2014
2 parents c6d003b + a174ecd commit 8208588
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/Shell/Task/ViewTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,27 @@ protected function _associations(Table $model) {
$target = $assoc->target();
$assocName = $assoc->name();
$alias = $target->alias();
$targetClass = get_class($target);
list($_, $className) = namespaceSplit($targetClass);

$modelClass = get_class($model);
if ($modelClass !== 'Cake\ORM\Table' && get_class($target) === $modelClass) {
if ($modelClass !== 'Cake\ORM\Table' && $targetClass === $modelClass) {
continue;
}

$className = preg_replace('/(.*)Table$/', '\1', $className);
if ($className === '') {
$className = $alias;
}

$associations[$type][$assocName] = [
'property' => $assoc->property(),
'variable' => Inflector::variable($assocName),
'primaryKey' => (array)$target->primaryKey(),
'displayField' => $target->displayField(),
'foreignKey' => $assoc->foreignKey(),
'controller' => $alias,
'alias' => $alias,
'controller' => $className,
'fields' => $target->schema()->columns(),
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Template/Bake/default/views/form.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ $fields = collection($fields)
foreach ($associations as $type => $data) {
foreach ($data as $alias => $details) {
if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) {
echo "\t\t<li><?= \$this->Html->link(__('List " . Inflector::humanize($details['controller']) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . Inflector::humanize(Inflector::singularize(Inflector::underscore($alias))) . "'), ['controller' => '{$details['controller']}', 'action' => 'add']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('List " . $this->_pluralHumanName($alias) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . $this->_singularHumanName($alias) . "'), ['controller' => '{$details['controller']}', 'action' => 'add']) ?> </li>\n";
$done[] = $details['controller'];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Template/Bake/default/views/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ $fields = collection($fields)
foreach ($associations as $type => $data) {
foreach ($data as $alias => $details) {
if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) {
echo "\t\t<li><?= \$this->Html->link(__('List " . Inflector::humanize($details['controller']) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . Inflector::humanize(Inflector::singularize(Inflector::underscore($alias))) . "'), ['controller' => '{$details['controller']}', 'action' => 'add']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('List " . $this->_pluralHumanName($alias) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . $this->_singularHumanName($alias) . "'), ['controller' => '{$details['controller']}', 'action' => 'add']) ?> </li>\n";
$done[] = $details['controller'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/default/views/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => []
foreach ($associations as $type => $data) {
foreach ($data as $alias => $details) {
if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) {
echo "\t\t<li><?= \$this->Html->link(__('List " . Inflector::humanize($details['controller']) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('List " . $this->_pluralHumanName($alias) . "'), ['controller' => '{$details['controller']}', 'action' => 'index']) ?> </li>\n";
echo "\t\t<li><?= \$this->Html->link(__('New " . Inflector::humanize(Inflector::singularize(Inflector::underscore($alias))) . "'), ['controller' => '{$details['controller']}', 'action' => 'add']) ?> </li>\n";
$done[] = $details['controller'];
}
Expand Down
43 changes: 43 additions & 0 deletions tests/TestCase/Shell/Task/ViewTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,49 @@ public function testGetContent() {
$this->assertContains('$testViewModel->body', $result);
}

/**
* Test getContent with associations
*
* @return void
*/
public function testGetContentAssociations() {
$vars = array(
'modelClass' => 'ViewTaskComments',
'schema' => TableRegistry::get('ViewTaskComments')->schema(),
'primaryKey' => ['id'],
'displayField' => 'name',
'singularVar' => 'viewTaskComment',
'pluralVar' => 'viewTaskComments',
'singularHumanName' => 'View Task Comment',
'pluralHumanName' => 'View Task Comments',
'fields' => ['id', 'name', 'body'],
'associations' => [
'belongsTo' => [
'Authors' => [
'property' => 'author',
'variable' => 'author',
'primaryKey' => ['id'],
'displayField' => 'name',
'foreignKey' => 'author_id',
'alias' => 'Authors',
'controller' => 'ViewTaskAuthors',
'fields' => ['name'],
]
]
],
'keyFields' => [],
);
$result = $this->Task->getContent('view', $vars);

$this->assertContains('Delete View Task Comment', $result);
$this->assertContains('Edit View Task Comment', $result);
$this->assertContains('List Authors', $result);
$this->assertContains('New Author', $result);

$this->assertContains("'controller' => 'ViewTaskAuthors'", $result);
}


/**
* Test getContent with no pk
*
Expand Down

0 comments on commit 8208588

Please sign in to comment.