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

h1

Debugging and benchmarking MySQL in PHP

August 27, 2007

I stumbled across this link on my travels and it helped quite a bit in sriting some small piece of benchmarking MySQL with PHP. I took the code and replaced it into my own script that wrote to a database and then read from it. It needed one small change to make it work for me, I needed to add in:
$res = array();

Here is the link:
http://3rdmover.com/mysql-debugging-in-php

h1

Drag and Drop ordering

June 29, 2007

Here is a very useful link that walks you through how to create a simple drag and drop section for a website. It creates a list and lets you move items around the list so that you can order them as you wish. With a little bit of CSS, this could turn into a really nice simple organiser.

http://dustinweber.com/cakephp/cakephp-sortable-ajax-drag-drops-the-basics/

I did find that I had to edit my javascript includes a little bit though. I had the scriptaculous libraries loaded with this tagged on at the end
?load=effects
With this in place, I was getting errors that sortable wasn’t defined. Once I removed it, it all worked perfectly.

h1

Using autocomplete and Scriptaculous

June 27, 2007

Using this fine page as a reference (http://metapundit.net/sections/blog/ajax_autocomplete_with_scriptaculous), I finally got the autocomplete function working as I wanted.
I wanted to show a venue with some extra info in brackets and when it was selected, to update a hidden input with the id of the venue from the database.

The secret to this was realising that even though I was passing the id of the link tag, it was numberical which is not allowed. So following metapundit’s advice, I added auto_ to the front of the id value. This was then stripped off with javascript and the number put into the hidden input.

Also, a nice thing about the scriptaculous autocomplete is that if you want to show extra info but not have it passed to the page, enclose it in

h1

Having problems moving from localhost to remote server

June 20, 2007

I have part of my application working nicely now and it mostly does what I want. But that is only on my localhost. When I copy it up to the remote server, most of it runs. It’s only when I try to update the page with an AJAX call that I get:

Not Found

The requested URL was not found on this server.
Apache Server at Port

I know I ran into this problem before, but can I remember how to fix it? Not on your nelly.

Oh well, at least if I get it fixed this time, it will be logged here for me to remember!

h1

Submitting a form using javascript

June 19, 2007

I have been struggling with a way to change a form depending on what category was selected. I have a number of forms that input different levels of information depending on the category.

I started off using the $ajax->observeField() function. The problem was that the category selection was half way down the form, so anything that was entered previously was being lost when the form was submitted. To change this I thought it would be a simple change to using $ajax->observeForm(). But no. That would be too easy. :)

I ended up having to code it by hand, so here is what I did.

Create a DIV to take the information from the form after it is submitted.
Create the dropdown that will be used to select the category.
Add this piece of code:

codeBlock('Event.observe("categories",
\'change\',
function(event){ new Ajax.Updater(\'form\',
\'worksheets/add/'.$worksheet['Worksheet']['sdate'].'\',
{asynchronous:true, evalScripts:true, parameters:Form.serialize(Event.element(event).form), requestHeaders:[\'X-Update\', \'form\']}) }, false);'); ?>

This observes the “categories” dropdown and when it changes it will call the add function and pass the sdate to it. It then updates the “form” DIV with the results.

h1

Using unBindModel() in CakePHP

June 15, 2007

If you have a lot of associations in your model setups, sometimes you dont want all that information to be returned in a query. The answer to this is to use the unBinModel() function. Using this you can unbind certain parts of your associations.
The easiest way to demonstrate this is with an example.

If you have a user model with the following associations:

var $hasMany = array(
'Worksheet' => array('className' => 'Worksheet'
),
'Login' => array('className' => 'Login'
),
'Expense' => array('className' => 'Expense'
)
);
var $belongsTo = array(
'Level' =>
array('className' => 'Level'),
'County' =>
array('className' => 'County'),
'Province' =>
array('className' => 'Province')
);

That is potentially a lot of information to get back each time you query the User.
So if we define

$this->User->unBindModel(array(hasMany => array('Login', 'Expense')));

before we call

$this->User->findAll();

we no longer return information from the Login and Expenses tables. This can be used for any of the associations and can cut down query times dramatically.

h1

Leave an empty option at the top of a select element

June 1, 2007

In 1.2, when you use the $form->input() function to create a select element, it no longer creates the empty value at the top.
To get this back, simply pass the empty variable back and set it as true.


echo $form->input('category_id', array('empty' => true));

h1

A simple way to order results in a pagination query

May 29, 2007

In the controller, put this piece of code:


$this->paginate = array('order' => 'Topic.created DESC');
$this->set('topics', $this->paginate(array('Topic.board_id' => $id)));

h1

Why ‘created’ and ‘modified’ fields might not save

May 28, 2007

https://trac.cakephp.org/ticket/2595

I found this when I was searching for a reason that the created and modified fields were not automagically saving when I saved a record to the database.

What is says is that instead of defining the created and modified fields as NOT NULL, they should be defined as NULL and let Cake then go ahead and put an entry into them. This is for version 1.2

Use
`modified` datetime default NULL,
instead of
`modified` datetime default NOT NULL default '0000-00-00 00:00:00',