Category » CakePHP


I've been developing a site using Wildflower CMS developed using CakePHP and ran into an infuriating problem. After working on it for several weeks without issue I started receiving javascript errors that caused everything to come to a grinding halt. The errors were all reported using Firebug and the javascript debugger. The culprit:

Unterminated String Literal

After that in the Firebug console was:

$ is not defined

I knew there was a strong possibility that the first error was somehow causing the second and that top-down debugging was the way to go. After inspecting the javascript code I discovered that there was a variable assignment going on that spanned several lines. This might be legal in PHP but not javascript.

CakePHPI have fallen in love with CakePHP's integration of the SimpleTest libraries. With the type of work that I normally do, unit-testing is hard to utilize successfully. That is to say, most of the applications I work on have very straight-forward components and not a lot of complex functions/methods. I would only be testing whether or not they worked at all, rather than if they worked in a wide-array of situations.

For example, unit-testing a simple news list and detail page is probably overkill. Sure, you can test your classes by simple instantiating them but that only goes so far. My new method involves using SimpleTest's Scriptable Browser to actually crawl webpages and ensure that the proper data is being displayed. That way, I can catch all my php errors, including notices and warnings, insure that the proper headers are being sent, and assert that certain text is appearing on the page. Unit-testing will rarely catch a poorly coded method that throws a PHP notice whereas the Scriptable Browser will.SimpleTest

 

Automagically generated date/time input fields normally default to the current date and time. For a couple of reasons, I had to change this to another default value. For example's sake, let's say I needed a time field to always select 1:30 pm in an add action.

Run of the mill example:

<?php
    
echo $form->input('start_dt');
?>

This will output 3 select boxes; one for hours, minutes, and the merdian (am/pm) with the current time pre-selected. So if it was 3:04 pm, that would be selected.

So lets change this so that 1:30 pm is always pre-selected:

<?php
    
echo $form->input('start_dt', array('selected' => array('hour' => '1',
                                                           
'minute' => '30',
                                                           
'meridian' => 'pm')
                                 )
                );
?>

That's all there is to it! Cake's automagic owns. Hope this helps someone else :)

CakePHPI've been doing a lot of work with CakePHP lately and it has been amazingly satisfying developing in a framework that encourages great coding practices. More on CakePHP, specifically, later. Right now, I'd like to present some things I've learned about the architecture that CakePHP is built on. I'm talking about the Model-View-Controller (MVC) architecture of course.

I'm hoping most of you reading know already know a thing or two about MVC but if you don't, here is a quick overview from wikipedia.

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.