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 namespace App\Listeners;
Generating responsive images in a Laravel listener
queued jobs
event listeners
image processing
Intermediate
7 steps
rust
use axum::{ extract::{FromRef, FromRequestParts}, http::{header, request::Parts, StatusCode}, RequestPartsExt,
How a JWT extractor works in Axum
jwt
authentication
extractors
Intermediate
8 steps
ruby
class Admin::AccountsController < Admin::BaseController before_action :set_account, only: :destroy def destroy
A guarded destroy action in Rails
controllers
callbacks
transactions
Intermediate
8 steps
php
final class InvoiceCalculator { private const SCALE = 4;
Precise money math with PHP's BCMath
bcmath
arbitrary precision
money
Intermediate
8 steps
php
<?php namespace App\Jobs;
Debouncing a Laravel shipping-rate job
queues
debouncing
atomic-locks
Advanced
9 steps
rust
#[derive(Deserialize)] pub struct CreateArticle { title: String, body: String,
Building a create endpoint in Axum
extractors
json-deserialization
sqlx
Intermediate
7 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.