我是cakephp的新手,但從我所瞭解的所有數據庫交互應該發生在模型中。cakephp編輯字段值
我遵循官方cakephp博客教程,但他們使用控制器保存,編輯和刪除帖子,而不是模型。 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html
我試圖分解程序,所以模型負責所有的數據庫交互。我能夠得到它來保存新的條目,但我似乎無法弄清楚如何編輯它們。
以下是官方cakephp教程用於編輯帖子的原始動作。
public function edit($id = null) {
$this->Post->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Post->read();
} else {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
所以,這是我的控制器操作。我的控制器名稱是ConcatenatesController,我的模型是Concatenate。我最初用它來測試串聯字符串。
public function edit($id = null) {
$this->Concatenate->id = $id;
$this->Concatenate->editPost($id);
}
...這與它
function editPost($id){
if ($this->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
我收到以下錯誤
Notice (8): Trying to get property of non-object [APP/Model/Concatenate.php, line 20]
Fatal error: Call to a member function setFlash() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/cake/app/Model/Concatenate.php on line 24
線20轉到模型是指
if ($this->save($this->request->data)) {
和24行是
$this->Session->setFlash('Unable to update your post.');
這是不正確的方式正在做;本教程記錄了您應該如何做的方式。 – sevenseacat 2012-03-30 01:05:52
所以你告訴我我應該使用控制器與數據庫交互? – user1104854 2012-03-30 01:07:28
控制器不與數據庫交互。 '$ this-> Post-> save($ this-> request-> data)'它指示模型去做。 – sevenseacat 2012-03-30 01:34:54