Archive for the ‘CakePHP’ Category

Form data in the View

Monday, November 26th, 2007

Using CakePHP all your form fields are normally populated automatically, it is easy to forget that to access the POST array all you need to do is use either:

$this->data or $this->params[’data’]

pr(); on either of these will present you with something like:

Array
(
    [Book] => Array
        (
            [id] => 402
            [name] => Everything about Everything
            [publisher] =>
            [ISBN] =>
            [publish_date] => 15/04/2001
            [summary] =>
        )

)

Route Deja Vu

Wednesday, October 24th, 2007

I just had one of those odd moments that happen when you are stuck and are trawling through Google and you come across something you have written. At the moment I have a blessed respite from the usual rubbish at work of sorting out and extending other peoples bad code, and I am writing a pretty big website CMS using CakePHP. (I haven’t touched it for about a month and I had quite forgotten how pleasant it can be to work with.)

I’ve got a bit of a routing issue - well kind of - I have a situation like this:

I have a controller called Products based on a table called products that also uses a table called product_items (products is actually a table of product categories - but this is all in the name of nice urls, product_items contains the actual products)

For example:

  • http://www.somewebsite.com/products - shows you a list of product categories e.g. ‘Pet Foods’
  • http://www.somewebsite.com/products/pet_foods - shows you all the products within the ‘Pet Foods’ category
  • http://www.somewebsite.com/products/pet_foods/chappie - shows you the detail page for chappie

This all works fine - the way I have done it is to do the following, I have set up the following route:

1
2
 
$Route->connect('/products/(.*)', array('controller' => 'products', 'action' => 'index'));

So anything gets pointed at the index method in the controller, my code within the index method looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
function index($product_cat_url = null, $product_item_url = null) {
 
 
 
	if($product_cat_url)
			{
                           // check the product cat is valid
                        }
 
        if($product_item_url)
			{
                           // check the product item is valid
                        }
 
        etc.

This lets me pass all the actions through a single method - works great - but now I want to add locations so for instance I might have a url like:

  • http://www.somewebsite.com/products/london/pet_foods - shows you all the products within the ‘Pet Foods’ category in london

Obviously I can just add more code in the index method, but then this in itself might get really unwieldy - but it has got me thinking is there a better way of doing this? I was Googling and found the following thread where I used a beforeFilter() to pass it to a different controller - but then I might just end up duplicating loads of code in two methods…

A simple PHP calendar function

Friday, September 21st, 2007

Recently I needed a calendar to display on a site. I looked around for something I could just cut and paste and drop in but I couldn’t find anything, but picked up something I had started about six months earlier and which had then fallen by the wayside. Now calendars aren’t actually very complicated, but they are a bit of a fiddle. The aim of this calendar function is therefore just to produce a skeleton calendar that I can drop into anything, you can stick anything you want in a calendar cell (day) - it doesn’t matter because all the code for content lives outside the calendar. There are css hooks for all the various cells, columns etc. so you can format it exactly how you want.

Calendar Screenshot

I wrote this as a CakePHP helper, but at the end of the day it is just a function and there is nothing Cake specific about it.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
 
<?php
 
function calendar($year = '', $month = '', $data = '', $base_url ='')
		{
			$str = '';
			$month_list = array('january', 'febuary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
			$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
			$day = 1;
			$today = 0;
 
			if($year == '' || $month == '')// just use current yeear & month
				{
					$year = date('Y');
					$month = date('M');
 
				}
 
 
			$flag = 0;
 
			for($i = 0; $i < 12; $i++)
				{
					if(strtolower($month) == $month_list[$i])
						{
							if(intval($year) != 0)
								{
									$flag = 1;
									$month_num = $i + 1;
									break;
								}
						}
				}
 
 
			if($flag == 0)
				{
					$year = date('Y');
					$month = date('F');
					$month_num = date('m');
				}
 
 
 
			$next_year = $year;
			$prev_year = $year;
 
			$next_month = intval($month_num) + 1;
			$prev_month = intval($month_num) - 1;
 
			if($next_month == 13)
				{
					$next_month = 'january';
					$next_year = intval($year) + 1;
				}
			else
				{
					$next_month = $month_list[$next_month -1];
 
				}
 
			if($prev_month == 0)
				{
					$prev_month = 'december';
					$prev_year = intval($year) - 1;
				}
			else
				{
					$prev_month = $month_list[$prev_month - 1];
				}
 
 
 
			if($year == date('Y') && strtolower($month) == strtolower(date('F')))
				{	// set the flag that shows todays date but only in the current month - not past or future...
					$today = date('j');
				}
 
			$days_in_month = date("t", mktime(0, 0, 0, $month_num, 1, $year));
 
			$first_day_in_month = date('D', mktime(0,0,0, $month_num, 1, $year)); 
 
 
 
			$str .= '<table class="calendar">';
 
			$str .= '<thead>';
 
			$str .= '<tr><th class="cell-prev"><a href="' . $base_url . '/' . $prev_year . '/' . $prev_month . '">prev</a></th><th colspan="5">' . ucfirst($month) . ' ' . $year . '</th><th class="cell-next"><a href="' . $base_url . '/' . $next_year . '/' . $next_month . '">next</a></th></tr>';
 
 
 
			$str .= '<tr>';
				for($i = 0; $i < 7;$i++)
					{
						$str .= '<th class="cell-header">' . $day_list[$i] . '</th>';
					}
			$str .= '</tr>';
 
			$str .= '</thead>';
 
			// get the first day of the month
 
 
			$str .= '<tbody>';
 
 
 
				while($day < $days_in_month)
					{
						$str .= '<tr>';
 
 
 
								for($i = 0; $i < 7; $i ++)
									{
 
										$cell = '&nbsp;';
 
										if(isset($data[$day]))
											{
												$cell = $data[$day];
											}
 
										$class = '';
 
										if($i > 4)
											{
												$class = ' class="cell-weekend" ';
											}
 
 
										if($day == $today)
											{
												$class = ' class="cell-today" ';
											}
 
										if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day < $days_in_month))
											{
												$str .= '<td ' . $class . '><div class="cell-number">' . $day . '</div><div class="cell-data">' . $cell . '</div></td>';
												$day++;
											}
										else
											{
 
														$str .= '<td ' . $class . '>&nbsp;</td>';
											}
									}
 
						$str .= '</tr>';
					}
 
 
			$str .= '</tbody>';
 
			$str .= '</table>';
 
			return $str;
		}
 
?>

The function parameters are as follows:

  • $year - expects a year in 4 digit e.g. 2007
  • $month - expects a month in english e.g. january
  • $data - an array containing the data for each day of the month e.g.

    $data[2] = ‘This is an entry for the 2nd day of the month’;
    $data[24] = ‘A link for the 24th of the month‘;

    The data is any HTML you want - it is up to you to generate it yourself before you hand it to the calendar.

  • $base_url - the url to send the back / foward links on to i.e. the address of page (the calendar expects to be in a mod re-written situation e.g. www.flipflops.org/calendar/2008/june)

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
/* calendar CSS */
 
table.calendar {width: auto; border: 1px solid #cccccc; border-collapse: collapse; margin: 0px; padding: 0px; background-color: #ffffff;}
table.calendar th {background-color: #eeeeee; text-transform: none; color: #444444; padding: 4px; text-align: center; border: 1px solid #eeeeee;}
 
table.calendar th.cell-prev {text-align: left;}
table.calendar th.cell-next {text-align: right;}
table.calendar th.cell-header {width: 70px; border-bottom: 1px solid #cccccc;}
table.calendar td.cell-today {background-color: #e2e8f6;} /* today in the current month */
table.calendar td.cell-weekend {background-color: #F3F5EB;}
table.calendar td {border: 1px solid #cccccc;}
 
table.calendar td div.cell-number {text-align: right; font-size: 8px; color: #444444; display: block;}
table.calendar td div {display: block; font-size: 10px; text-align: left;}
table.calendar thead th {border: 1px solid #cccccc;}

I use this as a CakePHP helper - if anyone is interested I’ll put up the supporting code showing how it might be used in a view / controller situation.

CakePHP : clear app/tmp/model when you set DEBUG 0

Thursday, September 6th, 2007

I’m about to hand over a CakePHP based CRM system to the clients. Happy that everything was working as it should, I set DEBUG to 0. I immediately noticed that almost all the data had disapeared from the edit views and the detail views (although not the index views curriously eneough).

It took me quite a while to find the solution. It turns out that you have to delete all the files in the /app/tmp/model/ directory.

See CakePHP group Setting debug = 0 kills my app?

CakePHP : form field order problem when saving related models

Monday, September 3rd, 2007

On Friday afternoon I spent the last hour of my day pulling my hair out. I’ve been building a CRM system for a recruitment agency. We had just had a meeting and they asked me to change one of the relationships from Client hasAndBelongsToMany Location and Client hasMany Job to Client hasMany Job and Job hasAndBelongsToMany Location.

This should have been a quick simple change, but nothing was working. Nothing was erroring either though. This morning I came in and examined the $data POST arrays.

This is what I discovered:

This array saves the data:


Array
(
    [Candidate] => Array
        (
            [skill_1] => Swiming Instructor
            [skill_2] => French
            [skill_3] =>
            [skill_4] =>
            [skill_5] =>
            [title_1] => Mrs
            [forename_1] => Celine
            [surname_1] => Rogers
            [address] =>
            [postcode] =>
            [phone] =>
            [mobile] =>
            [notes] =>
            [email] =>
            [active] => 1
            [id] => 13
        )

    [Location] => Array
        (
            [Location] => Array
                (
                    [0] =>
                    [1] => 8
                    [2] => 11
                )

        )

)

This array doesn’t save the data:


Array
(
    [Location] => Array
        (
            [Location] => Array
                (
                    [0] =>
                    [1] => 10
                    [2] => 2
                )

        )

    [Job] => Array
        (
            [client_id] => 2
            [name] => Web Designer
            [summary] => Web Designer in Taunton
            [active] => 1
            [id] => 3
        )

)

The difference is in the working example the ‘key’ model in the relationship is listed first and the related model data (Location) is posted afterward.

I had a quick look at the views and ended up moving a hidden field (data[Job][client_id]) with the so that part of the ‘key’ model was posted first. Sure eneough the layout of the $data POST array had changed - and guess what the data saved correctly.


Array
(
    [Job] => Array
        (
            [client_id] => 2
            [name] => Web Designer
            [summary] => Web Designer in Taunton
            [active] => 1
            [id] => 3
        )

    [Location] => Array
        (
            [Location] => Array
                (
                    [0] =>
                    [1] => 10
                    [2] => 2
                )

        )

)

Now that I’ve got it working I can see that there is a certain sense to this - I can understand why it happens, but I certainly can’t remember reading anything about this.

The moral of the story? Maybe we need to think about the logical layout of forms more instead of shunting bits about until they look good (which is what I do in a rush). This isn’t necessarily a bad thing though, one of the benefits of using frameworks is that by following a set of rules we benefit from greater consistency of method in our code and therefore produce things that are more robust and bug free.

propecia canada order cheap accutane online find no rx levitra order no rx accutane buy discount levitra online order cheap accutane propecia malaysia drug viagra online purchase cheap levitra pharmacy cialis tablet compare propecia prices online order propecia no prescription required viagra viagra online pharmacy purchase accutane buy levitra from canada viagra overnight cialis from canada buy discount levitra viagra in uk propecia online purchase accutane no rx free cialis viagra no online prescription find accutane online levitra approved discount viagra drug viagra levitra in australia buying viagra online levitra internet accutane online order propecia from canada cheap accutane no rx drug cialis order levitra online buy propecia in us levitra australia cialis internet buy cialis overnight delivery cost of levitra cialis in malaysia accutane cheap drug cheap price propecia propecia overnight buy viagra no rx viagra vendors propecia buy online cheapest propecia online best price for viagra accutane no online prescription viagra price discount accutane levitra cost cheapest cialis online find viagra no prescription required propecia overnight delivery viagra free delivery accutane purchase buy cheapest viagra find accutane without prescription buy cheapest viagra on line find propecia on internet propecia drug buy propecia online cheap levitra no online prescription levitra malaysia low cost viagra low cost cialis propecia sales buy discount viagra buy propecia without prescription cheap accutane no prescription best price propecia cheap accutane from uk find propecia order generic cialis cheap propecia without prescription cheap viagra overnight delivery order discount cialis overnight cialis overnight accutane order levitra without prescription order propecia in canada find discount accutane online buy accutane internet cialis cheap drug find cialis on internet online pharmacy cialis buy cialis generic buy cialis on internet propecia medication free accutane order viagra in us order discount propecia online cheapest generic propecia online buy viagra online cheap cheap price levitra cheap cialis overnight delivery discount accutane overnight delivery purchase cialis overnight delivery order viagra no rx buying propecia online cheap cialis online order viagra cheap propecia in canada propecia approved levitra overnight shipping purchase propecia online accutane drug buy accutane overnight delivery cialis drug propecia tablet levitra no rx buy propecia low price compare cialis prices best price viagra buy accutane where to order levitra order levitra on internet no rx accutane order levitra overnight delivery find cheap propecia accutane without prescription buy generic levitra buy discount propecia online find no rx viagra lowest price viagra discount propecia overnight delivery buy cheap propecia online order propecia cheap online viagra for order order cialis from us drug accutane online purchase buy cheap levitra viagra india buy cialis low price buy levitra in us buy viagra from us accutane overnight delivery cheapest generic viagra approved accutane pharmacy cost viagra find levitra without prescription accutane order cheap propecia tablet find no rx accutane purchase accutane overnight delivery buy cheap cialis find accutane viagra without prescription viagra purchase propecia pharmacy levitra in malaysia sale levitra buy discount accutane online order propecia overnight delivery accutane from canada cheapest viagra price cheap accutane pharmacy cheapest accutane cheap viagra in uk cheapest cialis price viagra pharmacy online cialis us levitra bangkok lowest price for propecia cheap cialis in uk viagra no prescription viagra generic online viagra buying cialis generic cialis online lowest price accutane online cialis buy levitra on line drug levitra online purchase accutane tablet accutane india cheap price accutane order propecia without prescription accutane sales order cialis without prescription order viagra no prescription required cheapest cialis accutane us cheap viagra from canada propecia us buy cheap levitra online buy accutane without prescription cheap viagra from usa buy propecia no prescription required cheap propecia from usa propecia tablets cheap viagra in usa levitra canada buy generic propecia online generic levitra fda approved cialis purchase viagra no rx order cialis no prescription discount accutane online cialis buy drug find propecia online buy generic levitra online cialis without a prescription order levitra no rx buy cialis find cheap propecia online buy accutane in us buy cialis internet propecia without rx buy viagra in us buy cheap accutane levitra cheap drug compare levitra prices online cialis online pharmacy lowest price propecia levitra cheapest price buy generic accutane buying generic viagra accutane pharmacy drug propecia order discount levitra buy viagra on internet discount levitra online viagra medication cialis side effects buy discount cialis online buying accutane online order generic levitra propecia sale buy discount accutane order propecia cialis from india order accutane without prescription levitra for sale cheap levitra in canada cheap price cialis viagra australia propecia uk purchase cialis without prescription buy cialis in canada propecia no online prescription cheapest generic viagra online cialis buy online levitra uk no prescription viagra cheap propecia from uk order accutane in us cheap viagra internet buy accutane from india buy cheapest propecia on line buying generic levitra accutane buy drug buy levitra order cheap propecia online cialis without prescription cialis in us cheapest generic propecia cheapest levitra order levitra cheap online sale accutane propecia australia cialis without rx order cheap viagra online cheapest generic accutane viagra buy online find cheap viagra buy viagra no prescription required accutane cost find levitra on internet order cialis overnight delivery online pharmacy levitra order cheap levitra online find cialis no prescription required best price cialis accutane vendors order accutane overnight delivery find cheap levitra online cialis pill propecia no prescription low cost propecia cheap cialis internet cialis online review cheapest levitra online cheap cialis from uk propecia in uk viagra without rx levitra online review accutane side effects lowest price for levitra cialis bangkok buy no rx viagra order accutane from canada buy no rx propecia propecia buy discount propecia online viagra buy propecia rx viagra side effects viagra information accutane online without prescription cialis generic purchase cialis online levitra buy drug accutane pharmacy online accutane buy levitra no rx required propecia without a prescription propecia cheap drug cheap propecia pharmacy tablet accutane approved levitra pharmacy fda approved levitra order levitra from canada find cheap accutane online cheap accutane online buy accutane from us buy accutane online cheap levitra tablets levitra price cheap levitra tablet propecia order buying cialis online accutane pills discount levitra cheapest accutane online find discount cialis online accutane without a prescription buy propecia from us certified viagra buy generic propecia overnight propecia viagra cost buying accutane cost of propecia propecia without prescription buy cheap viagra online sale cialis order discount accutane online accutane free sample order accutane no prescription required cheap cialis from usa accutane overnight shipping buy cheap propecia internet accutane buy accutane low price levitra buy online buying generic accutane cialis no rx required order cialis cheap online buy accutane lowest price cialis in australia free propecia cheap cialis pill cheap accutane without prescription accutane medication levitra rx order levitra from us viagra rx accutane internet levitra for order find viagra accutane overnight buy accutane generic buying levitra buy viagra from canada propecia cheap price buying propecia fda approved accutane pharmacy cialis levitra overnight delivery propecia from canada find cheap viagra online cheapest generic cialis online accutane tablets cheap cialis no rx levitra in uk buy levitra overnight delivery viagra tablets accutane bangkok viagra approved generic accutane cheap levitra no prescription cheap cialis without prescription cheap accutane pill cialis australia tablet propecia buying viagra order accutane no rx purchase levitra online buy cialis online pharmacy levitra cheap accutane cialis pharmacy online cialis for sale order no rx propecia cheapest viagra online cialis overnight shipping cheap accutane on internet cheap cialis order cialis online buy propecia from india find discount viagra no prescription accutane accutane malaysia viagra in bangkok discount viagra online generic levitra cheap online pharmacy propecia cheap accutane tablet viagra cheapest price cialis levitra online without prescription discount propecia without prescription cheap propecia overnight delivery buy cheapest cialis online propecia online review certified propecia online pharmacy viagra viagra overnight shipping best price levitra propecia buy accutane in canada levitra without a prescription levitra sales accutane generic cialis no online prescription viagra in malaysia order cialis no prescription required accutane free delivery order no rx viagra viagra order discount cialis online viagra cheap drug buy cheapest accutane online cheap viagra online levitra online viagra online review order discount propecia viagra internet buy no rx levitra purchase viagra without prescription pharmacy propecia buy viagra in canada propecia in malaysia purchase accutane without prescription cialis overnight delivery no prescription cialis cialis order cialis buy levitra free delivery levitra information buy generic viagra online propecia accutane online review buy generic cialis