{"success":true,"name":"wrap-multi-step-php-database-writes-in-transactions","description":"PHP code performs several dependent database writes in a row","category":"ai-brimind","path":"ai-brimind/wrap-multi-step-php-database-writes-in-transactions/SKILL.md","file":"SKILL.md","content":"---\nname: wrap-multi-step-php-database-writes-in-transactions\ndescription: \"PHP code performs several dependent database writes in a row\"\n---\n\n# Wrap Multi-Step PHP Database Writes in Transactions\n\n## When to use\nProject is PHP, and you are about to run two or more INSERT/UPDATE/DELETE operations that must all succeed together.\n\n## Example request\nOrder saved, stock decrement fails → half-applied state; beginTransaction/commit/rollBack bound the unit — atomicity. Loop of 500 inserts each auto-committing → slow and interruptible; one transaction wraps the batch — batching. Framework in use → DB::transaction / Doctrine transactional already own retry and rollback — built-in mechanism. Read-then-update race → transaction plus row locking (SELECT ... FOR UPDATE) names the fix — concurrency control.\n\n## Instructions\nYou are a PHP developer who identifies the unit of work before writing any multi-step database code. First list every write the operation performs and ask: if step N fails, is the data valid with only steps 1..N-1 applied? If not, the steps form one atomic unit — wrap them with the existing mechanism: PDO beginTransaction/commit/rollBack in a try/catch that rethrows after rollback, or the framework's transaction closure (which handles rollback for you) when available. Ensure the table engine supports transactions (InnoDB, not MyISAM) rather than assuming. Keep transactions short: no external API calls, mails, or user waits inside them, and name where concurrent access could race (read-modify-write) so locking or atomic UPDATE expressions handle it. If failure mid-way is genuinely acceptable, state that explicitly instead of leaving it implicit. Only write the code once the atomic boundary is confirmed, and explain why the database can never be observed in the half-done state.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #952\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}