BANSYSTEMSBLOG

Load of the ○○○○PHP!?

JsHelper使ってますか!
僕は元気ですw

便利なものは使おうということで、Js->submitで保存できるテンプレートを作りました。
bakeするとadd.ctpとedit.ctpがJs->submitになるというものです。
Ajaxで保存します。

cake/console/templates/defaultをコピーして、vendors/shells/templatesへコピーします。

defaultをjssubmitへリネームします。

変更内容
1.Ajax処理にするため、保存時に全体を書き換えるめ、idでactionより上をすべて囲む
2.Js Helperを読み込む
3.Js->submitを設定する
4.大前提としてjQueryを用意する

//form.ctp
<div id="<?php echo $action ?>">
<div class="<?php echo $pluralVar;?> form">
<?php echo "<?php echo \$this->Html->script('jquery', array('inline'=>false));?>\n";?>
<?php echo "<?php echo \$this->Form->create('{$modelClass}');?>\n";?>
	<fieldset>
 		<legend><?php echo "<?php printf(__('" . Inflector::humanize($action) . " %s', true), __('{$singularHumanName}', true)); ?>";?></legend>

<table>
<?php
		echo "\t<?php\n";
		foreach ($fields as $field) {
			if (strpos($action, 'add') !== false && $field == $primaryKey) {
				continue;
			} elseif (!in_array($field, array('created', 'modified', 'updated'))) {
				echo "echo '<tr><td>';\n";
				echo "\t\techo \$this->Form->input('{$field}');\n";
				echo "echo '</td></tr>';\n";
			}
		}
		if (!empty($associations['hasAndBelongsToMany'])) {
			foreach ($associations['hasAndBelongsToMany'] as $assocName => $assocData) {
				echo "echo '<tr><td>'\n";
				echo "\t\techo \$this->Form->input('{$assocName}');\n";
				echo "echo '</td></tr>'\n";
			}
		}
		echo "\t?>\n";
?>
</table>
	</fieldset>
<?php
	echo "<?php echo \$this->Js->submit(__('Save', true), array('update'=>'#".$action."', 'url'=>'".$action."', 'buffer'=>false));?>\n";
	echo "<?php echo \$this->Form->end();?>\n";
?>
</div>

<div class="actions">
	<h3><?php echo "<?php __('Actions'); ?>"; ?></h3>
	<ul>

<?php if (strpos($action, 'add') === false): ?>
		<li><?php echo "<?php echo \$this->Html->link(__('Delete', true), array('action' => 'delete', \$this->Form->value('{$modelClass}.{$primaryKey}')), null, sprintf(__('Are you sure you want to delete # %s?', true), \$this->Form->value('{$modelClass}.{$primaryKey}'))); ?>";?></li>
<?php endif;?>
		<li><?php echo "<?php echo \$this->Html->link(sprintf(__('List %s', true), __('{$pluralHumanName}', true)), array('action' => 'index'));?>";?></li>
<?php
		$done = array();
		foreach ($associations as $type => $data) {
			foreach ($data as $alias => $details) {
				if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) {
					echo "\t\t<li><?php echo \$this->Html->link(sprintf(__('List %s', true), __('" . Inflector::humanize($details['controller']) . "', true)), array('controller' => '{$details['controller']}', 'action' => 'index')); ?> </li>\n";
					echo "\t\t<li><?php echo \$this->Html->link(sprintf(__('New %s', true), __('" . Inflector::humanize(Inflector::underscore($alias)) . "', true)), array('controller' => '{$details['controller']}', 'action' => 'add')); ?> </li>\n";
					$done[] = $details['controller'];
				}
			}
		}
?>
	</ul>
</div>
</div>

jQueryを読み込んでいますが、別に読み込んでいる場合は不要です。

設置したらbakeします。

./cake/console/cake bake

Viewを選択します。
Welcome to CakePHP v1.3.0 Console
—————————————————————
App : app
Path: /Applications/MAMP/htdocs/hoge/app
—————————————————————
Interactive Bake Shell
—————————————————————
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> v

※テーブルは任意のテーブルです

—————————————————————
Bake View
Path: /Applications/MAMP/htdocs/hoge/app/views/
—————————————————————
Possible Controllers based on your current database:
1. Users
Enter a number from the list above,
type in the name of another controller, or ‘q’ to exit
[q] >1

※途中で下記のメッセージがでたら、jssubmitを選択する

—————————————————————
You have more than one set of templates installed.
Please choose the template set you wish to use:
—————————————————————
1. jssubmit
2. default
Which bake theme would you like to use? (1/2)
[1] > 1

その後はそのままbakeするだけ!
できあがったformはコントローラーを変更する必要はなく使用できるはずです。

デモ:http://bansystems.org:8080/hoge/users/add

No Comments :(