From e6881cced494d391e2d5f0e3d8f9054944bb91f1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 14 May 2009 23:00:29 -0400 Subject: [PATCH] Extracted another method. --- cake/console/libs/tasks/model.php | 62 ++++++++++++++++++------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index 2112c057b..28e62d050 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -689,7 +689,7 @@ class ModelTask extends Shell { * @param string $useDbConfig Database configuration setting to use * @access private */ - function bake($name, $associations = array(), $validate = array(), $primaryKey = 'id', $useTable = null, $useDbConfig = 'default') { + function bake($name, $associations = array(), $validate = array(), $primaryKey = 'id', $useTable = null, $useDbConfig = 'default') { if (is_object($name)) { if (!is_array($associations)) { @@ -731,35 +731,15 @@ class ModelTask extends Shell { * @param string $useDbConfig Database configuration name * @access public */ - function listAll($useDbConfig = null, $interactive = true) { - if (!isset($useDbConfig)) { - $useDbConfig = $this->connection; - } - $db =& ConnectionManager::getDataSource($useDbConfig); - $usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix']; - if ($usePrefix) { - $tables = array(); - foreach ($db->listSources() as $table) { - if (!strncmp($table, $usePrefix, strlen($usePrefix))) { - $tables[] = substr($table, strlen($usePrefix)); - } - } - } else { - $tables = $db->listSources(); - } - if (empty($tables)) { - $this->err(__('Your database does not have any tables.', true)); - $this->_stop(); - } + function listAll($useDbConfig = null) { + $this->__tables = $this->getAllTables($useDbConfig); - $this->__tables = $tables; - - if ($interactive === true) { + if ($this->interactive === true) { $this->out(__('Possible Models based on your current database:', true)); $this->_modelNames = array(); - $count = count($tables); + $count = count($this->__tables); for ($i = 0; $i < $count; $i++) { - $this->_modelNames[] = $this->_modelName($tables[$i]); + $this->_modelNames[] = $this->_modelName($this->__tables[$i]); $this->out($i + 1 . ". " . $this->_modelNames[$i]); } } @@ -793,6 +773,36 @@ class ModelTask extends Shell { return $useTable; } +/** + * Get an Array of all the tables in the supplied connection + * will halt the script if no tables are found. + * + * @param string $useDbConfig Connection name to scan. + * @return array Array of tables in the database. + **/ + function getAllTables($useDbConfig = null) { + if (!isset($useDbConfig)) { + $useDbConfig = $this->connection; + } + $tables = array(); + $db =& ConnectionManager::getDataSource($useDbConfig); + $usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix']; + if ($usePrefix) { + foreach ($db->listSources() as $table) { + if (!strncmp($table, $usePrefix, strlen($usePrefix))) { + $tables[] = substr($table, strlen($usePrefix)); + } + } + } else { + $tables = $db->listSources(); + } + if (empty($tables)) { + $this->err(__('Your database does not have any tables.', true)); + $this->_stop(); + } + return $tables; + } + /** * Forces the user to specify the model he wants to bake, and returns the selected model name. *