guagay_wk
guagay_wk

Reputation: 28030

How to make cakephp add multiple records to a single table with a single HTTP POST?

I would like to add multiple records to a single table in a single HTTP post. For single record, the HTTP post will look something like "http://127.0.0.1/app/model/api_add/data[Model][Field1]". How will the HTTP Post URL look like for adding multiple records? I am using cakephp 2.4.5

Below is the add() in the Controller:

public function add() 
{             
    if ($this->request->is('post')) 
    {
        $this->Model->create();
        $this->Model->saveAll($this->request->data);
    }
}

Upvotes: 0

Views: 400

Answers (1)

Nik Chankov
Nik Chankov

Reputation: 6047

From the cakebook:

echo $this->Form->input('Account.0.name', array('label' => 'Account name'));
echo $this->Form->input('Account.0.username');
echo $this->Form->input('Account.0.email');

So, you need to have iterator (in the example is 0, but you can loop like:

for($i=0;$i<5;$i++){
    echo $this->Form->input('Account.'.$i.'.name', array('label' => 'Account name'));
    echo $this->Form->input('Account.'.$i.'.username');
    echo $this->Form->input('Account.'.$i.'.email');
}

The article

Upvotes: 2

Related Questions