Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding the ability to set $displayField from bake. Refactoring ModelT…

…ask::bake() Fixes #4438.
  • Loading branch information...
commit 41eecdaa91ee006d9f8086f14097f63d10533091 1 parent d671056
@markstory markstory authored
View
37 cake/console/libs/tasks/model.php
@@ -245,7 +245,9 @@ function __interactive() {
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
if (strtolower($looksGood) == 'y') {
- if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $this->connection)) {
+ $vars = compact('associations', 'validate', 'primaryKey', 'useTable', 'displayField');
+ $vars['useDbConfig'] = $this->connection;
+ if ($this->bake($currentModelName, $vars)) {
if ($this->_checkUnitTest()) {
$this->bakeFixture($currentModelName, $useTable);
$this->bakeTest($currentModelName, $useTable, $associations);
@@ -702,27 +704,28 @@ function _generatePossibleKeys() {
* Assembles and writes a Model file.
*
* @param mixed $name Model name or object
- * @param mixed $associations if array and $name is not an object assume Model associations array otherwise boolean interactive
- * @param array $validate Validation rules
- * @param string $primaryKey Primary key to use
- * @param string $useTable Table to use
- * @param string $useDbConfig Database configuration setting to use
+ * @param mixed $data if array and $name is not an object assume bake data, otherwise boolean.
* @access private
*/
- function bake($name, $associations = array(), $validate = array(), $primaryKey = 'id', $useTable = null, $useDbConfig = 'default') {
-
+ function bake($name, $data = array()) {
if (is_object($name)) {
- if (!is_array($associations)) {
- $associations = $this->doAssociations($name, $associations);
- $validate = $this->doValidation($name);
- }
- $primaryKey = $name->primaryKey;
- $useTable = $name->table;
- $useDbConfig = $name->useDbConfig;
- $name = $name->name;
+ if ($data == false) {
+ $data = $associations = array();
+ $data['associations'] = $this->doAssociations($name, $associations);
+ $data['validate'] = $this->doValidation($name);
+ }
+ $data['primaryKey'] = $name->primaryKey;
+ $data['useTable'] = $name->table;
+ $data['useDbConfig'] = $name->useDbConfig;
+ $data['name'] = $name = $name->name;
+ } else {
+ $data['name'] = $name;
}
+ $defaults = array('associations' => array(), 'validate' => array(), 'primaryKey' => 'id',
+ 'useTable' => null, 'useDbConfig' => 'default', 'displayField' => null);
+ $data = array_merge($defaults, $data);
- $this->Template->set(compact('name', 'useDbConfig', 'associations', 'validate', 'primaryKey', 'useTable'));
+ $this->Template->set($data);
$this->Template->set('plugin', Inflector::camelize($this->plugin));
$out = $this->Template->generate('classes', 'model');
View
3  cake/console/libs/templates/default/classes/model.ctp
@@ -33,6 +33,9 @@ endif;
if ($primaryKey !== 'id'): ?>
var $primaryKey = '<?php echo $primaryKey; ?>';
<?php endif;
+if ($displayField): ?>
+ var $displayField = '<?php echo $displayField; ?>';
+<?php endif;
if (!empty($validate)):
echo "\tvar \$validate = array(\n";
View
7 cake/tests/cases/console/libs/tasks/model.test.php
@@ -564,7 +564,7 @@ function testBakeValidation() {
'time' => 'time'
)
);
- $result = $this->Task->bake('Article', array(), $validate);
+ $result = $this->Task->bake('Article', compact('validate'));
$this->assertPattern('/class Article extends AppModel \{/', $result);
$this->assertPattern('/\$name \= \'Article\'/', $result);
$this->assertPattern('/\$validate \= array\(/', $result);
@@ -615,7 +615,7 @@ function testBakeRelations() {
),
)
);
- $result = $this->Task->bake('Article', $associations, array());
+ $result = $this->Task->bake('Article', compact('associations'));
$this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result);
$this->assertPattern('/\$hasMany \= array\(/', $result);
$this->assertPattern('/\$belongsTo \= array\(/', $result);
@@ -642,7 +642,7 @@ function testBakeWithPlugin() {
$path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php';
$this->Task->expectAt(1, 'createFile', array(
- $path, new PatternExpectation('/Article extends ControllerTestAppModel/')));
+ $path, new PatternExpectation('/Article extends ControllerTestAppModel/')));
$this->Task->bake('Article', array(), array());
}
@@ -700,7 +700,6 @@ function testExecuteIntoInteractive() {
$this->Task->path = '/my/path/';
$this->Task->setReturnValueAt(0, 'in', '1'); //choose article
- // $this->Task->setReturnValueAt(2, 'in', 'n'); //no validation
$this->Task->setReturnValueAt(1, 'in', 'n'); //no validation
$this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations
$this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation
Please sign in to comment.
Something went wrong with that request. Please try again.