php
45 lines · 8 steps
Atomic order placement with PDO transactions
A repository writes an order, its items, and stock decrements as one all-or-nothing database transaction.
Explained by
highlit
1<?php
2
3final class OrderRepository
4{
5 public function __construct(private PDO $pdo)
6 {
7 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
8 }
9
10 public function placeOrder(int $customerId, array $lineItems): int
11 {
12 $this->pdo->beginTransaction();
13
14 try {
15 $insertOrder = $this->pdo->prepare(
16 'INSERT INTO orders (customer_id, status, created_at) VALUES (?, ?, NOW())'
17 );
18 $insertOrder->execute([$customerId, 'pending']);
19 $orderId = (int) $this->pdo->lastInsertId();
20
21 $insertItem = $this->pdo->prepare(
22 'INSERT INTO order_items (order_id, sku, quantity, unit_price) VALUES (?, ?, ?, ?)'
23 );
24 $decrementStock = $this->pdo->prepare(
25 'UPDATE products SET stock = stock - ? WHERE sku = ? AND stock >= ?'
26 );
27
28 foreach ($lineItems as $item) {
29 $insertItem->execute([$orderId, $item['sku'], $item['qty'], $item['price']]);
30 $decrementStock->execute([$item['qty'], $item['sku'], $item['qty']]);
31
32 if ($decrementStock->rowCount() === 0) {
33 throw new RuntimeException("Insufficient stock for SKU {$item['sku']}");
34 }
35 }
36
37 $this->pdo->commit();
38
39 return $orderId;
40 } catch (Throwable $e) {
41 $this->pdo->rollBack();
42 throw $e;
43 }
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Wrapping related writes in a transaction guarantees they all succeed or all roll back, leaving no half-finished orders.
- 2A conditional UPDATE plus rowCount() enforces business invariants like stock availability at the database level.
- 3Preparing a statement once and executing it per item reuses the query plan and keeps values safely bound.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/atomic-order-placement-with-pdo-transactions-explained-php-c992/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.