N
N
Nikita Ronzin2017-04-11 17:04:27
MySQL
Nikita Ronzin, 2017-04-11 17:04:27

How to write trigger body in yii1 migration?

A trigger has been created in the database, you need to transfer it to the project. I created the migration file in the project. in up I can't figure out how to spell it correctly

BEGIN 
DECLARE cnt INT; 
SELECT COUNT(*) FROM phone WHERE FIO_ID = OLD. FIO_ID INTO cnt; 
IF cnt = 1 THEN 
SIGNAL SQLSTATE '45000' 
SET MESSAGE_TEXT = 'Отмена удаления'; 
END IF; 
END

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Ronzin, 2017-04-11
@Ababinili

$this->execute("
CREATE TRIGGER last_number_fio
BEFORE DELETE ON phone
FOR EACH ROW
BEGIN
DECLARE cnt INT;
SELECT COUNT(*) FROM phone WHERE FIO_ID = OLD. FIO_ID INTO cnt;
IF cnt = 1 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cancel deletion';
END IF;
END");
in short, here is the final version of the trigger in the migration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question