我的 MySQL 数据库中有accounts
和money
表,所以如果有任何行增加钱,accounts.balance
应该更新,首先我通过 Active Record 和 Transaction 完成
$transaction = $connection->beginTransaction();
try {
if ($createModel->save()) {
$account = $createModel->accounts;
$account->balance += $createModel->amount;
if ($account->save())
$transaction->commit();
}
} catch(Exception $e){
$transaction->rollBack();
}
现在我将其更改为$createModel->save()
并创建触发器来更新accounts
表,
CREATE TRIGGER before_insert_money BEFORE INSERT ON money
FOR EACH ROW
BEGIN
UPDATE accounts SET balance=balance+NEW.amount WHERE id=NEW.accounts_id;
END;
触发器是否足以确保在不使用事务的情况下更新帐户表?如果您的答案是否定的,我如何结合触发器、事务和 ActiveRecord?