2

我用烘焙来制作设置表的厘米。

它包含三个字段。

列类型 Null 默认 id int(11) 无
键 varchar(10) 无
值 varchar(200) 无

它有3条记录。

所有创建功能都运行良好。但是删除和编辑只会编辑/删除第一条记录。

为了获取链接...

我在视图文件中使用了以下代码。

 foreach ($languages 作为 $language){
     echo $this->Html->link(__('Edit'), array('action' => 'edit', $language['Language']['id'])); ?>
     echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $language['Language']['id']), null, __('你确定吗你想删除 # %s?', $language['Language']['id']));
 }

我将以下值分配给控制器中的语言变量。

 $this->语言->递归 = 0;
 $this->set('语言', $this->paginate());

架构:

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 ;
4

1 回答 1

3

在您的控制器中执行以下操作

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'));
}
于 2013-11-13T00:52:05.080 回答