How to store multiple value in CakePHP

Few month ago I was started working with CakePHP. It was my learning project. So at that time there was a scenario to store multiple images for a post. So I was trying to store those images using loop. I have tried using foreach and for loop. But every time only one image was storing. Few days ago the same scenario was facing by my colleague. He was searching about this but I saw he did not found proper solution. So I am trying to write this here.

The scenario is, there will be one house and one house can have multiple rooms. My main intention is not about saving house and database here. My main intention is to show how to store multiple value at once. So some other functionality I may show slightly. Lets start from now…

Let me assume you have already setted up cakePHP project on your system.So I am not going to tell about those. I am starting with database structure. Here very simple database structure I will use.

In our project there will be 2 tables. one is houses and another is house_rooms.

Structure for housestable is : id, name

Structure for houses_roomstable is : id, name, house_id

Now I will be moving to the coding part. So I am starting with a form. Where there will be house name and room selection in the form. Create a .ctp file in this directory project/house/add.ctp. Now put bellow code into add.ctp

<?php 
echo $this->Form->create($venue);
echo $this->Form->input(‘name’);
echo $this->form->input(‘rooms’,[
‘type’ => ‘select’,
‘options’ => [
‘room 1’ => ‘Room 1’,
‘room 2’ => ‘Room 2’,
‘room 3’ => ‘Room 3’,
‘room 4’ => ‘Room 4’,
],
‘class’ => ‘form-control’,
‘label’=> [
‘text’ => ‘Select Rooms : ‘,
‘class’ => ‘control-label’
],
‘multiple’ => true
]
);
echo $this->Form->button(__(‘Add Home’));
echo $this->Form->end();
?>

Now create function to store the house and its rooms. Create a function named addHome()inside HomesController.php.

Here is the code of this function.

public function addHome()
{
$home = $this->Homes->newEntity();
if ($this->request->is(‘post’)) {
$home = $this->Homes->patchEntity($home, $this->request->getData());
if ($this->Homes->save($home)) {
$rooms = $this->request->getData(‘rooms’);
$houseRoom = array();
if ( !empty( $rooms ) ){
foreach ($rooms as $singleRoom) {
$data = [
‘name’ => $singleRoom,
‘houuse_id’ => $home->id
];
array_push($houseRoom, $data);
}
}
$houseRoomTable = $this->Houses->HouseRooms;
$entities = $houseRoomTable->newEntities($houseRoom);

$houseRoomTable->saveMany($entities);

}
$this->Flash->success(__(‘The room has been saved.’));
}
$this->set(compact(‘home’));
}

Now let me describe about above function where storing multiple data was done. First I have taken all selected rooms by this line$this->request->getData('rooms') . Now declared an empty array name as $houseRoom. In this array, I will keep all of the rooms and will save those rooms at once. Then created a foreach loop through all of the selected rooms. And kept house_idand room name in a temporary array named as $data. Now after each run of this loop this temporary array data is pushing to the previously declared array in this linearray_push($houseRoom, $data).

Now after loop is complete all of the rooms for newly created home will be available in$houseRoomarray. Now I have created entity forHouseRooms. And finally used saveMany()function to store all of the room for that house at once. That’s all. I hope it will help.

Database after storing the data

Now let me show you how I was trying to store those kind of data while I was started learning CakePHP and faced issue.

public function addHome()
{
$home = $this->Homes->newEntity();
if ($this->request->is('post')) {
$home = $this->Homes->patchEntity($home, $this->request->getData());
if ($this->Homes->save($home)) {
$rooms = $this->request->getData('rooms');
$houseRoomTable = $this->Houses->HouseRooms->newEntity();
foreach ($rooms as $singleRoom) {
{
$houseRoomTable->name = $singleRoom;
$houseRoomTable->code = $home->id;
$this->Codes->save($houseRoomTable);
}
}
}
$this->set(compact('home'));
}

In above function I have created new entity for HouseRoomsat once and it is before the loop. So it was storing only one value. One thing, I could put this new entity inside loop. But this is not good practice to create new entity again and again. So using saveMany()function is the best option to store many data at once.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade