hydra12’s blog

Tech Stuff, TCEA, PHP, etc.

Writing Multiple Rows at One Time in CakePHP

Filed under: Cake — admin at 10:16 am on Friday, November 16, 2007

I’m working on an application where I can keep track of who was at a meeting. I have a users table and a meetings table. Meeting belongsto User, and User hasMany Meeting. I wanted to have a form that shows the meeting date and a list of users with checkboxes next to each user name. When I click submit, I want to save the data in my meetings table.

Since User hasMany Meeting, I did this in my Users controller. I made a new function (called index2 here) that looks like this (explanation is in the comments):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function index2()
{
	//turn off recursive search.  We just need names.
	$this->User->recursive = 0;
	//get all the user names and ids
	$users = $this->User->findAll(null,array('id','name'));
	$this->set('users', $users);
	if(!empty($this->data))
	{
		//set some variables for data parsing
		$date = null;
		$day = null;
		$month = null;
		$year = null;
 
		$saved = 0;		//keep track of how many rows are saved
 
		//Copy $this->data and then set $this->data to null.
		$tempData = $this->data;
		$this->data = null;
 
		//loop through the data and assign it in various ways
		foreach($tempData['Meeting'] as $id => $value)
		{
			//extract month, day, and year for saving later
			//everything else in $tempData['Meeting'] is a userID and checkbox value
			if($id == 'date_month')
			{
				$month = $value;
			}
			elseif($id == 'date_day')
			{
				$day = $value;
			}
			elseif($id == 'date_year')
			{
				$year = $value;
			}
			else
			{
				//set this to null or else it will only save one row and over-write
				//it with each loop iteration
				$this->User->Meeting->id = null;
 
				//set the date and add it to $this->data
				$this->data['date'] = $year.'-'.$month.'-'.$day;
 
				//set user_id and present and add to $this->data
				$this->data['user_id'] = $id;
				$this->data['present'] = $value;
 
				//save $this->data to Meeting and, if successfull, change $saved
				if($this->User->Meeting->save($this->data))
				{
					$saved += 1;
				}
			}
		}
 
		//Check to see if $saved == number of users.  If so, everything saved correctly
		if(count($users) == $saved)
		{
			$this->flash('Saved.', '/meetings/index2');
		}
		else
		{
			$this->flash('Error','/meetings/index2');
		}
	}
}

This example is kind of complicated because of what I was trying to do. The important things are these:

  • Loop through your data
  • Put the data you want to save into an array
  • Set the id to null
  • Save the data
  • Do it all again (and again and again . . .)

I hope that helps someone!
hydra12

3 Comments »

640

Comment by Jano

June 8, 2009 @ 12:47 pm

Thanks! This really helped me. This should be in the documentation of CakePHP, you should consider contributing to the book with this tutorial.

643

Comment by Jas

September 7, 2009 @ 5:52 am

Could post up the DB scheme and the rest of the view please? Shouldnt some of this be in the controller?

653

Comment by John M

March 19, 2010 @ 8:54 pm

Thankx. Found this after two days of struggle and it worked.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>