我需要從URL向cakephp控制器發送參數。我有兩個參數'ufrom'和'uto'的消息表。在控制器中,我想將這些值保存在消息表中。從url傳遞參數到cakephp
我把網址:
http://localhost/ar/messages/add?ufrom=9&uto=3
在MessagesController
我有功能:
public function add() {
if(($this->request->query['uto'])and($this->request->query['ufrom'])){
$this->Message->create();
if ($this->Message->save($this->request->data)) {
$this->set('addMessage',TRUE);
$this->set('ufrom',$this->request->query['ufrom']);
$this->set('uto',$this->request->query['uto']);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The message could not be saved. Please, try again.'));
}
$targets = $this->Message->Target->find('list');
$this->set(compact('targets'));
}
else{
$this->set('error',true);
}
}
和add.ctp我有:
<?php
if(isset($error)){
echo('error');
}
else{
echo json_encode($ufrom);
echo json_encode($uto);
echo json_encode($addMessage);
}
?>
但是當我使用上面的URL我看到:
Notice (8): Undefined variable: ufrom [APP\View\Messages\add.ctp, line 6]null
Notice (8): Undefined variable: uto [APP\View\Messages\add.ctp, line 7]null
Notice (8): Undefined variable: addMessage [APP\View\Messages\add.ctp, line 8]null
並且Nothing存儲在數據庫中。我是cakephp新手。請幫忙。
感謝您的支持描述但我的數據尚未存儲在數據庫中。 – sahar
http://stackoverflow.com/questions/9394080/cakephp-2-0-cannot-save請參考這個 – liyakat
非常感謝。我的問題解決了。 – sahar