Skip to content

Commit

Permalink
Implement some more of the internals for updating bake.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 18, 2014
1 parent 11873fb commit e1c702d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
66 changes: 25 additions & 41 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -94,27 +94,26 @@ public function execute() {
}

if (empty($this->args)) {
$tables = $this->listAll();
$this->out(__d('cake_console', 'Choose a model to bake from the following:'));
foreach ($tables as $table) {
foreach ($this->listAll() as $table) {
$this->out('- ' . $table);
}
return true;
}

if (!empty($this->args[0])) {
if (strtolower($this->args[0]) === 'all') {
return $this->all();
}
$model = $this->_modelName($this->args[0]);
$this->listAll($this->connection);
$useTable = $this->getTable($model);
$object = $this->_getModelObject($model, $useTable);
if ($this->bake($object, false)) {
if ($this->_checkUnitTest()) {
$this->bakeFixture($model, $useTable);
$this->bakeTest($model);
}
if (strtolower($this->args[0]) === 'all') {
return $this->all();
}

$model = $this->args[0];
$table = $this->getTable();

$object = $this->getTableObject($model, $table);

if ($this->bake($object, false)) {
if ($this->_checkUnitTest()) {
$this->bakeFixture($model, $useTable);
$this->bakeTest($model);
}
}
}
Expand Down Expand Up @@ -146,27 +145,14 @@ public function all() {
*
* @param string $className Name of class you want model to be.
* @param string $table Table name
* @return Model Model instance
* @throws \Exception Will throw this until baking models works
* @return Cake\ORM\Table Table instance
*/
protected function _getModelObject($className, $table = null) {
if (!$table) {
$table = Inflector::tableize($className);
}
throw new \Exception('Baking models does not work currently.');

public function getTableObject($className, $table) {
$object = TableRegistry::get($className, [
'name' => $className,
'table' => $table,
'ds' => $this->connection
'connection' => $this->connection
]);
$fields = $object->schema();
foreach ($fields as $name => $field) {
if (isset($field['key']) && $field['key'] === 'primary') {
$object->primaryKey = $name;
break;
}
}
return $object;
}

Expand Down Expand Up @@ -937,19 +923,17 @@ protected function _getAllTables() {
}

/**
* Interact with the user to determine the table name of a particular model
* Get the table name for the model being baked.
*
* Uses the `table` option if it is set.
*
* @param string $modelName Name of the model you want a table for.
* @param string $useDbConfig Name of the database config you want to get tables from.
* @return string Table name
* @return string.
*/
public function getTable($modelName, $useDbConfig = null) {
$useTable = Inflector::tableize($modelName);
if (in_array($modelName, $this->_modelNames)) {
$modelNames = array_flip($this->_modelNames);
$useTable = $this->_tables[$modelNames[$modelName]];
public function getTable() {
if (isset($this->params['table'])) {
return $this->params['table'];
}
return $useTable;
return Inflector::tableize($this->args[0]);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -115,6 +115,38 @@ public function testListAllConnection() {
$this->assertContains('category_threads', $result);
}

/**
* Test getName() method.
*
* @return void
*/
public function testGetTable() {
$this->Task->args[0] = 'BakeArticle';
$result = $this->Task->getTable();
$this->assertEquals('bake_articles', $result);

$this->Task->args[0] = 'BakeArticles';
$result = $this->Task->getTable();
$this->assertEquals('bake_articles', $result);

$this->Task->args[0] = 'Article';
$this->Task->params['table'] = 'bake_articles';
$result = $this->Task->getTable();
$this->assertEquals('bake_articles', $result);
}

/**
* Test getting the a table class.
*
* @return void
*/
public function testGetTableObject() {
$result = $this->Task->getTableObject('Article', 'bake_articles');
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('bake_articles', $result->table());
$this->assertEquals('Article', $result->alias());
}

/**
* test that initializing the validations works.
*
Expand Down

0 comments on commit e1c702d

Please sign in to comment.