Archive for the ‘CakePHP’ Category

h1

Containable in CakePHP

May 26, 2009

I’ve see a lot of traffic to my post about Using unBindModel in CakePHP and while it is a way of cutting down the data that is returned from a query, there is now a better way. Containable behaviour

I’ve only just stumbled upon this and after having used it briefly in testing it seems like a really cool way to chain together models and yet only get the data that you want at the time.

I have used it like this to get the County that a Club is in

$this->User->contain(array(
            'Club' => array(
                'County'
            )
        ));
        $user = $this->User->find('first', array(
            'conditions' => array(
                'User.id' => 1
            ),
        ));

This query brings back an array with User data, Club data and County data. The interesting bit is the Club

[Club] => Array
        (
            [id] => 1
            [name] => Ballymun Kickhams GAA Club
            [county_id] => 2
            [County] => Array
                (
                    [id] => 2
                    [province_id] => 1
                    [name] => West Dublin
                )

        )
h1

Flash Chart Helper

May 25, 2009

I’ve been using the Flash Chart Helper recently and it’s been working really well once I got a couple of things ironed out with it.

I followed the instructions and copied all the files to the right place. I had the array created and sending what I thought was the correct format of data to be graphed but I was still getting errors. Originally I had this line:

echo $flashChart->begin(array('prototype'=>true));

at the top of my graph. When I removed the option array (because I wasn’t using prototype) it gave me a lovely graph.

Next thing was add labels to the X-axis. My data set was being sent in the following format:

Array (
[0] => Array (
    [day] => Monday
    [hits] => 0 )
[1] => Array (
    [day] => Tuesday
    [hits] => 15 )
[2] => Array (
    [day] => Wednesday
    [hits] => 58 )
[3] => Array (
    [day] => Thursday
    [hits] => 23 )
[4] => Array (
    [day] => Friday
    [hits] => 23 )
[5] => Array (
    [day] => Saturday
    [hits] => 99 )
[6] => Array (
    [day] => Sunday
    [hits] => 34
) )

So I set up

echo $flashChart->setData($logs, '{n}.hits', '{n}.day', 'byDay');
echo $flashChart->axis('x', array('labels' => $logs));

To explain that, $logs is the array of data, {n}.hits is the data to be graphed, {n}.day is the label path, byDay is the name of the graph. The options for the axis are simply the array to pass in to read the labels from.

I then wanted the labels to be vertical so that they wouldn’t overlap. This proved to be a harder thing to fix. In the API on page three of the helper article, it only mentions

axis($axis, $options = array())

I added in the option of

'vertical' => true

but I got an error that the helper couldn’t find the set_vertical() function. After some digging around in the source, I found that the real way to pass label options was as a third variable to the axis function. So I edited my call to read

echo $flashChart->axis('x', array('labels' => $logs), array('vertical' => true);

and lo and behold, vertical labels!

h1

DebugKit

May 17, 2009

Wow, this is pretty awesome!
I just downloaded DebugKit and installed it in a project I’m working on (more on that later).

It’s a nifty little plugin that gives you details about what queries are being called, what variables are being used, how long the page takes to render and a whole heap of other information. It should be pretty useful to debug some problems that I am inevitably going to run into.

I was unable to clone the repository from thechaw.com due to permissions being denied. I think you might have to sign up to it to be able to download. In the end I just downloaded the tar files and installed them into app/plugins and added var $components = array(‘DebugKit.Toolbar’); to app_controller.php.

That’s all there was to it

h1

Running cakePHP on IIS

July 3, 2008

I am trying to get cakePHP 1.2 running on IIS6 with a MSSQL backend, and so far it’s going pretty well. Once I got PHP running on the server using instructions from Peter Guy, it was a simple thing to copy up the cake folder.

I tried loading up the URL for the site but only got a directory listing. To fix this, go to the permissions window of your site and then to the Documents tab. Add index.php to the default content page section and voila, it now loads without any CSS.

To enable the css, there was one more change to make. Thats was in /app/config/core.php. Just uncomment the following line:
define ('BASE_URL', env('SCRIPT_NAME'));
hit reload and Bob’s your auntie’s husband.

h1

It’s been a while

June 22, 2008

I’ve been working on other things recently so have unfortunately left this blog a little quiet. I’m going to be starting a new project shortly that will access a MS SQL database on a separate server than the webserver I will be coding on. It’s only a simple internal user management application but it should give me a few new problems that I can blog about.

h1

Mod Rewrite on Leopard

December 10, 2007

After upgrading to Leopard, I had a wee problem with trying to run Cake. It turns out that you need to enable AllowOverride.

In the line that follows:

<Directory “/Library/WebServer/Documents”>

Change “AllowOverride None” to “AllowOverride All” and then restart Apache

h1

Problems with AJAX and IE

September 18, 2007

I had set up a page with a nice drag and drop interface and it worked lovely in Firefox and Opera. But when I went to try it in IE, it would work once and then not again.

What was happening was that after the drop, the page would refresh and the drop area would be created again inside of the old one. Firefox ignored it but IE decided that it wanted things to be right for a change.
So adding this little bit of code between script tags:

if (document.all) { Droppables.drops = [] }

before the creation of the drop area meant that it would clear out the area for a new one to be created.

This page has the full explanation:
AJAX problem with drag and drop for Internet Explorer using Scriptaculous

h1

This page is handy

September 17, 2007

Drag ‘n drop tutorial with the CakePHP 1.2 Ajax helper, Prototype framework and Scriptaculous library
Drag and Drop explained really nicely. The only thing that I have changed from his method is to put a letter in front of the id name for each option that will be dragged. This I then strip in the controller and play as usual. I made this change because my pages weren’t validating any more.

h1

A nice way to check if there are orphaned entries

September 5, 2007

In a database table that is linked to another, sometimes an entry gets deleted and the children of that entry don’t get deleted along with it. Use this little bit of SQL to check for these entries:
SELECT one.id
FROM one
WHERE one.other_id NOT IN (
SELECT other.id
FROM other);

This can then be modified to delete those entries by changing the SELECT one.id to DELETE, like this:
DELETE
FROM one
WHERE one.other_id NOT IN (
SELECT other.id
FROM other);

h1

If you have problems with beforeFilter

September 3, 2007

I was having some problems trying to use the beforeFilter function in my controllers because I was using it in app_controller.php as well. In app_controller.php I was playing with the session variables and this was breaking when I went to use the beforeFilter function in any of my controllers. The workaround is very simple and came from the mailing list.
In you controller version of the beforeFilter function, add in the following line:
parent::beforeFilter();
before you do anything in the function. This explicitly calls the beforeFilter function in app_controller.php and runs it first.

Thanks to Jon Bennet for that little nugget