基本CakePHP:无法获取id(编辑方法)

时间:2015-06-18 15:01:16

标签: cakephp sql-update

我的数据库: 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');
?>

以下链接是调试($ _ Post)显示: http://gyazo.com/5e569e6cc6b3026fc8896c315197a938

1 个答案:

答案 0 :(得分:1)

应该是:

echo $this->Form->create('EventDate');  // notice the capital CamelCase

旁注:字段中显示的信息将过时,因为您1)从数据库获取数据并设置为var,然后根据已发布进行保存数据

另一方面注意事项:您正在做的很多事情都是非标准的,并且与推荐的约定不一致。清理它将使其更容易使用并且更容易获得帮助。