[CakePHP 2.x]
複数レコードの同時保存
jQueryをちょっとだけ使います
Github:
https://github.com/mizoe/cakephp2_save_multi_records
想定するテーブル
• Customers
• id, created, modified, name(varchar)
• Addresses
• id, created, modified, address(varchar), customer_id
- Customer has many Addresses
- Address belongs to Customer
Bake直後
レコード保存の流れ: Customer新規作成 → Address追加
これを Customer追加の画面において、複数のAddressを追加できるようにしていく
CustomersController.php にメソッドadd_all追加
• add()からコピペして編集
if ($this->Customer->addAll($this->request->data)) {
$this->Session->setFlash(__('The customer has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
}
ここだけちょっと変更。モデルのaddAll()を呼び出します。
引数はPOSTでもらったものをそのまま。
Model: Customer.php
public function addAll($data = null) {
if($data == null){ // レコードがなければ return
return false;
}
$result = $this->saveAll($data); // Cusotmer に保存
return true;
}
モデル間のアソシエーションが定義済みなので、saveAll()を実行すると
実はsaveAssociated() が呼び出される。
saveAssosicated は適切なデータを与えるだけで、いろいろやってくれる。
今回の場合、Customer を追加してその ID を獲得し、Address の customer_id に
割り当てる。もしすべてのレコードが保存できなければ、トランザクションをたどって
保存する前の状態に戻す(DBがトランザクションをサポートしていれば)。
詳細: http://book.cakephp.org/2.0/ja/models/saving-your-data.html
View : add_all.ctp
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var $i = 1;
$('#add_address').click(function(e) {
var inputAddress = '<div class="input text required"><label for="Address' + $i + 'Address">Address</label>' +
'<input name="data[Address][' + $i + '][address]" maxlength="255" type="text" id="Address' + $i + 'Address" required="required"/></div>'
$('#add_address').before(inputAddress);
$i++;
});
});
</script>
<div class="customers form">
<?php echo $this->Form->create('Customer'); ?>
<fieldset>
<legend><?php echo __('Add Customer with Address'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('Address.0.address');
?>
<div class="submit" id="add_address"><input type="button" value="住所追加"/></div>
</fieldset>
<?php echo $this->Form->end(__('実行')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('顧客一覧'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('住所一覧'), array('controller' => 'addresses', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('新しい住所'), array('controller' => 'addresses', 'action' => 'add')); ?> </li>
</ul>
</div>
ボタンを押すと input フィールド追加
Bakeされたadd.ctpとほぼ同じ
できたー
https://github.com/mizoe/cakephp2_save_multi_records

CakePHP 2.x 複数レコード同時保存

  • 1.
  • 2.
    想定するテーブル • Customers • id,created, modified, name(varchar) • Addresses • id, created, modified, address(varchar), customer_id - Customer has many Addresses - Address belongs to Customer
  • 3.
    Bake直後 レコード保存の流れ: Customer新規作成 →Address追加 これを Customer追加の画面において、複数のAddressを追加できるようにしていく
  • 4.
    CustomersController.php にメソッドadd_all追加 • add()からコピペして編集 if($this->Customer->addAll($this->request->data)) { $this->Session->setFlash(__('The customer has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The customer could not be saved. Please, try again.')); } ここだけちょっと変更。モデルのaddAll()を呼び出します。 引数はPOSTでもらったものをそのまま。
  • 5.
    Model: Customer.php public functionaddAll($data = null) { if($data == null){ // レコードがなければ return return false; } $result = $this->saveAll($data); // Cusotmer に保存 return true; } モデル間のアソシエーションが定義済みなので、saveAll()を実行すると 実はsaveAssociated() が呼び出される。 saveAssosicated は適切なデータを与えるだけで、いろいろやってくれる。 今回の場合、Customer を追加してその ID を獲得し、Address の customer_id に 割り当てる。もしすべてのレコードが保存できなければ、トランザクションをたどって 保存する前の状態に戻す(DBがトランザクションをサポートしていれば)。 詳細: http://book.cakephp.org/2.0/ja/models/saving-your-data.html
  • 6.
    View : add_all.ctp <scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ var $i = 1; $('#add_address').click(function(e) { var inputAddress = '<div class="input text required"><label for="Address' + $i + 'Address">Address</label>' + '<input name="data[Address][' + $i + '][address]" maxlength="255" type="text" id="Address' + $i + 'Address" required="required"/></div>' $('#add_address').before(inputAddress); $i++; }); }); </script> <div class="customers form"> <?php echo $this->Form->create('Customer'); ?> <fieldset> <legend><?php echo __('Add Customer with Address'); ?></legend> <?php echo $this->Form->input('name'); echo $this->Form->input('Address.0.address'); ?> <div class="submit" id="add_address"><input type="button" value="住所追加"/></div> </fieldset> <?php echo $this->Form->end(__('実行')); ?> </div> <div class="actions"> <h3><?php echo __('Actions'); ?></h3> <ul> <li><?php echo $this->Html->link(__('顧客一覧'), array('action' => 'index')); ?></li> <li><?php echo $this->Html->link(__('住所一覧'), array('controller' => 'addresses', 'action' => 'index')); ?> </li> <li><?php echo $this->Html->link(__('新しい住所'), array('controller' => 'addresses', 'action' => 'add')); ?> </li> </ul> </div> ボタンを押すと input フィールド追加 Bakeされたadd.ctpとほぼ同じ
  • 7.