我正在尝试将CakePHP从1.3迁移到2.0。现在,我遇到了一个关于将值插入特定字段的小问题。这是我在1.3版中创建的代码:
function add() {
if(isset($_SESSION['User']['id'])){
if(!empty($this->data)) {
if($this->Ad->save($this->data)){
//saving the uID in table ads
$this->data['Ad']['uID'] = $_SESSION['User']['id'];
$this->data['Ad']['DatePosted'] = DboSource::expression('NOW()');
$this->data['Ad']['IP'] = $_SERVER['REMOTE_ADDR'];
$this->Ad->save($this->data, FALSE);
$this->Session->setFlash('The post was successfully added!');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('The post was not saved. Please try again.', 'alert-message', array('class'=>'error'));
}
}
}
else{
$this->Session->setFlash('You have to login first to access control panel');
$this->redirect(array('controller'=>'users','action'=>'login'));
}
//for setting the category of advertisement
$adcats = $this->Ad->Adcat->find('list',array('fields'=>array('id','Description')));
$this->set(compact('adcats'));
//for setting the type of advertisement
$adtyps = $this->Ad->Adtyp->find('list',array('fields'=>array('id','Description')));
$this->set(compact('adtyps'));
}
现在,我的代码中包含用户ID(表示为uID),发布日期(表示为DatePosted)以及用户的IP地址。
public function add() {
if($this->Session->read('user_id')){
if($this->request->is('post')) {
if($this->Ad->save($this->request->data)){
//saving the uID in table ads
//$this->data['Ad']['uID'] = $this->Session->read('user_id');
//$this->data['Ad']['DatePosted'] = DboSource::expression('NOW()');
//$this->data['Ad']['IP'] = $_SERVER['REMOTE_ADDR'];
$this->Ad->uID = $this->Session->read('user_id');
$this->Ad->DatePosted = DboSource::expression('NOW()');
$this->Ad->IP = $_SERVER['REMOTE_ADDR'];
$this->Ad->save($this->request->data);
$this->Session->setFlash('The post was successfully added!');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('The post was not saved. Please try again.', 'alert-message', array('class'=>'error'));
}
}
}
else{
$this->Session->setFlash('You have to login first to access control panel');
$this->redirect(array('controller'=>'users','action'=>'login'));
}
//for setting the category of advertisement
$adcats = $this->Ad->Adcat->find('list',array('fields'=>array('id','Description')));
$this->set(compact('adcats'));
//for setting the type of advertisement
$adtyps = $this->Ad->Adtyp->find('list',array('fields'=>array('id','Description')));
$this->set(compact('adtyps'));
}
现在,我的问题是如何保存我之前提到的那三个字段?
答案 0 :(得分:0)
if($this->Session->read('user_id')){
if($this->request->is('post')) {
$this->request->data('Ad.uID', $this->Session->read('user_id'));
$this->request->data('Ad.DatePosted', DboSource::expression('NOW()'));
$this->request->data('Ad.IP', $this->request->clientIp());
// or $_SERVER['REMOTE_ADDR'];
if($this->Ad->save($this->request->data)){
$this->Session->setFlash('The post was successfully added!');
$this->redirect(array('action'=>'index'));
}
// and so on
应该这样做。
CakeRequest :: data提供对请求数据的点表示访问。允许 对于读取和修改请求数据,可以链接呼叫 在一起
为了将来参考,如果你有一个名为created
datetime
的字段,其默认值为null
,那么Cake会自动神奇地插入记录创建时间 - 也许消除了需要DatePosted
。