diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index 39307355be8..c9f2920bedf 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -62,7 +62,7 @@ class ModelTask extends Shell { * @var array * @access public */ - var $tasks = array('DbConfig', 'Fixture', 'Test'); + var $tasks = array('DbConfig', 'Fixture', 'Test', 'Template'); /** * Holds tables found on connection. @@ -700,7 +700,12 @@ function bake($name, $associations = array(), $validate = array(), $primaryKey $useDbConfig = $name->useDbConfig; $name = $name->name; } - + + $this->Template->set(compact('name', 'useDbConfig', 'associations', 'validate', 'primaryKey', 'useTable')); + $this->Template->set('plugin', $this->plugin); + $out = $this->Template->generate('objects', 'model'); + + /* $out = "plugin}AppModel {\n\n"; $out .= "\tvar \$name = '{$name}';\n"; @@ -841,9 +846,11 @@ function bake($name, $associations = array(), $validate = array(), $primaryKey } $out .= "}\n"; $out .= "?>"; + */ $filename = $this->path . Inflector::underscore($name) . '.php'; $this->out("\nBaking model class for $name..."); - return $this->createFile($filename, $out); + $this->createFile($filename, $out); + return $out; } /** diff --git a/cake/console/libs/templates/objects/model.ctp b/cake/console/libs/templates/objects/model.ctp new file mode 100644 index 00000000000..5cb28dc9cfb --- /dev/null +++ b/cake/console/libs/templates/objects/model.ctp @@ -0,0 +1,125 @@ + +class extends AppModel { + var $name = ''; + + var $useDbConfig = ''; + + + + var $primaryKey = ''; + + $validations): + echo "\t\t'$field' => array(\n"; + foreach ($validations as $key => $validator): + echo "\t\t\t'$key' => array('rule' => array('$validator')),\n"; + endforeach; + echo "\t\t),\n"; + endforeach; + echo "\t);\n"; +endif; + +foreach (array('hasOne', 'belongsTo') as $assocType): + if (!empty($associations[$assocType])): + $typeCount = count($associations[$assocType]); + echo "\n\tvar \$$assocType = array("; + foreach ($associations[$assocType] as $i => $relation): + $out = "\n\t\t'{$relation['alias']}' => array(\n"; + $out .= "\t\t\t'className' => '{$relation['className']}',\n"; + $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; + $out .= "\t\t\t'conditions' => '',\n"; + $out .= "\t\t\t'fields' => '',\n"; + $out .= "\t\t\t'order' => ''\n"; + $out .= "\t\t)"; + if ($i + 1 < $typeCount) { + $out .= ","; + } + echo $out; + endforeach; + echo "\n\t);\n"; + endif; +endforeach; + +if (!empty($associations['hasMany'])): + $belongsToCount = count($associations['hasMany']); + echo "\n\tvar \$hasMany = array("; + foreach ($associations['hasMany'] as $i => $relation): + $out = "\n\t\t'{$relation['alias']}' => array(\n"; + $out .= "\t\t\t'className' => '{$relation['className']}',\n"; + $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; + $out .= "\t\t\t'dependent' => false,\n"; + $out .= "\t\t\t'conditions' => '',\n"; + $out .= "\t\t\t'fields' => '',\n"; + $out .= "\t\t\t'order' => '',\n"; + $out .= "\t\t\t'limit' => '',\n"; + $out .= "\t\t\t'offset' => '',\n"; + $out .= "\t\t\t'exclusive' => '',\n"; + $out .= "\t\t\t'finderQuery' => '',\n"; + $out .= "\t\t\t'counterQuery' => ''\n"; + $out .= "\t\t)"; + if ($i + 1 < $belongsToCount) { + $out .= ","; + } + echo $out; + endforeach; + echo "\n\t);\n\n"; +endif; + +if (!empty($associations['hasAndBelongsToMany'])): + $habtmCount = count($associations['hasAndBelongsToMany']); + echo "\n\tvar \$hasAndBelongsToMany = array("; + foreach ($associations['hasAndBelongsToMany'] as $i => $relation): + $out = "\n\t\t'{$relation['alias']}' => array(\n"; + $out .= "\t\t\t'className' => '{$relation['className']}',\n"; + $out .= "\t\t\t'joinTable' => '{$relation['joinTable']}',\n"; + $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; + $out .= "\t\t\t'associationForeignKey' => '{$relation['associationForeignKey']}',\n"; + $out .= "\t\t\t'unique' => true,\n"; + $out .= "\t\t\t'conditions' => '',\n"; + $out .= "\t\t\t'fields' => '',\n"; + $out .= "\t\t\t'order' => '',\n"; + $out .= "\t\t\t'limit' => '',\n"; + $out .= "\t\t\t'offset' => '',\n"; + $out .= "\t\t\t'finderQuery' => '',\n"; + $out .= "\t\t\t'deleteQuery' => '',\n"; + $out .= "\t\t\t'insertQuery' => ''\n"; + $out .= "\t\t)"; + if ($i + 1 < $habtmCount) { + $out .= ","; + } + echo $out; + endforeach; + echo "\n\t);\n\n"; +endif; +?> +'; ?> diff --git a/cake/tests/cases/console/libs/tasks/model.test.php b/cake/tests/cases/console/libs/tasks/model.test.php index 9c49bde375e..d66a6c9d34a 100644 --- a/cake/tests/cases/console/libs/tasks/model.test.php +++ b/cake/tests/cases/console/libs/tasks/model.test.php @@ -37,10 +37,10 @@ ob_end_clean(); } -if (!class_exists('ModelTask')) { - require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php'; - require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php'; -} +require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php'; +require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php'; +require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php'; + Mock::generatePartial( 'ShellDispatcher', 'TestModelTaskMockShellDispatcher', @@ -82,7 +82,9 @@ class ModelTaskTest extends CakeTestCase { function startTest() { $this->Dispatcher =& new TestModelTaskMockShellDispatcher(); $this->Task =& new MockModelTask($this->Dispatcher); - $this->Task->Dispatch = new $this->Dispatcher; + $this->Task->Dispatch =& new $this->Dispatcher; + $this->Task->Dispatch->shellPaths = Configure::read('shellPaths'); + $this->Task->Template =& new TemplateTask($this->Task->Dispatch); } /** @@ -517,5 +519,86 @@ function testInOptions() { $result = $this->Task->inOptions($options, 'Pick a number'); $this->assertEqual($result, 1); } + +/** + * test baking validation + * + * @return void + **/ + function testBakeValidation() { + $validate = array( + 'name' => array( + 'notempty' => 'notempty' + ), + 'email' => array( + 'email' => 'email', + ), + 'some_date' => array( + 'date' => 'date' + ), + 'some_time' => array( + 'time' => 'time' + ) + ); + $result = $this->Task->bake('Article', array(), $validate); + $this->assertPattern('/class Article extends AppModel \{/', $result); + $this->assertPattern('/\$name \= \'Article\'/', $result); + $this->assertPattern('/\$validate \= array\(/', $result); + $pattern = '/' . preg_quote("'notempty' => array('rule' => array('notempty')),", '/') . '/'; + $this->assertPattern($pattern, $result); + } +/** + * test baking relations + * + * @return void + **/ + function testBakeRelations() { + $associations = array( + 'belongsTo' => array( + array( + 'alias' => 'SomethingElse', + 'className' => 'SomethingElse', + 'foreignKey' => 'something_else_id', + ), + array( + 'alias' => 'User', + 'className' => 'User', + 'foreignKey' => 'user_id', + ), + ), + 'hasOne' => array( + array( + 'alias' => 'OtherModel', + 'className' => 'OtherModel', + 'foreignKey' => 'other_model_id', + ), + ), + 'hasMany' => array( + array( + 'alias' => 'Comment', + 'className' => 'Comment', + 'foreignKey' => 'parent_id', + ), + ), + 'hasAndBelongsToMany' => array( + array( + 'alias' => 'Tag', + 'className' => 'Tag', + 'foreignKey' => 'article_id', + 'joinTable' => 'articles_tags', + 'associationForeignKey' => 'tag_id', + ), + ) + ); + $result = $this->Task->bake('Article', $associations, array()); + $this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result); + $this->assertPattern('/\$hasMany \= array\(/', $result); + $this->assertPattern('/\$belongsTo \= array\(/', $result); + $this->assertPattern('/\$hasOne \= array\(/', $result); + $this->assertPattern('/Tag/', $result); + $this->assertPattern('/OtherModel/', $result); + $this->assertPattern('/SomethingElse/', $result); + $this->assertPattern('/Comment/', $result); + } } ?> \ No newline at end of file