Browse Source

allow baking models with missing tables - even for rdbms

test updated to reflect expected new expected behavior. If you bake a
model with the table missing, if forced to run skip any part of the
process that would result in an exception.
AD7six 14 years ago
parent
commit
8e38f666b7

+ 24 - 15
lib/Cake/Console/Command/Task/ModelTask.php

@@ -188,7 +188,7 @@ class ModelTask extends BakeTask {
 		$db = ConnectionManager::getDataSource($this->connection);
 		$fullTableName = $db->fullTableName($useTable);
 		if (!in_array($useTable, $this->_tables)) {
-			$prompt = __d('cake_console', "The table $useTable doesn't exist or could not be automatically detected\ncontinue anyway?");
+			$prompt = __d('cake_console', "The table %s doesn't exist or could not be automatically detected\ncontinue anyway?", $useTable);
 			$continue = $this->in($prompt, array('y', 'n'));
 			if (strtolower($continue) == 'n') {
 				return false;
@@ -196,26 +196,35 @@ class ModelTask extends BakeTask {
 		}
 
 		$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
-		$fields = $tempModel->schema(true);
+
+		$knownToExist = false;
+		try {
+			$fields = $tempModel->schema(true);
+			$knownToExist = true;
+		} catch (Exception $e) {
+			$fields = array($tempModel->primaryKey);
+		}
 		if (!array_key_exists('id', $fields)) {
 			$primaryKey = $this->findPrimaryKey($fields);
 		}
 
-		$displayField = $tempModel->hasField(array('name', 'title'));
-		if (!$displayField) {
-			$displayField = $this->findDisplayField($tempModel->schema());
-		}
+		if ($knownToExist) {
+			$displayField = $tempModel->hasField(array('name', 'title'));
+			if (!$displayField) {
+				$displayField = $this->findDisplayField($tempModel->schema());
+			}
 
-		$prompt = __d('cake_console', "Would you like to supply validation criteria \nfor the fields in your model?");
-		$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
-		if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) == 'y') {
-			$validate = $this->doValidation($tempModel);
-		}
+			$prompt = __d('cake_console', "Would you like to supply validation criteria \nfor the fields in your model?");
+			$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
+			if (array_search($useTable, $this->_tables) !== false && strtolower($wannaDoValidation) == 'y') {
+				$validate = $this->doValidation($tempModel);
+			}
 
-		$prompt = __d('cake_console', "Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?");
-		$wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
-		if (strtolower($wannaDoAssoc) == 'y') {
-			$associations = $this->doAssociations($tempModel);
+			$prompt = __d('cake_console', "Would you like to define model associations\n(hasMany, hasOne, belongsTo, etc.)?");
+			$wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
+			if (strtolower($wannaDoAssoc) == 'y') {
+				$associations = $this->doAssociations($tempModel);
+			}
 		}
 
 		$this->out();

+ 27 - 4
lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php

@@ -30,7 +30,7 @@ App::uses('ModelTask', 'Console/Command/Task');
 /**
  * ModelTaskTest class
  *
- * @package       Cake.Test.Case.Console.Command.Task
+ * @package	   Cake.Test.Case.Console.Command.Task
  */
 class ModelTaskTest extends CakeTestCase {
 
@@ -965,12 +965,35 @@ STRINGEND;
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
 
-		$this->Task->expects($this->once())->method('_stop');
-		$this->Task->expects($this->once())->method('err');
+		$this->Task->expects($this->any())->method('in')
+			->will($this->onConsecutiveCalls(
+				'Foobar', // Or type in the name of the model
+				'y', // Do you want to use this table
+				'n' // Doesn't exist, continue anyway?
+			));
+
+		$this->Task->execute();
+	}
+
+/**
+ * test using bake interactively with a table that does not exist.
+ *
+ * @return void
+ */
+	public function testForcedExecuteWithNonExistantTableName() {
+		$this->Task->connection = 'test';
+		$this->Task->path = '/my/path/';
 
 		$this->Task->expects($this->any())->method('in')
-			->will($this->onConsecutiveCalls('Foobar', 'y'));
+			->will($this->onConsecutiveCalls(
+				'Foobar', // Or type in the name of the model
+				'y', // Do you want to use this table
+				'y', // Doesn't exist, continue anyway?
+				'id', // Primary key
+				'y' // Looks good?
+			));
 
 		$this->Task->execute();
 	}
+
 }