I think many cakephp programmer will get confuse when they want to save column/field on table with mysql function such as NOW(), CURDATE(), etc, We cannot use simple way of the saveField method or save method on model object.
This code will not save the data field into current date time on mysql.
$this->ModelName->saveField('date_field', 'NOW()');
or
$data['ModelName']['id'] = 1;
$data['ModelName']['date_field'] = 'NOW()';
$this->ModelName->save($data);
It will not work as you want. Because cakephp will ada quote on the value.
(more…)
I found something strange on cakephp when i create form using FormHelper, a strange id appear on the action url. So i have this code on my view
<?php echo $form->create('Evoucher', array('url' => array('controller' => 'gift_vouchers', 'action' => 'buy', $giftVoucher['GiftVoucher']['amount'])));?>
It is generated a form tag with strange id added on action url. Something like this
<form id="EvoucherBuyForm" method="post" action="/gift_vouchers/buy/2/20" accept-charset="utf-8">
After exploration, i found the problem was on the controller action for buy. I have set id for the data variable
$this->data['Evoucher']['id'] = 2;
So after taking out this line, i change it into
$this->Evoucher->id = 2;
And cakephp can generate correct action url for the form tag
<form id="EvoucherBuyForm" method="post" action="/gift_vouchers/buy/20" accept-charset="utf-8">
I hope this tutorial help you. Thanks for visiting our blog
I just buy new shared hosting account for my new domain on hostgator. I want to install cakephp web application into this hosting. However the local setting or common setting that i used on other hosting is not work on hostgator. After figure out the problem, i found that the hostgator hosting does not allow for changing DOCUMENT ROOT setting.
So after research on cakephp tutorial, which come to setting cakephp on shared hosting and i think this solution also good for production phase. I will mention how i set my cakephp on hostgator (more…)
If you run redirect function on cakephp 1.1, don’t forget to add exit() function after redirect sintax. This thing can make security hole that confusing you. The purpose of adding exit() function after redirect sintax is to avoid php running others code after redirect function.
$this->redirect('controller/action');
exit();
However cakephp 1.2 give you default exit after redirect. The complete sintax for redirect on cakephp 1.2 is
$this->redirect('controller/action', null, true);
which the third parameter define exit = true, means terminate the script. second parameter is for setting exit status, for example 404, etc. Defaultly you only need to write
$this->redirect('controller/action');
Sometime we need to get the referer url that access a page or url on our website. It is easy to find referer url using cakephp framework. Inside your controller or cakephp class, you can get the referer url using this sintak
Controller::referer()
Referer url is the last page or url that accessed by the user before open current page or url.
Cakephp is a good framework, it is a rapid web development framework for building any type of website. As a framework, cakephp has many built in feature. One of cakephp feature is build in logging system. Developer can easily log their application using cakephp.
Log function that can be accessed through your controller easily. You can save or add new log into error log or debug log, or you can make new log file. All file will be placed inside CAKEPHP_FOLDER/app/tmp/logs folder
default sintax that added to CAKEPHP_FOLDER/app/tmp/logs/error.log
$this->log("error message log");
(more…)
Captcha is a type of challenge-response to ensure that the user is really a person that the response is not generated by computer. We usually find captcha on any form input on a website such us contact us, feedback, registration and etc. The captcha is used to protect our site from spammer.
The popular captcha is recaptcha from captcha.net website. It is free for use. Their interface or design also really nice to put on your website. You can check their sample in http://www.captcha.net/. They support both image captcha and audio chaptca.
Since i work with cakephp framework, i need to integrate this captcha into my application. I would like to share my captcha component for you. It is a simple code and easy to integrate. Here are the step (more…)
Cakephp has a good ways for handling table relationship model using AppModel association structure. It is easy to create and set relation between two or more tables on your database. Cakephp has setting for one to one, one to many, many to many relationship.
I would like to tell you about setting foreignKey for one to many relationship in cakephp that the master table doesn’t have foreignKey id. On your model, the relationship for one to many that set on child table would look like this
<?php
class CurrencyRate extends AppModel {
var $belongsTo = array('Currency' => array(
'className' => 'Currency',
'foreignKey' => 'currency_id',
));
}
?>
(more…)
Cakephp have great routing capability, i was install wordpress blog on my main website root directory. And then i need to move my blog into new folder, i put it on /app/webroot/blog/ because i build new web application on root directory using cakephp.
I use rewriting rule for my wordpress blog. The rule is http://www.balistupa.com/year/month/slug/. I want to redirect into http://www.balistupa.com/blog/year/month/slug/. I don’t want to loose my blog visitors because the blog posting has good ranking on google.
So i do setting on app/config/routes.php and add this line
Router::connect(‘/:year/:month/:slug’, array(‘controller’ => ‘pages’, ‘action’ => ‘redirect_blog’), array(‘pass’ => array(‘year’, ‘month’, ‘slug’), ‘year’ => ‘[0-9]{4}’, ‘month’ => ‘[0-9]{2}’, ‘slug’ => ‘[A-Za-z0-9-]+’));
(more…)
At this time, a company website normally should have blog to post some business activity or related activity with website themes. WordPress is the best blog framework (CMS) that i ever used. When i was build web application under my own php framework, i wasn’t worried about integrating wordpress into my website. It would has separated setting with main application.
However, now i am using cakephp framework. And if we need to put file or folder, it need to be put inside app/webroot folder. So if i need to install wordpress blog in http://www.balistupa.com/blog, i need to put the blog folder inside /app/webroot/.
The problem is about the url, with standard setting the blog would be redirected to http://www.balistupa.com/app/webroot/blog/ not to http://www.balistupa.com/blog/. This is not look good for visitor and also for seo. Because the folder structure is to deep.
(more…)