beginnerK
beginnerK

Reputation: 424

Basic CakePHP: Unable to get the id (Edit Method)

This my database: http://gyazo.com/c6a86127d6f91aae947cf45ee535cecd

Example: http://gyazo.com/c23fec3fabb7e4504c42453980fbc372 When I press the edit button , they able to retrieve the data however the page show me empty field instead of the field that have old data.

Secondly, unable to undate because they keep send empty id_field back to controller.

p.s. add,edit,delete method work totally fine.

Below are my code: Model

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


}

Controller

 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');
?>

Below link is the debug($_Post) been shown: http://gyazo.com/5e569e6cc6b3026fc8896c315197a938

Upvotes: 0

Views: 66

Answers (1)

Dave
Dave

Reputation: 29131

Should be:

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

Side note: The info that displays in the fields will be out-of-date, since you 1) get the data from the DB and set to var, THEN do the save based on posted data.

Another side note: There are quite a few things that you're doing that are non-standard and not consistent w/ the recommended conventions. Cleaning that up will make it easier to work with AND easier to receive help.

Upvotes: 1

Related Questions