CakePHP: Use Component In Model

I know it would make some Cake purists cringe, but I had to use a Component in a Model.

I wanted to use a component called ‘Geocoder’ in a model called ‘Address’.

In order for this to work, the controller that calls the function in Address must use the desired Component. Because I wanted to use the Geocoder component’s function in my beforeSave, I wanted to make sure that the call will work from every controller, and therefore added ‘Geocoder’ to the $uses in my custom app_controller.php, i.e:

class AppController extends Controller
    public $components = array('Auth', 'Session', 'GeoCoder');

Then, in my beforeSave function of my Address model I call the geocode method of my Geocoder component as follows, after using App::import and instantiating the component by its full name. It looked something like this:

function beforeSave()
   $gc = new GeoCoderComponent();
   $this->data['geocoded'] = $gc->geocode($this->data['address']);
   return true;

CakePHP: Model save function returning false without an error or SQL statements

I recently was running into an issue where the function $this->User->save($this->data); was returning false without any indication of an error, and also there was no SQL statement in the debugging section.

With some help from this stack overflow post, I found the error by putting the following after the save function in my controller:

echo var_dump($this->User->invalidFields());

This allowed me to see that the form was invalid because of a missing field, a field which I forgot to include the HTML for in the view and therefore the error was not displaying.

Loop Flowplayer

In Flowplayer 3.2.1, in order to the loop a clip you have to add a function onBeforeFinish which returns false. For example:

$f("player", "flowplayer.swf", {
    clip:  {
        autoPlay: true,
        onBeforeFinish: function() {
             return false;

CakePHP: Get html/output of view without view displaying or outputting using render

Looking to render a view output into a variable without the view actually displaying?  You need to create a new instance of a view with a second paramter of false, and then continue to use this variable to set variables and view paths.  When you call render, you can store the output in a variable and it won’t automatically display.  If autoRender is true in your controller, you’ve got to set it to false before proceeding.

Here’s an example of storing the html of an element called ‘box’ with the variable of ‘text’ without the view displaying or outputting:

/* Make sure the controller doesn't auto render. */
$this->autoRender = false;
/* Set up new view that won't enter the ClassRegistry */
$view = new View($this, false);
$view->set('text', 'Hello World');
$view->viewPath = 'elements';
/* Grab output into variable without the view actually outputting! */
$view_output = $view->render('box');

Localhost Subdomains on Ubuntu Linux 10.10 Maverick

This is on Ubuntu 10.10, with help from the following forum post:

The following is instructions on how to setup ‘http://test.localhost’ to use the document root /var/www/test

1) Modify hosts file.

Run the following command using ALT + F2:

gksudo gedit /etc/hosts

And add the following line	test.localhost

2) Modify apache config

Run the command:

gksudo gedit /etc/apache2/sites-available/default

Add the following lines:

    DocumentRoot /var/www/test/

3) Restart apache using the command

sudo /etc/init.d/apache2 restart

CakePHP model updating instead of inserting.

Cake 1.1’s documentation is a little lacking, again.

I was having a bug where a model was updating instead of inserting, even though I was sending in an array with all of the model’s attribute except for the id. In order to get the save function to insert instead of update, I had to explicitly set the model’s id to ‘null’.

Firefox Flash Plugin Being Overwritten In Ubuntu

In order to get debugging working with Adobe Flex Builder for Linux Alpha, I had to install the debugger version of the Flash 9 firefox plug-in.

I ran into a strange issue where after installing the Flash 9 debugger plug-in, the next instance of Firefox opened would use this correct flash version, but any other instances of firefox I opened after that would revert back to the regular version of Flash 9.

A quick post to the Ubuntu Forums revealed that running sudo apt-get remove flashplugin-nonfree fixes this without a hitch.

Simply Rails 2 Mistake: Unable to open database file

In Chapter 4 of Sitepoint’s Simply Rails 2, you are told to run the command

sqlite3 db/development.sqlite3

then execute the sql query

CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`link` varchar(255) default NULL,

Unfortunately, following these directions you get the error: Unable to open database "db/development.sqlite3": unable to open database file.

After searching a sitepoint post I found that you have to run rake db:migrate first in the command line from the shovell directory project first, and then run this SQL query instead:

CREATE TABLE `stories` (
`id` int(11) autoincrement NOT NULL,
`name` varchar(255) default NULL,
`link` varchar(255) default NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL