Skip to content

Commit

Permalink
Fix errors baking tree models.
Browse files Browse the repository at this point in the history
When generating code for tree models, bake incorrectly assumed that the
parent/child associations had separate tables. Reloading the registry by
itself should work, however, composer caches that files do not exist and
requires us to manually include the newly generated class.

Refs #4914
  • Loading branch information
markstory committed Oct 19, 2014
1 parent 760dd50 commit 12c2f21
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Shell/Task/ModelTask.php
Expand Up @@ -646,6 +646,14 @@ public function bakeTable($model, array $data = []) {
$filename = $path . 'Table' . DS . $name . 'Table.php';
$this->out("\n" . sprintf('Baking table class for %s...', $name), 1, Shell::QUIET);
$this->createFile($filename, $out);

// Work around composer caching that classes/files do not exist.
// Check for the file as it might not exist in tests.
if (file_exists($filename)) {
require_once($filename);
}
TableRegistry::clear();

$emptyFile = $path . 'Table' . DS . 'empty';
$this->_deleteEmptyFile($emptyFile);
return $out;
Expand Down
5 changes: 5 additions & 0 deletions src/Shell/Task/ViewTask.php
Expand Up @@ -357,6 +357,11 @@ public function getContent($action, $vars = null) {
$vars = $this->_loadController();
}

if (empty($vars['primaryKey'])) {
$this->error('Cannot generate views for models with no primary key');
return false;
}

$this->Template->set('action', $action);
$this->Template->set('plugin', $this->plugin);
$this->Template->set($vars);
Expand Down
29 changes: 28 additions & 1 deletion tests/TestCase/Shell/Task/ViewTaskTest.php
Expand Up @@ -102,7 +102,7 @@ public function setUp() {
parent::setUp();

Configure::write('App.namespace', 'TestApp');
$this->_setupTask(['in', 'err', 'createFile', '_stop']);
$this->_setupTask(['in', 'err', 'error', 'createFile', '_stop']);

TableRegistry::get('ViewTaskComments', [
'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
Expand Down Expand Up @@ -312,6 +312,33 @@ public function testGetContent() {
$this->assertContains('$testViewModel->body', $result);
}

/**
* Test getContent with no pk
*
* @return void
*/
public function testGetContentWithNoPrimaryKey() {
$vars = array(
'modelClass' => 'TestViewModel',
'schema' => TableRegistry::get('ViewTaskComments')->schema(),
'primaryKey' => [],
'displayField' => 'name',
'singularVar' => 'testViewModel',
'pluralVar' => 'testViewModels',
'singularHumanName' => 'Test View Model',
'pluralHumanName' => 'Test View Models',
'fields' => ['id', 'name', 'body'],
'associations' => [],
'keyFields' => [],
);
$this->Task->expects($this->once())
->method('error')
->with($this->stringContains('Cannot generate views for models'));

$result = $this->Task->getContent('view', $vars);
$this->assertFalse($result);
}

/**
* test getContent() using a routing prefix action.
*
Expand Down

0 comments on commit 12c2f21

Please sign in to comment.