Browse Source

Merge pull request #4928 from cakephp/issue-4914

Fix errors baking tree models.
Mark Story 11 years ago
parent
commit
d0b12bcc22

+ 8 - 0
src/Shell/Task/ModelTask.php

@@ -646,6 +646,14 @@ class ModelTask extends BakeTask {
 		$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;

+ 5 - 0
src/Shell/Task/ViewTask.php

@@ -357,6 +357,11 @@ class ViewTask extends BakeTask {
 			$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);

+ 28 - 1
tests/TestCase/Shell/Task/ViewTaskTest.php

@@ -102,7 +102,7 @@ class ViewTaskTest extends TestCase {
 		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',
@@ -313,6 +313,33 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void