0

I have some group of inputs each with same name, as shown in the pictureenter image description here

I want to save each category with each number of rooms and standard rate. All category inputs have the same name (<input name="category[]">) same for No. of rooms and standard rate.

So far I've tried:

foreach($data['category'] as $cat){
 $this->Model->save($cat)
//Will save only categories without No. of rooms and standard rate.
}

Is there a way I can foreach through the 3 groups of inputs and save data in different rows accordingly? My table columns are named after inputs name.

4
  • Which version of CakePHP are you using? Commented Jun 19, 2015 at 6:31
  • @ drmonkeyninja version 2.5.7 Commented Jun 19, 2015 at 6:36
  • I think you need saveMany book.cakephp.org/2.0/en/models/… Commented Jun 19, 2015 at 10:30
  • @Michel thanks. See my answer below. Remember to always include the Cake version number in future. Commented Jun 19, 2015 at 11:08

2 Answers 2

1

This is simple to achieve using the FormHelper and saveMany. Firstly in your View you want to include the form elements using the FormHelper using something like:-

echo $this->Form->input('Model..category');

Where you need to replace Model with your model alias. The .. will cause it to output an input with a name like Model[][category]. If you want to control the numeric index (for example in a loop to associate it with other fields) you can change this to $this->Form->input('Model.1.category') which would produce an input with the name Model[1][category].

Then in your controller you can save this data using saveMany:-

$this->Model->saveMany($this->request->data);

Again replacing Model with your model's alias.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you able to send this type of format to your controller, then you can use saveMany() function in Caek php "

http://book.cakephp.org/2.0/en/models/saving-your-data.html

$data = array(
   array('field1' => 'valuea', 'field2' => 'valuea'),
   array('field1' => 'valueb', 'field2' => 'valueb'),
   array('field1' => 'valuec', 'field2' => 'valuec')
)

or in

 $data = array(
    array('Model' => array('field1' => 'valuea', 'field2' =>  'valuea')),
  array('Model' => array('field1' => 'valueb', 'field2' => 'valueb')),
  array('Model' => array('field1' => 'valuec', 'field2' => 'valuec'))
 )

format.

You can do like this $this->Model->saveMany($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.