Sep 20, 2007

Provide access to a model from a Component
Say you have a model called FoosFum in a file called foos_fum.php

$this->FoosFum = &new FoosFum();

Limit the size of your controller

If you only use a model in certain circumstances do not use the $uses array, use loadModel where you need it.


Google Group




Use JavaScript to Find the Center of the Browser.

Sep 13, 2007

I found myself in a situation where I needed to dynamically display a div in the center of browser’s window without knowing anything about the page or where the user was on it. For example, the user could be at the top of a page or could have scrolled 3000 pixels down. I also wasn’t allowed to use mouse clicks or anything to determine where the users was.

So, to use JavaScript to find the center of the browser’s window regardless of where the user is:

posY = getScreenCenterY();
posX = getScreenCenterX();

dv = document.createElement('div');
dv.style.cssText = "position:absolute;top:"+posY+"px;left:"+posX+"px;";

function getScreenCenterY() {
var y = 0;

y = getScrollOffset()+(getInnerHeight()/2);


function getScreenCenterX() {

function getInnerHeight() {
var y;
if (self.innerHeight) // all except Explorer
y = self.innerHeight;
else if (document.documentElement &&
// Explorer 6 Strict Mode
y = document.documentElement.clientHeight;
else if (document.body) // other Explorers
y = document.body.clientHeight;

function getScrollOffset() {
var y;
if (self.pageYOffset) // all except Explorer
y = self.pageYOffset;
else if (document.documentElement &&
// Explorer 6 Strict
y = document.documentElement.scrollTop;
else if (document.body) // all other Explorers
y = document.body.scrollTop;

Many thanks to Quirksmode for this page: www.quirksmode.org/viewport/compatibility.html

Not So Stupid Trick… compact()

Aug 16, 2007

Here is a good post that explains how to make several local variables available to the view at once.

Of course, you can always reference $this->data in the view…

Count characters in a textarea.

Aug 16, 2007

You just need the bolded text…


<script type=”text/javascript”>
function textCounter(textarea, counterID, maxLen) {
cnt = document.getElementById(counterID);
if (textarea.value.length > maxLen)
textarea.value = textarea.value.substring(0,maxLen);
cnt.innerHTML = maxLen – textarea.value.length;

<form action=”whatever.php”>

<textarea name=”text_area” cols=”50″ rows=”5″ onKeyUp=”textCounter(this,’count_display’,3500);” >
onBlur=”textCounter(this,’count_display’,3500);” >

<span id=”count_display”>3500</SPAN> characters remaining

<input type=”submit” value=”Submit”>

Open a Form

Aug 15, 2007

There isn’t really a helper method to create a form in Cake 1.1.X; it is deprecated. To create a form the easiest way would be to write:

form action=”<?php echo $html->url(‘/’ . $this->controller->name . ‘/’ . $this->controller->action)?>” method=”post”>

Checkbox and Database

Aug 14, 2007

To make life somewhat easier, database columns that will be rendered as checkboxes should be of type tinyint (or number) with values of zero and one (where 1 is checked).

The definition of the html->checkbox function is (note: title is not used):

checkbox($fieldName, $title = null, $htmlAttributes = array(), $return = false)

Whether or not the checkbox is checked is based on the htmlAttributes array. To illustrate what I mean, these $htmlAttribute arrays will result in a checked checkbox:


A bit of odd code does make the value of 0 (zero) also check the checkbox, so do not do this

echo $html->checkbox('Model/column', null, array('value'=>$ind));

and expect the checkbox to be unchecked if $ind is zero. It seems this would be the right thing to do to optimize your code, however, it does not work as expected.

So, to create a checkbox you’d write the easiest thing possible:

echo $html->checkbox('Model/column');

and CakePHP will make it checked for you (no extra code required).

echo HTML Helper output

Aug 14, 2007

If you do not have AUTO_OUTPUT set to TRUE in /cake/app/config/core.php then be sure to echo all HTML helper methods.


echo $html->input('Model/column', array('size'=>'1', 'maxlength'=>'1'));

This is also true when rendering elements (which is also done in the View):

echo $this->renderElement('menu');

Variable title

Aug 14, 2007

The variable $title is reserved for the title of the page. Do not use it for anything else.


$this->set('title', 'Add a Post');

Find Function

Aug 14, 2007

The find() function has the following parameters:

  • string $conditions
  • array $fields
  • string $order
  • int $recursive

Returns the specified (or all if not specified) fields from the first record that matches $conditions or FALSE if no data is found. Recursive must be -1 to not return any associated data.


$one_row = $this->ModelName->find('id = ' . clean_id,  null, null, -1);

Return example (i.e. value in $one_row):

    [ModelName] => Array
            [id] => 1000
            [name] => Ford
            [status] => A
            [created] => 2007-08-04 00:42:17
            [modified] => 2007-08-04 00:42:17


So to get the value of name to your view you could write:

$this->set('name', $one_row['ModelName']['name']);