Archive for the ‘CakePHP 1.1’ Category

loadModel

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

loadModel('FoosFum');
$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.

 

References:
http://manual.cakephp.org/chapter/controllers
Google Group
http://manual.cakephp.org/chapter/components

 

 

Advertisements

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…

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:

array('value'=>1)
array('value'=>0)
array('checked'=>1)
array('checked'=>0)
array('checked'=>'checked')

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.

Example:

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.

Example:

$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.

Example:

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

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

Array
(
    [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']);