How to control a cakephp cache duration

October 7th, 2009

I’m using CakePHP1.2.5.

Cache is very powerful function for web pages using heavy SQL queries. If you use the CakePHP default Cache, you can see cache files in the app/tmp/cache directory.

I want to use the Cake Cache function at many points of my system and set different cache duration.

Firstly, check the core.php for using Cache.

//in core.php
Cache::config('default', array('engine' => 'File'));

This is a sample script using cache function, duration is 1 minute.

class PostController extends AppController {

var $uses = array('Post');

function index() {

$return_data = null;
Cache::set(array('duration' => '+60 seconds'));
if( ($return_data = Cache::read('PostController-index') ) === false ){

$return_data = $this->Post->find('all');
Cache::set(array('duration' => '+60 seconds'));
Cache::write('PostController-index' , $return_data);
}

$this->set('data',$return_data);
}
}

If there is no cache file(PostController-index),get DB data with the Post model and create cache file with its result. Array data is serialized.
You access same page within 1 minute, cake read cache file(PostController-index)
and return it insted of getting data from the Post model.

Key point is to use Cache::set() before each Cache::read and Cache::write(), we can change cache duration in each point.
http://book.cakephp.org/view/773/Cache-set

Be careful setting cache file name without overlap at each point. I recommend the rule, Class name + function name + parameter.

BTW, we can set Cache::config parameter in Cache::read() and Cache::write().
http://book.cakephp.org/view/766/Cache-read
http://book.cakephp.org/view/767/Cache-write
So, we can define multiple cache config as follow and use it each cache point.

//in core.php
Cache::config('default', array('engine' => 'File'));
Cache::config('onemin', array('engine' =>'File','duration'=> 60,));
Cache::config('onehour', array('engine' => 'File','duration'=> 3600,));
Cache::read('PostController-index', 'onemin')
Cache::write('PostController-index' , $return_data,'onemin');

How to extend a plugin indirectly

September 16th, 2009

I’m using CakePHP1.2.4
At times, I want to modify/extend a  plugin indirectly.

How do I do that?
It’s very simple, only import and inherit.

Here is the plugin(cakeplus) I made on the github.
http://github.com/ichikaway/cakeplus/tree

In this example ,   I use the behavior(add_validation_rule.php) of the cakeplus plugin and modify some method.

1. download the cakeplus plugin, and set in the “app/plugins” directory.
2. make new directory(cakeplusplus) in the “app/plugins”.

Now you can see directories as follow.


plugins/cakeplus/models/behaviors/add_validation_rule.php

plugins/cakeplusplus/models/behaviors/

3. create “ext_add_validation_rule.php”  file in “cakeplusplus/models/behaviors/” directory .


App::import('Model', 'cakeplus.AddValidationRule');
class ExtAddValidationRuleBehavior extends AddValidationRuleBehavior {

}

4. Now you can use the “ExtAddValidationRuleBehavior” class which has same functions as the “AddValidationRuleBehavior” class.

class Post extends AppModel {

var $name = 'Post';

//var $actsAs = array('Cakeplus.AddValidationRule');
var $actsAs = array('Cakeplusplus.ExtAddValidationRule');

}

5.If you want to modify or extend, you just override a method of the “AddValidationRuleBehavior” class in the “ExtAddValidationRuleBehavior” class as follow.
In this example, override the maxLengthJP method and change that function.

App::import('Model', 'cakeplus.AddValidationRule');
class ExtAddValidationRuleBehavior extends AddValidationRuleBehavior {

function maxLengthJP( &$model, $wordvalue, $length ) {
$word = array_shift($wordvalue);

//extend
$length = $length * 2;

return( mb_strlen( $word ) <= $length );
}
}

Use placeholder in Model::query() of CakePHP

August 10th, 2009

I’m using CakePHP1.2.3

Model::query() is very useful for writing SQL statements as follow.

<?php

$this->Model->query("SELECT `Post`.`id` FROM `posts` AS `Post` WHERE `Post`.`id` = 100", $cachequeries = false);

To avoid SQL Injection, we want to use placeholders instead of embedding user input value in the statement.
We use “?” character in the SQL statement, set array data in 2nd parameter of the query method.
If 2nd parameter is array data in query method, it executes DboMysql::value() for escape value, using the “mysql_real_escape_string” function.

<?php

$sql = "SELECT `Post`.`id` FROM `posts` AS `Post` WHERE `Post`.`id` = ? LIMIT ?";
$this->Model->query($sql, array(100,1), $cachequeries = false);

Cake constructs a SQL statement and executes as follow.

SELECT `Post`.`id` FROM `posts` AS `Post` WHERE `Post`.`id` = 100 LIMIT 1

References
http://book.cakephp.org/view/456/query
http://en.wikipedia.org/wiki/SQL_injection

CakePHP in Japan

July 24th, 2009

Cakephp Bakers are increasing rapidly in Japan. I think there are some reasons as follow.

1. Books

There are over 7  japanese books for CakePHP!  Japanese Book is very important factor to get many developers in Japan.

Some books for beginner(XAMP install, bake, create small applications), some books for intermediate or higher level( component, behavior, helper, test cases, mobile site, Routing, callbacks, create practical applications).

You can see book covers followin link (Japanese web site).

http://d.hatena.ne.jp/keyword/CakePHP

2. Community Site

Japanese cakephp community ( http://cakephp.jp ).  There is a good forum on its web site. Many bakers ask questions and argue many topics in japanese , very usefull for not only beginners but also higher level users. Sometimes find some bugs in that discussion, then post tickets for trac.cakephp.org.

3. Events

Now in japan,  it is boom of meet-up for study( Programming, Design, Server Operation, etc ). In these meet-up, many different company people come together after working or in a holiday, and discuss some topics, often talking with drinking beer :-)

There were some CakePHP meet-up in  Tokyo, Osaka and Fukuoka. This is a report of CakePHP meet-up at Tokyo. http://bakery.cakephp.org/articles/view/report-cakephp-meet-up-at-tokyo-4th

In these meet-up, 50 or 100 japanese bakers come together, so nice events.

4. Blogs

There are many japanese baker’s blogs. Some bakers post good articles ( how to solve a problem, find bug, explain or make compoent, behavior, plugins,etc). If we have problems, we can find japanese articles of that resolutions  easily with the google.

It is very important to get many japanese information and resolutions easily.

Validation error message i18n

July 23rd, 2009

In a Model, we can define validation error message as follow

<?php
class User extends AppModel {
	var $validate = array(
		'email' => array(
			"email_invalid" => array('rule' => VALID_EMAIL,
				'required' => true,
				'message'    =>    'Invalid Email address.',
	),

But, can not use i18n function __() for validation error messages .
This is a solution to do it.

<?php
class AppModel extends Model {

	//Validation message i18n
	function invalidate($field, $value = true){
		parent::invalidate($field, $value);
		$this->validationErrors[$field] = __($value, true);
	}
}

Model::invalidate method is called in validation processing(Model::validates , Model::invalidFields).
This solution override Model::invalidate and set __() in each error messsage.

Then, you write po file of each language, language of validation error messages change depending on browser language configuration.

SQL syntax highlighting for Debug_kit

July 8th, 2009

I make SQL syntax highlighting feature for sql log panel of Debug_kit. I use GeSHi for syntax highlight.

Source code download here(include GeSHi).

You can see diff code here.

screen shot

debugkit_geshi_sql3

Hello Baker!

July 8th, 2009

test

<?php echo "Hello baker!"; ?>