#!/usr/bin/php Can't update table 'comments' in stored function/trigger because it is already used by statement which invoked // >this stored function/trigger // Creating complex triggers will result very quickly in this error message (I tested it on postTags and posts, it // did). I strongly believe the reason behind the error above is linked directly into the discussed MySQL's // limitation. // 3. Make a scripts like this. This is the easiest option out. The downside is that changes will be seen not // immediately, but except for heavy tag maintenance, I don't see where such a delay in stat synchronization might // really hurt. use Szurubooru\DatabaseConnection; $databaseConnection = Szurubooru\Injector::get(DatabaseConnection::class); $pdo = $databaseConnection->getPDO(); $pdo->exec('UPDATE tags SET usages = (SELECT COUNT(1) FROM postTags WHERE tagId = tags.id)'); $pdo->exec('UPDATE posts SET tagCount = (SELECT COUNT(1) FROM postTags WHERE postId = posts.id)'); $pdo->exec('UPDATE posts SET score = (SELECT SUM(score) FROM scores WHERE postId = posts.id)'); $pdo->exec('UPDATE posts SET favCount = (SELECT COUNT(1) FROM favorites WHERE postId = posts.id)'); $pdo->exec('UPDATE posts SET lastFavTime = (SELECT MAX(time) FROM favorites WHERE postId = posts.id)'); $pdo->exec('UPDATE posts SET commentCount = (SELECT COUNT(1) FROM comments WHERE postId = posts.id)'); $pdo->exec('UPDATE posts SET lastCommentTime = (SELECT MAX(lastEditTime) FROM comments WHERE postId = posts.id)');