顯示具有 CakePHP 標籤的文章。 顯示所有文章
顯示具有 CakePHP 標籤的文章。 顯示所有文章

2009/03/12

快速升級 CakePHP

底下是我用來升級新版的 CakePHP 的作法:



  1. 解壓縮新版的 CakePHP 壓縮包

  2. 刪除解壓出來的所有 .htaccess


    • <解壓目錄>\.htaccess

    • <解壓目錄>\app\.htaccess

    • <解壓目錄>\app\webroot\.htaccess



  3. 更名以下檔案


    • <解壓目錄>\app\config\bootstrap.php => bootstrap.php.default

    • <解壓目錄>\app\config\core.php => core.php.default

    • <解壓目錄>\app\config\inflections.php => inflections.php.default

    • <解壓目錄>\app\config\routes.php => routes.php.default



  4. 將解壓出來的檔案複製到程式目錄中


 

2008/08/15

CakePHP 的 Model 中 id 屬性被修改的問題

我在使用 Auth 這個 Component 時,由於很多地方都會使用到 User 這個 Model 來實現一些功能,所以我在 app_controller.php 裡面於 __construct 時將一個 User 物件 new 出來,並將其設為 AppController 的屬性來共用。

問題發生在 user/add 的 action 上面,當執行 $this->User->save($this->data) 時,總是會改寫到登入 User 的資料,而非新增一筆 User 的資料。
後來我發現,$this->User 的 id 屬性變成登入 User 的 id(正常的狀況是,這個 id 屬性應為 false)。
所以我檢查 app_controller.php 使用到 User 的地方,發現只要在 beforeFilter 這個方法內,另外 new 一個 User 物件出來代替使用 AppController 的 User 屬性即可解決這個問題。

不過為了保險起見,在 user/add 這個 action 當中,我還是會加入以下兩行敘述在 $this->User->save($this->data) 之前:

unset($this->data['User']['id']);
$this->User->id = false;

2008/08/12

CakePHP 將預設值填入表單的方法

方法其實很簡單,在 Controller 裡面的 action function 裡面設定 $this->data 這個值即可。

add:
$this->data = $this->Model->create();

edit:
$this->data = $this->Model->read(null, $id);