close
我在使用 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;

arrow
arrow
    全站熱搜

    HACGIS 發表在 痞客邦 留言(0) 人氣()