提问者:小点点

编辑MySQL DB表中的数据,在CakePHP中没有主键


使用cakephp-2.6。3在Windows系统上。

我只需要编辑我在ABCData/index中列出的MySQL数据库表中的行数据。php文件。

控制器

    public function index() {
        $this->set('abcdata', $this->ABCData->find('all'));
    }

    public function edit($custom_id = null) {
        if (!$custom_id) {
            throw new NotFoundException(__('Invalid entry'));
        }

        $post = $this->ABCData->findById($custom_id);
        if (!$abcdata) {
            throw new NotFoundException(__('Invalid entry'));
        }

        if ($this->request->is(array('post', 'put'))) {
            $this->ABCData->custom_id = $custom_id;
            if ($this->ABCData->save($this->request->data)) {
                $this->Session->setFlash(__('Your ABCData has been updated.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your ABCData.'));
        }

        if (!$this->request->data) {
            $this->request->data = $abcdata;
        }
    }
}

模型

class ABCData extends AppModel {
    public $primaryKey = 'custom_id';
}

指数ctp

    <h3>ABCData list</h3>
<table>
    <tr>
        <th></th>
        <th>field_1</th>
        <th>field_2</th>
        <th>field_3</th>
        <th>...</th>
        <th>...</th>
        ...
    </tr>

    <?php foreach ($abcdata as $abc): ?>
    <tr>
        <td><?php echo $this->Html->link('Edit', array('action' => 'edit', $abc['ABCData']['custom_id']));?></td>
        <td><?php echo $abc['ABCData']['field_1']; ?></td>
        <td><?php echo $abc['ABCData']['field_2']; ?></td>
        <td><?php echo $abc['ABCData']['field_3']; ?></td>
        <td>...</td>
        <td>...</td>
        ...
    </tr>
    <?php endforeach; ?>
    <?php unset($abcdata); ?>
</table>

编辑ctp

<h1>Edit ABCata</h1>
<?php
    echo $this->Form->create('ABCata');
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '16'));
    echo $this->Form->input('custom_id', array('type' => 'hidden'));
    echo $this->Form->end('Save ABCata');
?>

我所拥有的只是一个带有外键字段的表。

这里的custom\u id仅为唯一字段。

我得到这个错误:

数据库错误

错误:SQLSTATE[42S22]:找不到列:1054未知列'ABCData.id'in'where子句'...

请帮忙。


共1个答案

匿名用户

正如评论中所说,如果你的模型中没有id,你就不能用id搜索。你应该这么做

$post = $this->ABCData->findByCustomId($custom_id);

根据蛋糕惯例,或者您可以指定条件,如

$post = $this->ABCData->find('first', array('conditions' =>
                                           array('ABCData.custom_id' => $custom_id)));