fitness-web/console/migrations/m181205_050104_create_trigger_event_registration.php
2021-09-27 20:40:18 +02:00

42 lines
1.2 KiB
PHP

<?php
use yii\db\Schema;
use yii\db\Migration;
class m181205_050104_create_trigger_event_registration extends Migration
{
public function up()
{
$this->addColumn('{{%event}}',"seat_count",$this->integer());
$this->execute("CREATE TRIGGER event_seat_count_check
AFTER INSERT ON event_registration FOR EACH ROW
BEGIN
IF (SELECT coalesce(COUNT(id),0) FROM event_registration
WHERE canceled_at is null AND id_event = NEW.id_event)
>
(SELECT coalesce(seat_count,0) from event where id_event = NEW.id_event)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'MAX_SEAT_COUNT_EXCEEDED';
END IF;
END;
");
}
public function down()
{
$this->execute("DROP TRIGGER IF EXISTS event_seat_count_check;");
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}