Learning CakePHP

May 27, 2011

JSTree – onclick event on link nodes not firing.

Filed under: Ajax, jquery, jstree — ldazo @ 6:14 am

[Note:  not really a cakephp issue ]

I am using jstree to represent my data like a directory listing. So far, jstree gave me the functionality i needed, till one of my teammate pointed to me that clicking a node in the tree does not fire up the onclick event i assigned to that node.

The json_data each node is as follows:

{"data":{"title":"1st Commentary","attr":{"id":"article-85", "class":"articles","onclick":"summary(event)"}} 

and the html output of the node is like this:

<div id="articles">
            <a href="#" onclick="summary(event)"  id=article-85">
            <ins>&nbsp;</ins>1st Commentary</a>

I have a function defined as:

function summary(event) {...}

and its not firing in Google Chrome, though it works perfectly okay with Mozilla Firefox.

The odd thing is that if i copy the whole ‘html output’ in a separate page, the onclick event works fine in Chrome. So i figured it might be a jstree issue. There must be a right way of delegating this event to a handler rather than specifying it to the list of attributes of each node. And after googling i found out that, yes there is. Thanks to a lot of people who continuously responds to questions in stackoverflow. Here is the thread [http://stackoverflow.com/questions/4611317/how-can-i-make-jstree-render-nodes-as-links]

so the solution to my problem is this:

        .delegate('a', 'click', summary);


March 28, 2011

Using Different Database Configurations for Reading/Writing Data

Filed under: CakePHP How-To's, Idiot's Guide — Tags: , , — ldazo @ 12:08 pm

When dealing with applications using large database plus numerous scripts on the background that constantly reads/writes data to the same database server, one of the problems we usually encounter is that tables get locked up during one of the processes. This causes our application to slow down. In this case, it is often necessary to separate database calls. Set up a slave DB with a user with read-only privileges, and which is constantly being synched with the master DB.

To make this work in cakePHP, you need to set-up two db configs. Define ur /config/database.php as such:

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '' ,
'login' => '*****',
'password' => '****',
'database' => '****',
'schema' => '',
'prefix' => **_',
'encoding' => ''
var $master = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '' ,
'login' => '****',
'password' => '****',
'database' => '*****',
'schema' => '',
'prefix' => '**_',
'encoding' => ''


The default configuration always uses the Slave DB. For security reasons, make sure that the login is set to a user with a read-only privilege.

Now create an app_model.php file. Create the following functions that shall override the parent model class:

function save($data = null, $validate = true, $fieldList = array())
$isSave = parent::save($data, $validate, $fieldList);
return $isSave;

function saveAll($data = null, $options = array())
$isSave = parent::save($data, $options);

return $isSave;

function updateAll($fields, $conditions=true)
$isUpdated = parent::updateAll($fields, $conditions);

return $isUpdated;

function delete($id = null, $cascade = true)
$isDeleted = parent::delete($id, $cascade);

return $isDeleted;

function deleteAll($conditions, $cascade = true, $callbacks = false)
$isDeleted = parent::deleteAll($conditions, $cascade, $callbacks);

return $isDeleted;

For a more complicated db setup, wherein you have multiple slave databases to access data from you could refer to my actual source here.

April 2, 2009

On Being Hard-Headed: Not following naming conventions

With all the features and possibilities cakePHP can offer, one sometimes find it hard to always adhere to cake’s naming convention. Like a table definition in your database should be always plural. The class name of the model for that table will be the singular name of the table and so on and so forth.

But as in some cases, someone (like me!) will not always want to follow this convention. Like for example, if I would want to create a table that shall contain some info’s on the site: amazon.com,  I would like to be firm on naming my database table as amazon and not amazons. 

So for that,  we have to tell cakePHP that the model amazon.php shall use the table amazon and not amazons.

So inside amazon.php:

  class Amazon extends AppModel {
    var $name = Amazon';
    var $useTable = 'amazon';

Or if you would rather not use a table at all:

  class Amazon extends AppModel {
    var $name = Amazon';
    var $useTable = false';

April 1, 2009

Tips and Tricks: Allowing Global Definition of Components and Helpers in CakePHP


I am starting to create my layouts and pages. But I encountered this problem (much to my annoyance. How much errors could I take?):

Notice (8): Undefined variable: javascript [APP/views/layouts/default.ctp, line 14]

I remembered encountering this before on my localhost development. So I browsed my local files and see that I created an app_controller.php inside app/controllers/

The controller just have these codes:


class AppController extends Controller {

  var $helpers = array(‘Html’, ‘Form’, ‘Ajax’, ‘Javascript’, ‘Time”);

  var $components = array(‘Session’); 



which just enables you to allow the use of the following helpers and components all throughout your cakePHP application without redefining these on individual controllers.

so. ciao! :Dn

Strict Standards: CakePHP Fresh Install

🙂 . It’s funny how my enthusiasm to refresh my cakephp knowledge turns to be an ‘ordeal’. I am now facing one error to another. After solving the .htaccess (Namely the 500 Internal Server Error) I am faced with yet another unfamiliar error. A number of errors show on my freshly installed cakephp.



My first instinct is that I have to change the “debug” setting mode of cakephp in app/config/core.php. However that is not the case. So I relied again to google and it (keyword search in Google: cakephp Strict Standards: Redefining already defined constructor for class Object in) returned a number of results and the first search result gave me an immediate most direct solution.

According to the blog, MAMP, PHP5, CakePHP, and Strict Standards, this is not a cakephp problem and instead a new PHP5 Error Standard (Oh my! another trouble with the new PHP 5) which can be turned off in two ways:


  • Changing your php.ini 
  • Or modifying the .htaccess in your root cakephp installation

The first method requires access to the php.ini (which I do not have) so I went on two solution # 2:

Or, in your .htaccess file in your root cake installation, you can add the following line:

php_value error_reporting 6143

And when I did that, I refreshed my page and saw that the error’s gone. (Yipee!!!)

It is also quite interesting to note that the reason why this error exists (still exists) is to maintain back compatibility with older version of PHP (that is PHP4).

More powers to the Open-Source Community and to the people who continually share their knowledge and experiences for people like me who is just starting to learn (really learn).


CakePHP: 500 Internal Error

Filed under: CakePHP How-To's, Idiot's Guide — Tags: , , , — ldazo @ 5:49 am

I decided it’s the time I build my own application on cakephp. So I downloaded the latest release from cakephp.org And uploaded them on my server in mosso.
I got a 500 Internal Error.

Looking up the web again. from various help, experiences and FAQs in blogs and other sites I found two articles:

In mosso (cloud sites), according to their knowledgeBase, due to their unique hosting environment, they require a slight addition to the code used for mod_rewrite. That is, an additional  line of code is necessary to make it work.

The RewriteBase should be specified for the root directory – / or to the subdirectory of the application. This is written after stating RewriteEngine on as follows:

RewriteEngine on
RewriteBase /

This should be specified in the .htaccess located at the web/content directory.

It is indeed quite frustrating encountering a problem you do not know the cause (since the page just tells you to contact the root-administrator of your site). Especially when you have already developed a whole cakephp application that is already working on your localhost and later finds out that an error  of this kind is thrown out upon uploading the entire cakeApp to your server.

It’s good thing that a lot of people are sharing their experiences in their blogs or forums so that people who might have experience this kind of problem has a reference.

October 24, 2008

CakePHP Blog:: Database Structure

Filed under: My Blog Application :) — Tags: , , — ldazo @ 8:40 am

I need the following tables for my blog application:

  • categories
  • posts
  • replies
  • users

There after some thinking I have thought I needed only four tables. 🙂

Of course, the posts -> non explanatory. Posts should be  categorized and are written by users. The exercise only asked for one user so the users table is almost unnecessary.


My First CakePHP Blog Application

Filed under: About, My Blog Application :) — Tags: , — ldazo @ 8:24 am

I build this using cakePHP 1.1.19. It was an exercise for a company I applied for.

The task was simple, build a blog application using cakephp. I was given 3 days to work this out.

From thursday night(I think.) to saturday night. So technically, I had two nights and one whole day to do it.

Let us, disregard the fact that it had been almost a year since I last dealt with anything that has something to do with cakePHP framework. But having just this span of time to build something, I do not know if I could do it.

Mainly because I have not really made a whole cakephp app on my own. I do not know how to handle sessions in cake, make a simple validation and other simple things like those. But I have a clear picture of how should my blog application should look like. Because I’ve been blogging for sometime. And I have been using a number of online applications that I think, I know what is expected from this work.

And so I accepted the challenge and spent the next hours reviewing cakephp basics, building my database and then baking… Ooopss.. I have forgotten the basics like apache mod_rewrite and how to actually use the bake script in Console.

Before I had difficulty with this. I have forgotten that changing configuration files in your server means you have to restart xampp again..

This and all that. I was able to finish the application on time. So I was happy. This exercise made me want to study cakephp more.

script to generate cakephp app -> changed

Filed under: Idiot's Guide — Tags: , — ldazo @ 6:53 am

I have jumped from cakePHP 1.1.17 to cakePHP 1.2.0 and there have been a lot of changes particularly the bake generator.

Then, i use the bake.php file located at the /cake/scripts/bake.php

(Since I am using Windows XP)

On the console i cd to C:/ then assuming my cakePHP app is contained under cake_app folder:

C:\>cd xampp/htdocs/cake_app

C:\xampp\htdocs\cake_app> C:\xampp\php\php.exe cake\scripts\bake.php

and the bake script runs.

Now the generator is run through cake.php inside the /cake/console folder.

If you do things without reading first, you’d be damned.

I had been..

About this Blog

Filed under: About — Tags: , , — ldazo @ 1:57 am

I know, i know. CakePHP has its own ‘nice’ framework to build a blog. So why am i using wordpress instead?

It’s free.

I’m poor, and I have to make use of the free-available resources. Soon maybe, i’ll put my own site using my own domain to build a cakephp application, but hey.. right now, i need a place to get attention, that is to blog what i know, what i learned and hopefully someone will learn from it to and most of all someone would tell me maybe what i know is wrong. I just need an online journal not because i want to brag or something. I just feel that I need one.

I already have a wordpress account.

It is frustrating for me to keep several accounts. Since i already have blogs in wordpress, it’s just another one-step registering another blog.

Older Posts »

Blog at WordPress.com.