1

我正在尝试遍历模型中的记录并更新它们的序列号。根据 tempArray,所有值都应该按顺序排列,但更新部分没有发生。在模型中使用读取/设置/保存是否有限制,或者我缺少什么?

    $tempArray['highest serial'] = $highest['Paragraph']['serial_number'];
    for( $i=$paragraph['Paragraph']['serial_number']; $i<$highest['Paragraph']['serial_number']; $i++ ){
        $current = $this->find('first', array(
            'fields' => array(
                'Paragraph.id',
                'Paragraph.serial_number'
            ),
            'conditions' => array(
                'Paragraph.webpage_id' => $paragraph['Paragraph']['webpage_id'],
                'Paragraph.list_element' => $paragraph['Paragraph']['list_element'],
                'Paragraph.serial_number' => ($i + 1)
            )
        ));
        $tempArray['i ' . $i . ' id'] = $current['Paragraph']['id'];
        $tempArray['i ' . $i . ' serial'] = $current['Paragraph']['serial_number'];
        $this->create($current);
        // $this->read('serial_number', $current['Paragraph']['id']);
        // $this->id = $current['Paragraph']['id'];
        $this->set('serial_number', $i);
        $this->save();
    }
    return $tempArray;

虽然 tempArray 的输出是( [highest serial] => 2 [i 1 id] => 7 [i 1 serial] => 2 )记录的序列号,id=7但未更新。

救命!

编辑:按照建议将 read() 更改为 create()。没有发生 SQL 或 PHP 错误,并且 tempArray 值正确,但没有更新到数据库记录。任何帮助将不胜感激。

编辑2:它正在工作!最后..由于某种原因,我只能使用 saveField() 让它工作

    for( $i=$paragraph['Paragraph']['serial_number']; $i<getHighestSerial($paragraph); $i++ ){
        $current = $this->find('first', array(
            'fields' => array(
                'Paragraph.id',
                'Paragraph.serial_number'
            ),
            'conditions' => array(
                'Paragraph.webpage_id' => $paragraph['Paragraph']['webpage_id'],
                'Paragraph.list_element' => $paragraph['Paragraph']['list_element'],
                'Paragraph.serial_number' => ($i + 1)
            )
        ));         
        $this->id = $current['Paragraph']['id'];
        $this->saveField('serial_number', $i);
    }
4

1 回答 1

1

根据CakePHP 书

在循环中调用 save 时,不要忘记调用create()

于 2013-11-02T14:37:59.547 回答