Win a new Mac Book Air laptop

Archive for the ‘CakePHP’ Category

Cake in the Wild

Friday, June 1st, 2007

I’ve just finnished my first real live application built with CakePHP. It’s nothing too wild - a booking management system based on 10 tables, the only vaguely fancy bit a couple of AJAX lookup / search fields.

Considering it has been the project I’ve actually built using Cake, and I’ve been learning the framework on the job, it has been very painless. It hasn’t taken long - it is an internal project for another department that I’ve squeezed in when I need a break from whatever else I’m doing.

The hardest part has been geetiing used to the format of query results, but that is easily solved with a quick print_r() and it has been a dream to debug and tweak, just working through it with the main guy who will be using it.

Training Database

Reflections on learning CakePHP

Friday, May 18th, 2007

I wrote this as a response to a post on this blog - it has a lot of useful links and it also got me thinking.

I’ve been a full time PHP developer for 3 years now (ASP prior to that) - over the past few months I’ve been learning and writing a couple of sites in Cake.

MVC is quite a different way of working to the way most PHP things seem to have been built (and the way I have built my projects) - but when you persevere and everything falls into place you don’t want to go back.

I find myself constantly checking to see if the work I’ve done so far in Cake is up to the point where I can stop using our current CMS framework at work and move to a Cake based system - unfortunately it isn’t quite there - but it will be soo, and I can’t wait.

Personally the hardest thing I have found is the transition to getting query results as quite complex arrays rather than the simple flat results that you get from mysql_fetch_array.

The one thing that I have found invaluble (when dealing with multi table relationships) as an aid is to simply pr() the variable holding my results array into the view I’m working on so I can see the paths I need to follow to the data I want.

Modified Muliple Checkbox Helper

Thursday, April 12th, 2007

I’ve been getting some very odd results with Joshua McFarrens excellent checkbox helper and ended up having to modify it slightly to get it to work. Here is my revised version:

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
 
<?php 
class HabtmHelper extends HtmlHelper { 
 
    /** 
     * Returns a list of checkboxes. 
     * 
     * @param string $fieldName Name attribute of the SELECT 
     * @param array $options Array of the elements (as 'value'=>'Text' pairs) 
     * @param array $selected Selected checkboxes 
     * @param string $inbetween String that separates the checkboxes. 
     * @param array $htmlAttributes Array of HTML options 
     * @param  boolean $return         Whether this method should return a value 
     * @return string List of checkboxes 
     */ 
    function checkboxMultiple($fieldName, $options, $selected = null, $inbetween = null, $htmlAttributes = null, $return = false) 
		{ 
 
       		$this->setFormTag($fieldName); 
       		 if ($this->tagIsInvalid($this->model, $this->field)) 
			 	{ 
            		if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") 
						{ 
                			$htmlAttributes['class'] .= ' form_error'; 
           				}
					else 
						{ 
                			$htmlAttributes['class'] = 'form_error'; 
						} 
        		} 
 
        if (!is_array($options)) 
			{ 
            	return null; 
        	}  
 
        if (!isset($selected)) 
			{ 
				$selected = $this->tagValue($fieldName); 
        	} 
 
		while(list($key, $name) = each($options))
			{
				$optionsHere = $htmlAttributes; 
 
				if(in_array($key, $selected))
					{
						$optionsHere['checked'] = 'checked'; 
					}
 
				$optionsHere['value'] = $key; 
            	$checkbox[] = "<li>" . sprintf($this->tags['checkboxmultiple'], $this->model, $this->field, $this->parseHtmlOptions($optionsHere), $name) . "</li>\n"; 
			}
 
 
        return "\n" . sprintf($this->tags['hiddenmultiple'], $this->model, $this->field, null, $name) . "\n<ul class=\"checkboxMultiple\">\n" . $this->output(implode($checkbox), $return) . "</ul>\n"; 
    } 
 
	} 
?>

(The difference between this and the original version is that the foreach loop has been replaced with a while(list loop that works in slightly different way.)

HABTM / Checkbox working example

Thursday, April 5th, 2007

Reading the CakePHP group messages, a lot of people seem to run into problems with creating HABTM relationships and changing from the default multipleSelect to using a series of checkboxes. In an app I was building I ran into a brick wall where I just wasn’t able to find out waht was wrong so I just decided to bake a quick example that I could use as a reference.

This is for Cake 1.1.x.x

I used a slightly modified copy of the SQL from the manual and used  Josh McFarren’s checkbox helper.

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
--
-- Table structure for table `posts`
--
 
CREATE TABLE `posts` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(50) DEFAULT NULL,
  `body` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;
 
-- --------------------------------------------------------
 
--
-- Table structure for table `posts_tags`
--
 
CREATE TABLE `posts_tags` (
  `post_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `tag_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY  (`post_id`,`tag_id`)
) TYPE=MyISAM;
 
-- --------------------------------------------------------
 
--
-- Table structure for table `tags`
--
 
CREATE TABLE `tags` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

I have made two changes to the SQL above:

  1. Removed the user_id field in ‘posts’ (to simplify the example)
  2. Changed ‘tag’ to ‘name’ in ‘tags’ - I changed this because generateList was not working and the $tags array geneated for the view did not contain the tag name ‘tag’ - only the ‘tag_id’ value.

You can download my working example, it contains all of the files that I create in bake and the files added and changed to use the checkboxHelper. To install the example:

  1. Bake a new application
  2. Connect to database
  3. Paste files from zip example to replace the newly baked files
  4. Create database tables using the SQL

HABTM_checkbox_example.zip

Session Error in Cake App

Tuesday, April 3rd, 2007

I have spent the past 24 hours pulling my hair out over a very odd error in a CakePHP application I have been developing.
I had an app called training, so my directory structure looked like this (the training app was generated using bake)

Dreamweaver Screenshot

I had copied large amounts of code from another cake site that I am also developing including a simple login system to password protect the admin routes.

I noticed that as I was browsing the site and tried to login, I would login successfully into the admin area and then as soon as I tried to access any controller method in the admin section I would be logged out.

I echoed all my POST and SESSION variables to the screen in the layout using:

1
2
3
<pre>
   <?php print_r($_POST); ?>
   <?php print_r($_SESSION); ?>

So I knew that I was being logged in successfully.

Session Screenshot

When I tried to access another controller action e.g. /admin/courses/index I would be logged out and asked to login again. Eventually I noticed that when I logged in I was being redirected to /training/admin/homes/index instead of the expected /admin/homes/index which is why I was being logged out.

I examined all the releveant files in my working application and my broken one and discovered that the only difference was that the working application was called /app/ and the broken one was called /training/ I re-named training to /app/ uploaded the site and it all worked again.

I am not sure at the moment whether this is the result of something I have misunderstood or if it is a bug, anyway I’ll stick it on trac when I get the chance.

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