mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-12 20:49:50 +00:00
Adding the ability to set $displayField from bake. Refactoring ModelTask::bake() Fixes #4438.
This commit is contained in:
parent
d671056044
commit
41eecdaa91
3 changed files with 25 additions and 20 deletions
|
@ -245,7 +245,9 @@ class ModelTask extends Shell {
|
|||
$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 @@ class ModelTask extends Shell {
|
|||
* 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);
|
||||
if ($data == false) {
|
||||
$data = $associations = array();
|
||||
$data['associations'] = $this->doAssociations($name, $associations);
|
||||
$data['validate'] = $this->doValidation($name);
|
||||
}
|
||||
$primaryKey = $name->primaryKey;
|
||||
$useTable = $name->table;
|
||||
$useDbConfig = $name->useDbConfig;
|
||||
$name = $name->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');
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -564,7 +564,7 @@ class ModelTaskTest extends CakeTestCase {
|
|||
'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 @@ class ModelTaskTest extends CakeTestCase {
|
|||
),
|
||||
)
|
||||
);
|
||||
$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 @@ class ModelTaskTest extends CakeTestCase {
|
|||
|
||||
$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 @@ class ModelTaskTest extends CakeTestCase {
|
|||
$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
|
||||
|
|
Loading…
Add table
Reference in a new issue