CakePHP 2.0.0-RC1

Over a year after CakePHP 2.0 was announced, the team released Release Candidate 1 yesterday. It’s a very exciting news for all Cake developers. CakePHP 2.0 will not work on PHP4 systems anymore. It integrates SimpleTest, a new rewriting URLs structure, better handling in Authentication and new PDO drivers to work the same with different databases.

Checkout more from the original article.

http://bakery.cakephp.org/articles/markstory/2011/09/04/cakephp_2_0_0-rc1_hot_and_fresh

壹號車場

終於完成 Automall 的 revamp。因為一些外在因素,這網站遲了差不多半年才面世。又是一個 CakePHP + jQuery 的 project,當中遇到不少問題,例如CakePHP跟MSSQL的連接,Multilingual,處理某些Big-5編碼(舊系統用Big-5,而且MSSQL沒有UTF-8)會出現的怪獸字等。這些問題最終都能一一解決,多謝在網上分享的先知們及在MSN上當我求救的時候給我回應的好友。

其實這個網站還有很多地方可以改善,例如更好的SEO,使用AJAX後的Deep-linking等。

HKID Card check digit validation with CakePHP

HKID is always needed when filling application forms. It’s a string with combination of maximum 9 characters. The pattern is like this.

XY123456(Z)

X and Y are 2 letters from A to Z. After that is a 6-digit number. The whole HKID is ended with a check digit displayed with a pair of parenthesis. The validation rule can be found in Wikipeida – Hong Kong Idenitiy Card.

With the strong validation feature of CakePHP, we can define our customized validation rule for HKID check digit.

In the model class file.

var $validate = array(
     'hkid' => array(
        'checkDigit' => array(
            'rule' => 'checkDigit',
            'message' => '請填寫正確的香港身份證號碼。<br />Please input a valid HKID.'
        )
    )
);
 
function checkDigit($hkid){
    // the value is stored in $hkid['hkid'], store it back to $hkid for ease of use
    $hkid = $hkid['hkid'];
 
    // create an array for the weight of the letters
    $weight = array();
    for ($i=65; $i&lt;91; $i++){
        $weight[chr($i)] = ($i-65)%11+1;
    }
 
    // add a space with weight 0 for HKID without the first letter
    $weight[' '] = 0;
 
    if ((strlen($hkid) == 8) || (strlen($hkid) == 9)){
        // convert all letters to uppercase
        $hkid = strtoupper($hkid);
        // make all HKID become 9-digit long
        if (strlen($hkid) == 8){
            $hkid = ' '.$hkid;
        }
        // split the HKID string into a 9-element long array
        $chars = preg_split('//', $hkid, -1, PREG_SPLIT_NO_EMPTY);
 
        $value = 0;
        // calculate the sum with the 9-position value
        $value += $weight[$chars[0]]*9;
        $value += $weight[$chars[1]]*8;
 
        for ($i=2; $i&lt;8; $i++){
            $value += $chars[$i]*(9-$i);
        }
 
        // compare the check digit value and the calculated 11 - 11-modulus 
        if (is_numeric($chars[8])){
            $checkValue = $chars[8];
        }
        elseif ($chars[8] == 'A'){
            $checkValue = 10;
        }
        return ($checkValue == (11 - $value%11));
    }
    return false;
}

So simple but very handy validation function for checking HKID check digit.

Dealing with CJK in CakePHP

As a web development company in Hong Kong, we are in a lot of projects with multilingual support, usually English, Traditional Chinese & Simplified Chinese. Having the web application and database running in UTF-8 is very important.

CakePHP is set to run in UTF-8 by default. However, this is not the case with MySQL. If you do your MySQL connection with purely PHP, you need to run a SQL statement.

SET NAMES = 'UTF8';

This tells your PHP page to connect MySQL in UTF-8 collation. In CakePHP, the default database setting in database.php is like this:

var $default = array(
	'driver' => 'mysqli',
	'persistent' => false,
	'host' => 'localhost',
	'login' => {database_username},
	'password' => {database_password},
	'database' => {database_name},
	'prefix' => ''
);

This won’t connect MySQL in UTF-8. To add UTF-8 support with MySQL in CakePHP, simply add one line after ‘prefix’ => ”.

var $default = array(
	'driver' => 'mysqli',
	'persistent' => false,
	'host' => 'localhost',
	'login' => {database_username},
	'password' => {database_password},
	'database' => {database_name},
	'prefix' => '',
	'encoding' => 'utf8'
);

Data save to and read from MySQL will be in UTF-8.

.live() and change event in IE6 and IE7 with jQuery 1.4.2

I’m working on an online nomination application for The Hong Kong Academy for Gifted Education (HKAGE). Again, extensive work on CakePHP and jQuery. As usual, jQuery is linked from Google API. I only define the major version so that the application always link to the latest jQuery version. Bryan and I faced an issue on IE6 and IE7 with .live() and change event.

$("select").live("change", function(){
	alert('haha');
});

It worked on Firefox, Safari and Chrome but not IE6 & IE7. I felt strange about this as .live() has been implemented since jQuery 1.3 and it’s supposed to be stable in later versions.

To make this work in IE6 & 7, either use jQuery 1.4.1 or livequery.

I hope this can be fixed soon.