php 执行迁移中的所有SQL查询yii2

zynd9foi  于 2023-05-21  发布在  PHP
关注(0)|答案(1)|浏览(128)

我正在尝试进行一次在不同表上执行许多查询的迁移

$sql = '';

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

$cardsSeries1600 = CardList::find()
            ->where(['seriesId' => 1600])
            ->orderBy('ID');
        $cardNum = 2;
        foreach ($cardsSeries1600->each() as $card) {
               if ($card->client_id != NULL) {
                    $sql .= "DELETE FROM client_cards WHERE card_id = ".$card->ID.";";
                }
            $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.";";

            $sql .= "UPDATE card_list_log SET card_id = '".$cardId."', Code = 903, Number = '".$number."' WHERE card_id = ".$card->ID.";";

            $cardNum++;
}

\Yii::$app->db->createCommand($sql)->execute();

但最终,我只有一个第一个请求UPDATE card_series SET Code = 903 WHERE ID = 1600;
我尝试转储$sql变量,所有请求都在那里,但只有第一个请求仍在执行
我怎样才能使我所有的请求都被一个接一个地执行呢??

68bkxrlz

68bkxrlz1#

你可以像下面这样试试。

public function safeUp()
{
    $query = "your query statement";
    $this->execute($query);
}

相关问题