Wednesday, December 23, 2009

CakePHP - Load Model within Model

There are times that we want to link-up the model, but not the "hard way" example: hasOne, hasMany, belongsTo etc.
When you are in such situation the code below might be able to help you.

We can actually use the model function by using ClassRegistry class.
Below are the example:
class ThisModel extends AppModel {
var $name = "ThisModel";

function somename($id){
//note that "ThatModel" does not belongs to "ThisModel"
$rows = ClassRegistry::init("ThatModel")->findById($id);

Sunday, June 28, 2009

Get Random Key

Useful function for getting a random key


private function _getRandomKey($md5=false){
    $char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $key = '';
    $char_length = strlen($char)-1;

    for ($i = 0; $i != 32;  $i++){
        $random_pos = rand(0, $char_length);
        $key .= $char{$random_pos};

    return $key = $md5 ? md5($key) : $key;

Monday, May 25, 2009

CakePHP - Pagination

I've spent quite sometimes looking for this information, after lots of trial and errors I've found the solution.

If you want to know what is the param to be inserted in
$paginator->prev("Previous", null);

The second parameter has to be like below:
array("url"=>array("limit:".$limit)), url is fixed. In the array "name_of_param":"value_of_param"

If you have dynamic limit in your page, and you want to maintain it when you click on Previous or Next. This is the solution you need.

By the way, if your CakePHP is not up to date (like mine), when you want to do a toggle sort for your TH, the default function might not work. Update your pagination.php to this version:

Then you'll be able to toggle this sort('Name', 'name')?>

Good luck!

Sunday, May 3, 2009

CodeIgniter - Removing index.php by Changing .htaccess and httpd.conf

I've done this many, many times. But whenever I setup a new site on a new developer machine, I will have to do research over and over again. I decided to write down the details, actually only a few steps.
  1. Remove the index.php in the CI config file
    $config['index_page'] = "";

  2. Add .htacess file in the root of your application
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]

  3. Uncomment the mod_rewrite in httpd.conf
    LoadModule rewrite_module modules/

  4. Change None to All
    <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all

  5. As well as the web root folder (this is needed)
    <Directory "C:Apache/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
And done!

Tuesday, April 28, 2009

CakePHP or CodeIgniter

I’m deciding which framework to use, there are lots of pros and cons for both framework.  Personally I have experience in CodeIgniter, which I find it is fairly easy to use/ master. 

On the other hand, as the new company using CakePHP, I wanted to jump into the bakery world, but something stops me.

No doubt about it, lots of people are supporting CakePHP, if you type CakePHP vs CodeIgniter on Google, it will returns you tons of result.  I have a tight deadline to meet.  To pick up a new framework might not be a wise choice at this time.

At the documentation helps me to decide, CakePHP produce a bad use guide if compare to CodeIgniter.  As for me, I came from CI, I think I will stick to CI for the time being. 

I have decided, CodeIgniter is the way to go!  But I will revisit CakePHP, and definitely will get to know more about it.  Hopefully the future version will provide a better user guide and documentation.

Wednesday, April 22, 2009

Apache - Making Localhost and Virtual Hosts (vhosts) Work Together

I’ve been searching for this for quite sometimes almost forget about it.  Anyway, I’ve found the solution here to share…

If you have a situation that you want to round your live site from your machine, you can always change the hosts file and your conf file.  Example, you want to make local version accessible

Add setting in your Apache Configuration file (httpd.conf), for me I prefer to add in a new file so that it will not mixed up

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Include conf/extra/mysite-vhosts.conf


In mysite-vhost.conf, you have to set

<VirtualHost *:80>
DocumentRoot "C:/Apache/htdocs/"
    ErrorLog "logs/"
    CustomLog "logs/" common


Pre-create the log file if Apache prompt you error when restarting.

Next, change your hosts file to (your hosts file located in [Windows] C:/windows/system32/drivers/etc/hosts)

Do this: (you can access via or       localhost


If you restarted your apache right now and have other file that located in your htdocs folder, these site will not be accessible, for example your http://localhost/test will not work.  The solution:

<VirtualHost _default_:80>
    DocumentRoot C:/Apache/htdocs

Restart your apache and DONE!

Enjoy coding!!!

Ups and Downs

Everything seems so right, you have planned for each and everything. But you forget you depend on something to achieve your plan, what if the something failed?

Life is full of obstacles, there are ups and downs.  You will never stay down forever, and you will never stay up forever; you will go up one day, you will drop down one day.

I believe in law of attractions, if you have negative thinking, everything around you will turn bad, from bad to worse.  Bad energy attracted by your negative thinking.  I told myself, when things fall apart, be calm. 

So now I dropped from the top, the worst thing had happened, the good thing is I will no go down anymore, there is one and only way to carry on, climb up.

Time to take a break, you can never start a new and more exciting chapter if you did not end the previous one.  So do not stop if you face problem, the only you will fail if your stop trying.

nonono There are more than one beach around, look around you might find a better one.  Bring along your dogs, bicycle and your swimming suit definitely there will be useful in elsewhere!

Thursday, March 5, 2009


My dog had a serious sleep walking problem…


Thursday, February 19, 2009

Tuesday, February 3, 2009

A Thousand Words

A Thousand Words from Ted Chung on Vimeo.

Synopsis: “Every day so many opportunities to connect… What if you took just one?”

“A Thousand Words,” a beautifully shot b&w, short film directed by Ted Chung.

Monday, February 2, 2009

Automatic Mahjong Table

2 sets of Mahjong tiles… cool isn’t it?

But you will have to pay a bomb for this table (compare to standard table), and lose in a faster pace. –.-"

Thursday, January 8, 2009

Wednesday, January 7, 2009

Printing 3D - WOW!

At first I was thinking an ordinary printing using ordinary printer, after took a look on the video... Wow!!! It is so cool.  From Google Earth, then SketchUp then print... Nice...

Monday, January 5, 2009

Story about Speedy

One day, Speedy and friends were playing in the park. They were so happy they forgot the time to go back home. Steve, one of Speedy friend yelled "It is getting late now, let rush home before the traffic built up!" Sullivan reply anxiously "Yeah, lets run before it is too late!".

Without second thoughts, five of them were dashing across the busy street, squeezing between cars and bikes.

Suddenly a loud roar coming from a shinning black Aston Martin DB9, it heading towards Smith. "Look out Smith!" yelled Susan with the loudest voice she could ever coped. Less than a split second, Speedy flashed-in doing a two and a half summersault grabbing Susan away from danger with style, and landed perfectly on top of the DB9 bonnet with Susan in his arms.

Everyone was shocked, and silent.

A loud cheer coming from the traffic breaks the silent, "Speedy! Speedy! Speedy! Speedy!" Everyone seems to know about Speedy, everyone is cheering for him!


After that incident, Speedy became unexpectedly famous, even Steve, Sullivan, Smith and Susan imitate him by changing their name to Speedy. Not just that, everyone that heard about that incident start imitating him by changing their name as well.


Speedy counterfeits exist for a reason...



Thursday, January 1, 2009