$this->User->findAll("User.firstname = 'Daniel' OR User.firstname = 'Hugo'");
$this->User->findAll(array('or' => array("User.firstname" => 'Daniel'), array("User.firstname" => 'Hugo')));
2008年8月4日 星期一
[CakePHP]findAll / find 使用OR conditions
2008年6月1日 星期日
[Debug]CakePHP的Session無法完整寫入到資料庫DB
原來是因為中文編號的問題,
只要是有中文資料的Session,
在寫入到資料庫的過程式,就會寫入失敗,
變成空的資料。
但是就算在app_model.php中加入
還是一樣無法成功寫入到Session
最後改成這樣子,問題才得已解決
只要是有中文資料的Session,
在寫入到資料庫的過程式,就會寫入失敗,
變成空的資料。
但是就算在app_model.php中加入
function __construct(){
parent::__construct();
$this->execute("SET NAMES 'big5'");
}還是一樣無法成功寫入到Session
最後改成這樣子,問題才得已解決
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name',
'prefix' => '',
'encoding' => 'utf8'
);
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name-test',
'prefix' => '',
'encoding' => 'utf8'
);
}
2008年5月14日 星期三
[Debug]CakePHP MODEL::save()有問題
$this->Member->save($this->data['Member']一直無法寫入到Database
找了好久
後來發現原來是某欄位資料前面多了一個空格@@"
找了好久
後來發現原來是某欄位資料前面多了一個空格@@"
將
foreach($cols as $key => $col_name){
$this->data['Member'][$col_name] = $data['MemberInvite'][$col_name];
}
改成
foreach($cols as $key => $col_name){
$this->data['Member'][$col_name] = trim($data['MemberInvite'][$col_name]);
}
即可
$this->Member->save($this->data['Member']寫入成功!
2008年3月10日 星期一
[CakePHP]CakeSession needs support to set cookies over ALL subdomains
Description
I have a site where I need to preserve cookies over all sub-domains. Since it is quite useless to set the domain of a cookie to another domain (since browsers won't allow that) just allowing a flag to cover all subdomains might be useful.
Core...
in cake/libs/session.php function initSession()
摘自:https://trac.cakephp.org/ticket/830
I have a site where I need to preserve cookies over all sub-domains. Since it is quite useless to set the domain of a cookie to another domain (since browsers won't allow that) just allowing a flag to cover all subdomains might be useful.
Core...
define('CAKE_SESSION_SUBDOMAINS', true);
in cake/libs/session.php function initSession()
switch($this->security) {
case 'high':
$this->cookieLifeTime=0;
if (function_exists('ini_set')) {
ini_set('session.referer_check', $this->host);
}
break;
case 'medium':
$this->cookieLifeTime = 7 * 86400;
break;
case 'low':
default:
$this->cookieLifeTime = 788940000;
break;
}
//subdomain code
if (CAKE_SESSION_SUBDOMAINS){
if (substr_count ($this->host, ".") == 1){
cookie_domain = "." . $this->host;
}else{
$cookie_domain = preg_replace ('/^([^.])*/i', null, $this->host);
}
ini_set ("session.cookie_domain", $cookie_domain);
}
//end subdomain code
摘自:https://trac.cakephp.org/ticket/830
2008年2月20日 星期三
[CakePHP]讓 dreamweaver 可以讀取 thtml & ctp檔案
cakephp视图文件的扩展名都是thtml,不能用dw可视编辑,不过可以把cake\libs\controller\controller.php里的 var $ext=’.thtml’改成html就可以直接用html视图文件了.
不过这不是正式的方法,我看了lib里面好几个地方直接用了’.thtml’,所以会出问题。
最好就是用一下方法:
如果作業系統OS是XP
1、修改“C:\Documents and Settings\user_name\Application Data\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
2、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
3、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration\DocumentTypes directory下的MMDocumentTypes.xml”文件加入thtml,ctp字样。
如果作業系統OS是Vista
1、修改“C:\Users\user_name\AppData\Roaming\Macromedia\Dreamweaver 8”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
2、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
3、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration\DocumentTypes directory下的MMDocumentTypes.xml”文件加入thtml,ctp字样。
不过这不是正式的方法,我看了lib里面好几个地方直接用了’.thtml’,所以会出问题。
最好就是用一下方法:
如果作業系統OS是XP
1、修改“C:\Documents and Settings\user_name\Application Data\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
2、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
HTM,HTML,SHTM,SHTML,HTA,HTC,XHTML,STM,SSI,JS,AS,ASC,ASR,XML,XSL,XSD,DTD,XSLT,RSS,RDF,LBI,DWT,ASP,ASA,ASPX,ASCX,ASMX,CONFIG,CS,CSS,CFM,CFML,CFC,TLD,TXT,PHP,PHP3,PHP4,PHP5,TPL,LASSO,JSP,JSF,VB,VBS,VTM,VTML,INC,JAVA,EDML,WML,THTML,CTP:全部文件
.
.
.
PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP 檔案
3、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration\DocumentTypes directory下的MMDocumentTypes.xml”文件加入thtml,ctp字样。
1.<documenttype id="PHP_MySQL" writebyteordermark="false" file="Default.php" macfileextension="php,php3,php4,php5,thtml,ctp" winfileextension="php,php3,php4,php5,thtml,ctp" internaltype="Dynamic" servermodel="PHP MySQL">
如果作業系統OS是Vista
1、修改“C:\Users\user_name\AppData\Roaming\Macromedia\Dreamweaver 8”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
2、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration”目录下的Extensions.txt文件。在“PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files”处加入thtml,ctp字样。
HTM,HTML,SHTM,SHTML,HTA,HTC,XHTML,STM,SSI,JS,AS,ASC,ASR,XML,XSL,XSD,DTD,XSLT,RSS,RDF,LBI,DWT,ASP,ASA,ASPX,ASCX,ASMX,CONFIG,CS,CSS,CFM,CFML,CFC,TLD,TXT,PHP,PHP3,PHP4,PHP5,TPL,LASSO,JSP,JSF,VB,VBS,VTM,VTML,INC,JAVA,EDML,WML,THTML,CTP:全部文件
.
.
.
PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP 檔案
3、修改你的dreamwaver安装目录如 “C:\Program Files\Macromedia\Dreamweaver 8\Configuration\DocumentTypes directory下的MMDocumentTypes.xml”文件加入thtml,ctp字样。
1.<documenttype id="PHP_MySQL" writebyteordermark="false" file="Default.php" macfileextension="php,php3,php4,php5,thtml,ctp" winfileextension="php,php3,php4,php5,thtml,ctp" internaltype="Dynamic" servermodel="PHP MySQL">
2007年12月25日 星期二
Using distinct and count with CakePHP
This post is the answer to Mandy Singh’s comment to the article Select distinct with CakePHP in which he asked how to use “count” with “distinct”. The answer I gave then — to use a custom query — works, but there exists a more Cake-like solution.
The obvious solution — using findCount() — doesn’t work, as there is no way to specify that you want to use “distinct”.
So to get the desired result we have to use a find() or findAll() statement as shown in the following example:
The obvious solution — using findCount() — doesn’t work, as there is no way to specify that you want to use “distinct”.
So to get the desired result we have to use a find() or findAll() statement as shown in the following example:
$this->User->find(null, "COUNT(DISTINCT User.city) AS 'count'");
Redirect with exit
If you do a redirect in CakePHP 1.1 you often have to add an exit() after the redirect statement to stop the script execution:
Thanks to a small enhancement in CakePHP 1.2 in the form of an additional parameter for the redirect() function you can now do:
It’s the same functionality as in the previous code snippet, but with the advantage that your code becomes testable (you cannot test a function which contains an exit(), as the exit() also stops the execution of the test).
$this->redirect('controller/action');
exit();Thanks to a small enhancement in CakePHP 1.2 in the form of an additional parameter for the redirect() function you can now do:
$this->redirect('controller/action', null, true);It’s the same functionality as in the previous code snippet, but with the advantage that your code becomes testable (you cannot test a function which contains an exit(), as the exit() also stops the execution of the test).
Writing an installer for your CakePHP application
If you write an application which the user must install on his own server, you have to think about the installation process of your application. In this article I will describe the approach I have used in my application.
This approach is heavily inspired by the one used by WordPress. That means the user has to create the database and to define the database settings in app/config/database.php himself. When this is done, the user must run the installer, which then creates the tables and does some application-specific stuff like creating a default user.
The installer is a usual controller, which is automatically called when the user accesses the application for the first time. For this purpose I added a bit of logic to app/config/routes.php:
So as long as there is no “installed.txt” file, the user gets the first page of the installer when he requests the url of the application. The consequence of this logic is that we have to create such a file at the end of the installation process (see below).
The installer controller itself is straight-forward, each action is one step in the installation process. You can find a minimal version of the installer below:
It is a pragmatic solution with the drawback that it is not really reusable,..
This approach is heavily inspired by the one used by WordPress. That means the user has to create the database and to define the database settings in app/config/database.php himself. When this is done, the user must run the installer, which then creates the tables and does some application-specific stuff like creating a default user.
The installer is a usual controller, which is automatically called when the user accesses the application for the first time. For this purpose I added a bit of logic to app/config/routes.php:
if (file_exists(TMP.'installed.txt')) {
// the routes for when the application has been installed
} else {
Router::connect('/:action', array('controller' => 'installer'));
}So as long as there is no “installed.txt” file, the user gets the first page of the installer when he requests the url of the application. The consequence of this logic is that we have to create such a file at the end of the installation process (see below).
The installer controller itself is straight-forward, each action is one step in the installation process. You can find a minimal version of the installer below:
// app/controllers/
uses('model' . DS . 'connection_manager');class InstallerController extends AppController {
var $uses = array();
function beforeFilter() {
if (file_exists(TMP.'installed.txt')) {
echo 'Application already installed. Remove app/config/installed.txt to reinstall the application';
exit();
}
}
function index() {
}
function database() {
$db = ConnectionManager::getDataSource('default');
if(!$db->isConnected()) {
echo 'Could not connect to database. Please check the settings in app/config/database.php and try again';
exit();
}
$this->__executeSQLScript($db, CONFIGS.'sql'.DS.'app.sql');
$this->redirect('/installer/thanks');
}
function thanks() {
file_put_contents(TMP.'installed.txt', date('Y-m-d, H:i:s'));
}
function __executeSQLScript($db, $fileName) {
$statements = file_get_contents($fileName);
$statements = explode(';', $statements);
foreach ($statements as $statement) {
if (trim($statement) != '') {
$db->query($statement);
}
}
}
}
It is a pragmatic solution with the drawback that it is not really reusable,..
How to enable/disable debug messages for a certain action
An often asked question in the CakePHP IRC channel is: “How can I enable [or disable] the debug messages for a certain action?” The answer to this question is simple (works in both CakePHP 1.1 and 1.2):
Please notice that the key must be in lowercase, ‘DEBUG’ won’t have the desired effect.
function action() {
Configure::write('debug', '2');
// do something
}Please notice that the key must be in lowercase, ‘DEBUG’ won’t have the desired effect.
Select distinct with CakePHP
The solution is rather simple:
Or with the array syntax:
Please notice that the keyword “DISTINCT” has to be uppercase, else you will get the following error:
Unknown column 'distinct User.city'
$this->User->findAll(null, 'DISTINCT User.city');Or with the array syntax:
$this->User->findAll(null, array('DISTINCT User.city'));Please notice that the keyword “DISTINCT” has to be uppercase, else you will get the following error:
Unknown column 'distinct User.city'
訂閱:
文章 (Atom)