【问题标题】:Basic CakePHP: Unable to get the id (Edit Method)基本 CakePHP:无法获取 id(编辑方法)
【发布时间】:2015-06-18 15:01:15
【问题描述】:

这是我的数据库: http://gyazo.com/c6a86127d6f91aae947cf45ee535cecd

示例: http://gyazo.com/c23fec3fabb7e4504c42453980fbc372 当我按下编辑按钮时,他们可以检索数据,但是页面显示的是空字段,而不是包含旧数据的字段。

其次,无法取消日期,因为他们不断将空 id_field 发送回控制器。

附言添加、编辑、删除方法工作得很好。

以下是我的代码: 型号

<?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

【问题讨论】:

    标签: cakephp sql-update


    【解决方案1】:

    应该是:

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

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

    另一个说明:您正在做的很多事情都是非标准的,并且与推荐的约定不一致。清理它会让工作更容易,也更容易获得帮助。

    【讨论】:

    • 感谢帮助我解决问题。我已经阅读了“cakephp-conventions”。但是我还是有些困惑,你能帮忙说明我工作中的一些非标准约定吗?我会尽量注意这一点并做一些改变。
    • 烘焙(“蛋糕烘焙”)你的代码,这样你就可以看到它是如何完成的。
    • 烘焙(“蛋糕烘焙”)?我仍然混淆你的意思。
    • @beginnerK - 只需按照 CakePHP 在线书籍中非常深入的教程进行操作即可。这样做之后,您将对我们所指的内容有一个更好的了解。另外,如果这个答案解决了您的问题,请将其标记为答案,这样更多的人就不会浪费时间认为这是没有答案的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    相关资源
    最近更新 更多