mysql yii2 sql迁移覆盖新更改的记录

wn9m85ua  于 2023-05-21  发布在  Mysql
关注(0)|答案(1)|浏览(153)

我正在尝试创建一个迁移,该迁移将更改表中所需的记录

$sql = '';

$sql .= "UPDATE card_series SET Code = 903 WHERE ID = 1600;";

$cardsSeries1600 = CardList::find()
            ->where(['seriesId' => 1600])
            ->orderBy('ID')
            ->all();
        $cardNum = 2;

        foreach ($cardsSeries1600 as $card) {
                $number = str_pad($cardNum, 8, "0", STR_PAD_LEFT);
                $cardId = (1903).$number; 
                $sql .= "UPDATE card_list SET ID = ".$cardId.", Code = '903', Number = '".$number."' WHERE ID = ".$card->ID.";";

                $cardNum++;
        }
        $this->execute($sql);

但是最后,我的迁移以这样一种方式工作,一开始我所有必要的记录都发生了变化,但随后它们立即更改为更新的记录,这里是一个示例
UPDATE card_series SET Code = 903 WHERE ID = 1600; UPDATE card_list SET ID = 19030000002,Code = '903',Number = ' 0000002 ' WHERE ID = 1111000005; UPDATE card_list SET ID = 19030000003,Code = '903',Number = ' 0000003 ' WHERE ID = 1111000011; UPDATE card_list SET ID = 19030000004,Code = '903',Number = ' 0000004 ' WHERE ID = 190300000002; UPDATE card_list SET ID = 19030000005,Code = '903',Number = ' 0000005 ' WHERE ID = 190300000003;
如何解决这个问题?

mklgxw1f

mklgxw1f1#

可以试试下面的吗?

$sql = '';
$sql .= "UPDATE card_series SET Code = 903 WHERE ID = 1600;";
$this->execute($sql);
$cardsSeries1600 = CardList::find()->where(['seriesId' => 1600])->orderBy('ID')
    ->all();
$cardNum = 2;
$sql = '';
foreach ($cardsSeries1600 as $card) {
        $number = str_pad($cardNum, 8, "0", STR_PAD_LEFT);
        $cardId = (1903).$number; 
        $sql .= "UPDATE card_list SET ID = ".$cardId.", Code = '903', Number = '".$number."' WHERE ID = ".$card->ID.";";
        $cardNum++;
}
$this->execute($sql);

相关问题