LoginSignup
2
0

More than 3 years have passed since last update.

CakePHP 複数レコードを一括保存するsaveAllの使い方

Posted at

ビュー

form.ctp
echo $this->Form->create ( 'User', array (
    'type' => 'post',//POSTとGETの設定
    'controller' => 'users',//コントローラーの設定
    'action' => 'edit',//アクションの設定
) );

echo $this->Form->input ( 'User.0.name.');
echo $this->Form->input ( 'User.0.age');

echo $this->Form->input ( 'User.1.name');
echo $this->Form->input ( 'User.1.age');


echo $this->Form->input ( 'User.2.name');
echo $this->Form->input ( 'User.2.age');

echo $this->Form->end('送信');

$this->request->data;の中身

$data = array(
    'User' => array(
        0 => array(
            'name' => '太郎',
            'age' => 10
        ),
        1 => array(
            'name' => '花子',
            'age' => 8
        ),
        2 => array(
            'name' => '二郎',
            'age' => 5
        ),
    )
);

$data["User"]というように$dataの後ろにモデル名を指定するところがポイントです。

$this->User->saveAll($data["User"]);
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0