Permalink
Browse files

allow creating models without the table existing

particularly useful/appropriate for schemaless datasources
  • Loading branch information...
1 parent 90b007e commit ac8e1e8304e858175dfc89ea693c64c0b6d54baa @AD7six AD7six committed Nov 19, 2011
Showing with 16 additions and 14 deletions.
  1. +15 −13 lib/Cake/Console/Command/Task/ModelTask.php
  2. +1 −1 lib/Cake/Model/CakeSchema.php
@@ -24,7 +24,7 @@
/**
* Task class for creating and updating model files.
*
- * @package Cake.Console.Command.Task
+ * @package Cake.Console.Command.Task
*/
class ModelTask extends BakeTask {
@@ -187,18 +187,20 @@ protected function _interactive() {
$useTable = $this->getTable($currentModelName);
$db = ConnectionManager::getDataSource($this->connection);
$fullTableName = $db->fullTableName($useTable);
-
- if (in_array($useTable, $this->_tables)) {
- $tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
- $fields = $tempModel->schema(true);
- if (!array_key_exists('id', $fields)) {
- $primaryKey = $this->findPrimaryKey($fields);
+ if (!in_array($useTable, $this->_tables)) {
+ $prompt = __d('cake_console', "The table $useTable doesn't exist or could not be automatically detected\ncontinue anyway?");
+ $continue = $this->in($prompt, array('y', 'n'));
+ if (strtolower($continue) == 'n') {
+ return false;
}
- } else {
- $this->err(__d('cake_console', 'Table %s does not exist, cannot bake a model without a table.', $useTable));
- $this->_stop();
- return false;
}
+
+ $tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
+ $fields = $tempModel->schema(true);
+ if (!array_key_exists('id', $fields)) {
+ $primaryKey = $this->findPrimaryKey($fields);
+ }
+
$displayField = $tempModel->hasField(array('name', 'title'));
if (!$displayField) {
$displayField = $this->findDisplayField($tempModel->schema());
@@ -467,7 +469,7 @@ public function doAssociations($model) {
}
if (empty($this->_tables)) {
- $this->_tables = $this->getAllTables();
+ $this->_tables = (array) $this->getAllTables();
}
$associations = array(
@@ -779,7 +781,7 @@ public function bakeTest($className) {
* @return array
*/
public function listAll($useDbConfig = null) {
- $this->_tables = $this->getAllTables($useDbConfig);
+ $this->_tables = (array) $this->getAllTables($useDbConfig);
if ($this->interactive === true) {
$this->out(__d('cake_console', 'Possible Models based on your current database:'));
@@ -206,7 +206,7 @@ public function read($options = array()) {
}
$tables = array();
- $currentTables = $db->listSources();
+ $currentTables = (array) $db->listSources();
$prefix = null;
if (isset($db->config['prefix'])) {

0 comments on commit ac8e1e8

Please sign in to comment.