基本 CakePHP:无法获取 ID(编辑方法)
Basic CakePHP: Unable to get the id (Edit Method)
这是我的数据库:
http://gyazo.com/c6a86127d6f91aae947cf45ee535cecd
示例:
http://gyazo.com/c23fec3fabb7e4504c42453980fbc372
当我按下编辑按钮时,他们能够检索数据但是页面显示空字段而不是具有旧数据的字段。
其次,无法取消日期,因为他们一直将空 id_field 发送回控制器。
p.s。添加、编辑、删除方法完全没问题。
下面是我的代码:
型号
<?php
App::uses('AppModel', 'Model');
class EventDate extends AppModel {
public $useTable = 'eventdate';
public $primaryKey = 'eventDate_id';
public $validate = array(
'event_date' => array(
'rule' => array('date','ymd'),
'message' => 'Enter a valid date in YY-MM-DD format.',
'required' => true,
'allowEmpty' => false
)
);
}
控制器
public function edit($id = null) {
//Retrieve from database
$post = $this->EventDate->findByEventdateId($id);
$this->set('edate', $post) ;
// debug($post);
//Without Id
if (!$id) {
throw new NotFoundException(__('Invalid Event Date'));
}
//If Not Exist the id
if (!$post) {
throw new NotFoundException(__('Invalid Event Date'));
}
//After press the submit
if ($this->request->is(array('post','put'))) {
$this->EventDate->eventDate_id = $id;
debug($_POST);
if ($this->EventDate->save($this->request->data)) {
$this->Session->setFlash(__('The Event has been saved.'));
return $this->redirect(array('action' => 'index'));}
else
{$this->Session->setFlash(__('The Event could not be saved. Please, try again.'));}
}
//Set the data same as retrieved
if (!$this->request->data) { $this->request->data = $post;}
}
edit.ctp
<h2>Edit Event</h2>
<?php
echo $this->Form->create('eventdate');
//echo $this->Form->input('eventDate_id', array('type' => 'hidden'));
echo $this->Form->hidden('eventDate_id');
echo $this->Form->input('event_date',array(
'label' => 'Event Date',
'type' => 'text',
'id' => 'datepicker'));
echo $this->Form->end('Save Post');
?>
下面link是显示的调试($_Post):
http://gyazo.com/5e569e6cc6b3026fc8896c315197a938
应该是:
echo $this->Form->create('EventDate'); // notice the capital CamelCase
旁注:字段中显示的信息将过时,因为您 1) 从数据库获取数据并设置为 var,然后执行基于发布数据的保存。
另一个旁注:您正在做的很多事情都是非标准的,并且与推荐的约定不一致。清理它将使工作更容易,也更容易获得帮助。
这是我的数据库: http://gyazo.com/c6a86127d6f91aae947cf45ee535cecd
示例: http://gyazo.com/c23fec3fabb7e4504c42453980fbc372 当我按下编辑按钮时,他们能够检索数据但是页面显示空字段而不是具有旧数据的字段。
其次,无法取消日期,因为他们一直将空 id_field 发送回控制器。
p.s。添加、编辑、删除方法完全没问题。
下面是我的代码: 型号
<?php
App::uses('AppModel', 'Model');
class EventDate extends AppModel {
public $useTable = 'eventdate';
public $primaryKey = 'eventDate_id';
public $validate = array(
'event_date' => array(
'rule' => array('date','ymd'),
'message' => 'Enter a valid date in YY-MM-DD format.',
'required' => true,
'allowEmpty' => false
)
);
}
控制器
public function edit($id = null) {
//Retrieve from database
$post = $this->EventDate->findByEventdateId($id);
$this->set('edate', $post) ;
// debug($post);
//Without Id
if (!$id) {
throw new NotFoundException(__('Invalid Event Date'));
}
//If Not Exist the id
if (!$post) {
throw new NotFoundException(__('Invalid Event Date'));
}
//After press the submit
if ($this->request->is(array('post','put'))) {
$this->EventDate->eventDate_id = $id;
debug($_POST);
if ($this->EventDate->save($this->request->data)) {
$this->Session->setFlash(__('The Event has been saved.'));
return $this->redirect(array('action' => 'index'));}
else
{$this->Session->setFlash(__('The Event could not be saved. Please, try again.'));}
}
//Set the data same as retrieved
if (!$this->request->data) { $this->request->data = $post;}
}
edit.ctp
<h2>Edit Event</h2>
<?php
echo $this->Form->create('eventdate');
//echo $this->Form->input('eventDate_id', array('type' => 'hidden'));
echo $this->Form->hidden('eventDate_id');
echo $this->Form->input('event_date',array(
'label' => 'Event Date',
'type' => 'text',
'id' => 'datepicker'));
echo $this->Form->end('Save Post');
?>
下面link是显示的调试($_Post): http://gyazo.com/5e569e6cc6b3026fc8896c315197a938
应该是:
echo $this->Form->create('EventDate'); // notice the capital CamelCase
旁注:字段中显示的信息将过时,因为您 1) 从数据库获取数据并设置为 var,然后执行基于发布数据的保存。
另一个旁注:您正在做的很多事情都是非标准的,并且与推荐的约定不一致。清理它将使工作更容易,也更容易获得帮助。