Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 8e38f666b77ea7cea708359766eb33fc0bc3f309 1 parent ac8e1e8
@AD7six AD7six authored
View
39 lib/Cake/Console/Command/Task/ModelTask.php
@@ -188,7 +188,7 @@ protected function _interactive() {
$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 @@ protected function _interactive() {
}
$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();
View
31 lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php
@@ -30,7 +30,7 @@
/**
* ModelTaskTest class
*
- * @package Cake.Test.Case.Console.Command.Task
+ * @package Cake.Test.Case.Console.Command.Task
*/
class ModelTaskTest extends CakeTestCase {
@@ -965,12 +965,35 @@ public function testExecuteWithNonExistantTableName() {
$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();
}
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.