更新记录category_id = 0

时间:2013-11-07 11:33:01

标签: php cakephp

我将category_id设置为0,类别ID为自动增量。所以我在cakephp中更新了这条记录,然后cakephp会插入这条记录更新此记录。

$this->Category->id=$id;
$this->Category->save($this->data);

请帮助我......

3 个答案:

答案 0 :(得分:2)

Id为0无效

CakePHP有一个implicit assumption所有标识符都是真的:

/**
 * Returns the current record's ID
 *
 * @param integer $list Index on which the composed ID is located
 * @return mixed The ID of the current record, false if no ID
 */
public function getID($list = 0) {
    if (empty($this->id) || ...) {
        return false;
    }
    ...

因此尝试以id为0进行保存将无法工作,导致Cake尝试插入,即使存在具有该falsey id的行。

答案 1 :(得分:1)

不确定我是否理解正确,但是如果你想更新所有记录WHERE category_id = 0那么这段代码会很有效:

$this->Category->updateAll(
    $this->data,
    array('Category.category_id' => 0)
);

答案 2 :(得分:1)

如果id大于0,Cakephp将更新记录,否则它会插入新记录。因此,尝试将id 0更改为大于0。 试试这个。