Skip to content

Commit 2dca77c

Browse files
committed
Updating Scaffold to merge hasAndBelongsToMany keys when generating field lists for scaffolded forms. Fixes #48
1 parent 31ec714 commit 2dca77c

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

cake/libs/controller/scaffold.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ function __scaffoldIndex($params) {
237237
* @access private
238238
*/
239239
function __scaffoldForm($action = 'edit') {
240+
$this->controller->viewVars['scaffoldFields'] = array_merge(
241+
$this->controller->viewVars['scaffoldFields'],
242+
array_keys($this->ScaffoldModel->hasAndBelongsToMany)
243+
);
240244
$this->controller->render($action, $this->layout);
241245
$this->_output();
242246
}

cake/tests/cases/libs/controller/scaffold.test.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ class ScaffoldMock extends CakeTestModel {
140140
'foreignKey' => 'article_id',
141141
)
142142
);
143+
/**
144+
* hasAndBelongsToMany property
145+
*
146+
* @var string
147+
**/
148+
var $hasAndBelongsToMany = array(
149+
'ScaffoldTag' => array(
150+
'className' => 'ScaffoldTag',
151+
'foreignKey' => 'post_id',
152+
'associationForeignKey' => 'tag_id',
153+
'joinTable' => 'posts_tags'
154+
)
155+
);
143156
}
144157
/**
145158
* ScaffoldUser class
@@ -195,6 +208,21 @@ class ScaffoldComment extends CakeTestModel {
195208
)
196209
);
197210
}
211+
/**
212+
* ScaffoldTag class
213+
*
214+
* @package cake
215+
* @subpackage cake.tests.cases.libs.controller
216+
*/
217+
class ScaffoldTag extends CakeTestModel {
218+
/**
219+
* useTable property
220+
*
221+
* @var string 'posts'
222+
* @access public
223+
*/
224+
var $useTable = 'tags';
225+
}
198226
/**
199227
* TestScaffoldView class
200228
*
@@ -226,7 +254,7 @@ class ScaffoldViewTest extends CakeTestCase {
226254
* @var array
227255
* @access public
228256
*/
229-
var $fixtures = array('core.article', 'core.user', 'core.comment');
257+
var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.posts_tag', 'core.tag');
230258
/**
231259
* startTest method
232260
*
@@ -559,7 +587,7 @@ class ScaffoldTest extends CakeTestCase {
559587
* @var array
560588
* @access public
561589
*/
562-
var $fixtures = array('core.article', 'core.user', 'core.comment');
590+
var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.posts_tag', 'core.tag');
563591
/**
564592
* startTest method
565593
*
@@ -648,6 +676,40 @@ function testScaffoldVariableSetting() {
648676
$this->assertEqual($result['pluralVar'], 'scaffoldMock');
649677
$this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'));
650678
}
679+
/**
680+
* test that habtm relationship keys get added to scaffoldFields.
681+
*
682+
* @see http://code.cakephp.org/tickets/view/48
683+
* @return void
684+
**/
685+
function testHabtmFieldAdditionWithScaffoldForm() {
686+
$this->Controller->action = 'edit';
687+
$this->Controller->here = '/scaffold_mock';
688+
$this->Controller->webroot = '/';
689+
$params = array(
690+
'plugin' => null,
691+
'pass' => array(1),
692+
'form' => array(),
693+
'named' => array(),
694+
'url' => array('url' =>'scaffold_mock'),
695+
'controller' => 'scaffold_mock',
696+
'action' => 'edit',
697+
);
698+
//set router.
699+
Router::reload();
700+
Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
701+
$this->Controller->params = $params;
702+
$this->Controller->controller = 'scaffold_mock';
703+
$this->Controller->base = '/';
704+
$this->Controller->constructClasses();
705+
ob_start();
706+
$Scaffold = new Scaffold($this->Controller, $params);
707+
$result = ob_get_clean();
708+
$this->assertPattern('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
709+
710+
$result = $Scaffold->controller->viewVars;
711+
$this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'));
712+
}
651713
/**
652714
* test that the proper names and variable values are set by Scaffold
653715
*

0 commit comments

Comments
 (0)