-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6029 from shinya/issue_6028/duplicate_stock
重複して在庫が登録されてしまう問題に対処
- Loading branch information
Showing
4 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20230928014611 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// 重複した在庫がdtb_product_stockにあるのか確認する。 | ||
$exists = $this->connection->fetchAllKeyValue(" | ||
SELECT product_class_id, product_class_id_count | ||
FROM ( | ||
SELECT | ||
product_class_id, | ||
COUNT(product_class_id) AS product_class_id_count | ||
FROM | ||
dtb_product_stock | ||
GROUP BY | ||
product_class_id | ||
) AS subquery | ||
WHERE product_class_id_count > 1; | ||
"); | ||
|
||
// 重複在庫がある場合、dtb_product_class.stockを正として、それ以外の在庫情報は削除する | ||
if (count($exists) != 0) { | ||
foreach ($exists as $pc_id => $value) { | ||
$stock = $this->connection->fetchOne("SELECT stock FROM dtb_product_class WHERE id = :id", ["id" => $pc_id]); | ||
$this->addSql("DELETE FROM dtb_product_stock WHERE product_class_id = :pc_id", ["pc_id" => $pc_id]); | ||
$this->addSql("INSERT INTO dtb_product_stock (product_class_id, creator_id, stock, create_date, update_date, discriminator_type) VALUES (:pc_id, NULL, :stock, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'productstock')", ["pc_id" => $pc_id, "stock" => $stock]); | ||
} | ||
} | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters