Why Bulk Operations?
Without bulk operations:Bulk Create
Setup
Define theMODEL constant in your model:
Product.php
Single Create (Normal)
Bulk Create
Wrap records in an array with the model key:The key must match the lowercase
MODEL constant. For Product::MODEL = 'product', use {"product": [...]}.Bulk Update
Route Setup
Register theupdateMultiple route:
routes/api.php
Request Format
Each record must include the primary key:Partial Updates
You don’t need to send all fields, only the ones you want to update:Transaction Handling
All bulk operations are wrapped in database transactions.Automatic Rollback
If any record fails, the entire transaction rolls back:Commit on Success
If all records succeed, the transaction commits:Validation in Bulk Operations
Per-Record Validation
Each record is validated individually:Validation Rules
Define validation in your model or request:ProductRequest.php
Cache Invalidation
Bulk operations invalidate cache for the entire model.How It Works
All cached
list_all and get_one queries for products are invalidated with a single version bump.Performance Optimization
Benchmark: 100 Record Updates
Improvement: 28.8x faster ✅
Best Practices
✅ Do:- Use bulk operations for batches of 10+ records
- Send only fields that need updating
- Batch similar operations together
- Use pagination for very large batches (1000+)
- Use bulk operations for 1-2 records (overhead not worth it)
- Send all fields when only one changed
- Mix creates and updates in same batch
- Send batches larger than 500 records without testing
Real-World Examples
Inventory Update (E-commerce)
Update stock levels from warehouse system:Price Update (Admin Dashboard)
Apply discount to multiple products:Status Batch Update (CRM)
Update lead status after import:Bulk Create from Import (CSV Upload)
Error Handling
Partial Success Not Supported
Rest Generic Class does not support partial success in bulk operations. Either all records succeed or all fail. Why?Partial success leads to inconsistent state and makes error recovery complex. Alternative:
If you need partial success, send smaller batches:
Logging Errors
Bulk operation errors are logged automatically:storage/logs/rest-generic-class.log.
Testing Bulk Operations
Unit Test Example
Troubleshooting
”MODEL constant not defined” Error
Symptom: Bulk create fails with “MODEL not found” Cause: MissingMODEL constant in model
Solution:
Wrong Key in Request
Symptom: Returns single-record response instead of bulk Cause: Key doesn’t match lowercaseMODEL constant
Fix:
Transaction Timeout
Symptom: Request times out with large batches Cause: Batch too large or slow validation Solution:Memory Limit Exceeded
Symptom: “Allowed memory size exhausted” Cause: Loading too many models into memory Solution:Next Steps
Caching
Understand cache invalidation with bulk operations
Many-to-Many
Bulk attach/detach operations for pivot tables
Performance
Advanced performance optimization techniques
Testing
Write integration tests for bulk endpoints
Evidence
-
File:
src/Core/Services/BaseService.php
Lines: 611-627, 664-676
Implementscreate()(detects bulk),save_array(), andupdate_multiple() -
File:
src/Core/Controllers/RestController.php
Lines: 158-176, 211-230
Showsstore()andupdateMultiple()with transaction handling -
File:
src/Core/Models/BaseModel.php
Lines: 28
DefinesMODELconstant requirement for bulk operations -
File:
README.md
Lines: 181-194
Shows bulk update example usage