【发布时间】:2014-07-21 13:16:02
【问题描述】:
如何使用 cakephp formhelper 将变量值添加到表中。在我的表单中,我允许用户输入一些值,但用户的 IP 地址也添加到表中。在插入数据库之前,我还会计算用户的输入。
我应该修改 cakephp 中的控制器或模型吗?我有一个编号表,其中包含 id、cybernumber、created、department、ipaddress。非常感谢。
//Controller/NumberController.php
public function add()
{
if ($this->request->is('post'))
{
$this->Number->create();
//not working
//$this->request->data['Number']['ipaddress'] = $this->request->clientIp();
//not working
//$this->Number->set('ipaddress',$this->request->clientIp());
if ($this->Number->save($this->request->data))
{
$this->Session->setFlash(__('OK.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
// Model/Number.php
<?php
class Number extends AppModel {
public $validate = array(
'cybernumber' => array(
'rule' => 'isUnique',
'message' => 'unique only'
),
'department' => array(
'rule' => 'notEmpty'
)
);
}
【问题讨论】:
标签: php cakephp form-helpers