Archive for 九月 2006
剛剛跟 thegiive 聊天的時候,他問了我一個CakePHP的問題。
CakePHP 的 model 能自動 save hasMany 的 entry 嗎?
經過測試後發現似乎沒有辦法。
不過還是可以經由修改 model 做到類似的功能。
在所建立的 model 中加入一個 function , afterSave ,afterSave 是 Model 的 CallBack Function …
在save後會自動被呼叫。
寫入類似下列的 內容,假設我是 hasmany 一個 Post 的 model …
function afterSave(){
if ($user_id = $this->getLastInsertID()){
foreach ($this->data['Post'] as $val){
$this->Post->create();
$data['Post'] = $val;
$data['Post']['user_id'] = $user_id;
$this->Post->save($data);
unset($data);
}
}
}
這樣子在存檔的時候,只要輸入的資料是類似這樣…
date => Array( [User] => Array ( [name] => Darkhero [email] => darkhero@gmail.com )
[Post] => Array ( [0] => Array ( [title] => test1 [content] => content1 )
[1] => Array ( [title] => test2 [content] => content2 )
[2] => Array ( [title] => test3 [content] => content3 ) ))
--
去 cakephp 的 irc 問了才知道有 model::create 初始化一個物件,所以重新改一下程式碼...
[@more@]
No tags
在TWPUG中有朋友問可以不可以require 一整個目錄。
但是PHP的 require 本身沒有這個功能,所以只好自己寫一個…
還沒有試過,但是應該是可以run的啦~
<?php
/**
* Recursion require file.
* If $target is directory then include all file in the directory.
*
* @param string $target
*/
function require_recursion($target){
if(is_dir($target)){
$d = dir($target);
while (false !== ($entry = $d->read())) {
if ($entry != '.' and $entry != '..') {
if (is_dir($entry)) {
require_recursion($entry);
}else{
require($entry);
}
}
}
}
}
?>
[@more@]
No tags
等了好久,終於有比較好的解決方案了!
http://projects.simpleteq.com/CakePHP/smarty.html
直接把 Smarty 繼承 View 作成了 SmartyView,也可以在 tpl 中直接用 {$html->url()}…
[@more@]
No tags
剛剛在逛網站的時候看到的。
CakePHP 範例: 簡易使用者認證
http://987.tw/home/p2_articleid/18
很棒的一篇翻譯文章,清楚明白,若是想找簡單的使用者認證功能,可以參考這篇喔!~
[@more@]
No tags
之前我在 CakePHP 中用的都是 FCKeditor ,最近則越看TinyMCE越順眼,所以找了一下有沒有 TinyMCE for TinyMCE 的文章…
http://cakephp.org/pastes/show/cf924a342aa55ba3cfe3aea6cb1ed29b
實作方式如下:
- 先將 TinyMCE 下載後解開.
- 將 TinyMce中 jscript 目錄下的 tiny_mce 完整的考貝到 WEBROOT/js/ 下.
- 在你的layout中,修改 default.thtml (如果你有用其他的也跟著修改吧~) ,在 <head></head> 中間加入
<?php
if(isset($javascript)):
echo $javascript->link('tiny_mce/tiny_mce.js');
endif;
?> - 最後在你的有需要用到 tinymce的網頁,例如 add.thtml 跟 edit.thtml 加入下列javascript
<?php
<script language="javascript" type="text/javascript">
tinyMCE.init({
theme : "advanced",
mode : "textareas"
});
</script>
?> - 看看你的新增修改頁面是否已經自動把tinymce啟動了呢~
[@more@]
No tags
今天女朋友傳給我一張照片,很有趣,我覺得好像是 Gnome Boy 阿!!
轉寄的email原文:
> 在兆豐農場看到一個弟弟,他ㄉ頭超酷的…
>
聽說是他爸剃ㄉ!哈哈哈…
No tags
這幾周很忙,加上這兩天身體很不舒服,整天都是頭暈眼睛痛的狀態,根本無法做事情,所以寫點簡單的東西好了。
MySQL 4.1 以上由於有字元編碼設定的問題,所以所有的PHP程式都需要跑一下 set names xxx …(關於編碼的問題請看MySQL 中文編碼徹底研究)
而CakePHP的編碼設定要放在哪呢?
根據網路一陣搜索,找到的比較好的方法是利用自建的 app_model.php 來完成.
[@more@]在你的app目錄下,加上一個檔案,app_model.php(也可以從/cake/拷貝過來)
裡面應該有個空的物件
class AppModel extends Model
{}
這個檔案是用來讓自己建立額外的AppModel function 用的…
我們要加上自動送出 set names utf8 的話要這樣作…
class AppModel extends Model{
function __construct(){
parent::__construct();
$this->execute(‘set names utf8′);
}
}
當然,如果你用的是 big5 ,就要改用 set names big5 喔..~
No tags


