2

I used bake to make cms of the settings table.

It contains three fields.

Column Type Null Default id int(11) No
key varchar(10) No
value varchar(200) No

And it have 3 records.

All create functionality is working fine. But delete and edit would only edit/delete the first record.

For getting link...

I used the following code in view file.


 foreach ($languages as $language){
     echo $this->Html->link(__('Edit'), array('action' => 'edit', $language['Language']['id'])); ?>
     echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $language['Language']['id']), null, __('Are you sure you want to delete # %s?', $language['Language']['id']));
 }

I assigned following value to languages variable from controller.


 $this->Language->recursive = 0;
 $this->set('languages', $this->paginate());

Schema:

CREATE TABLE IF NOT EXISTS languages (
    id int(11) NOT NULL AUTO_INCREMENT,
    title varchar(30) NOT NULL,
    slug enum('eng','rus') NOT NULL DEFAULT 'eng',
    symbol varchar(50) NOT NULL,
    status enum('A','I','D') NOT NULL DEFAULT 'A',
    created_dt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified_dt datetime NOT NULL, PRIMARY KEY (id),
    UNIQUE KEY Unique Language code (slug),
    KEY status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
joshua.paling
  • 13,762
  • 4
  • 45
  • 60
Parag Kuhikar
  • 485
  • 2
  • 6
  • 17
  • 1
    I do not think your schema is valid. Primary cannot be default null. You might be missing "auto increment" and "unique" on "id" here. – mark Sep 03 '13 at 19:26
  • Thanks for your reply... Please check the table. CREATE TABLE IF NOT EXISTS `languages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(30) NOT NULL, `slug` enum('eng','rus') NOT NULL DEFAULT 'eng', `symbol` varchar(50) NOT NULL, `status` enum('A','I','D') NOT NULL DEFAULT 'A', `created_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified_dt` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `Unique Language code` (`slug`), KEY `status` (`status`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ; – Parag Kuhikar Sep 03 '13 at 22:02
  • No. This table has proper auto increment id as well as proper default value. – Parag Kuhikar Sep 03 '13 at 22:03
  • Please can you post what $languages contains,means print $languages array here – Er.KT Sep 04 '13 at 05:37
  • Array ([0] => Array ([Language] => Array ([id] => 5 [title] => Russia [slug] => rus [symbol] => Russia [status] => A [created_dt] => 2013-08-28 10:49:00 [modified_dt] => 2013-08-28 10:49:00 )) [1] => Array([Language] => Array([id] => 6 [title] => asdasdasd [slug] => eng [symbol] => asdsadasd [status] => A [created_dt] => 2013-08-28 10:54:00 [modified_dt] => 2013-08-28 10:54:00 ) ) ) – Parag Kuhikar Sep 06 '13 at 03:16
  • This issue is resolved. There is not any bug in the code. I was using Foundation 4 for listing and on clicking image it will popup the edit and delete link. But in which, i forgot to make dynamic id for that. So when i click on the image, it will always popup the first id. So when i was using the dynamic popup id the it will resolve problem. Again thanks for your reply. – Parag Kuhikar Sep 16 '13 at 05:51

1 Answers1

3

in your controller do the following

public function edit($id = null) {
    if (!$id) {
          throw new NotFoundException(__('Invalid Edit Id'));
    }
    $language = $this->Language->find('first', array(
          'conditions' => array(
                  'Language.id' => $id,
           ),
    ));

    if (empty($language)) {
           throw new BadRequestException(__('Invalid Data'));
    }

    if ($this->request->is('post') || $this->request->is('put')) {
         if ($this->Language->save($this->request->data()) {
            $this->Session->setFlash(__('saved'));
         } else {
            $this->Session->setFlash(__('something went wrong'));
         }
    }

    if (empty($this->request->data)) {
        $this->request->data = $language;
    }
}

public function delete($id = null) {
 $this->Language->id = $id;
 if (!$this->Language->exists()) {
     throw new NotFoundException(__('Invalid Language'));
 }
 $this->request->onlyAllow('post', 'delete');
 if ($this->Language->delete()) {
        $this->Session->setFlash(__('Language deleted'));
        $this->redirect(array('action' => 'index'));
 }
 $this->Session->setFlash(__('Language was not deleted'));
 $this->redirect(array('action' => 'index'));
}
Chris Pierce
  • 706
  • 5
  • 9