Execute all SQL queries in yii2 migration
P粉505450505
P粉505450505 2023-09-06 18:08:57
0
1
593

I'm trying to do a migration that will execute multiple queries on different tables at the same time

$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();

But in the end, I only have a first request UPDATE card_series SET Code = 903 WHERE ID = 1600;

I tried printing the $sql variable and all the requests were there but only the first request was executed

What can I do to make all requests execute one after another?

P粉505450505
P粉505450505

reply all(1)
P粉605233764

You can try the following methods.

public function safeUp()
{
    $query = "你的查询语句";
    $this->execute($query);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template