bajomodavid/product-inventory-bundle
Installation
composer require bajomodavid/product-inventory-bundle
Add to config/bundles.php:
BajomoDavid\ProductInventoryBundle\ProductInventoryBundle::class => ['all' => true],
Database Migration Run these commands in sequence:
php bin/console make:migration
php bin/console doctrine:migrations:migrate
First Use Case Import stock via CLI:
php bin/console inventory:import-stock public/stock.csv
Verify stock records in the database via doctrine:query:sql or a simple API endpoint.
Stock Management
inventory:import-stock for bulk CSV uploads. Ideal for nightly batch jobs or initial data seeding.x-action: inventory-import header to dynamically update stock. Example:
// Controller
public function updateStock(Request $request) {
$request->headers->set('x-action', 'inventory-import');
$validator = $this->get('validator');
$data = $request->request->all();
// Validate and process
}
Integration with Product Entities
Stock entity to include product relationships:
// src/Entity/Product.php
#[ORM\OneToMany(targetEntity: Stock::class, mappedBy: 'product')]
private Collection $stocks;
// src/EventListener/ProductListener.php
public function postPersist(Product $product) {
$stock = new Stock();
$stock->setProduct($product);
$stock->setBranch('default');
$stock->setQuantity(0);
$em->persist($stock);
}
Multi-Branch Inventory
branch field to segment stock by location. Example query:
$stocks = $entityManager->getRepository(Stock::class)
->findBy(['branch' => 'nyc-store']);
Validation Layer
config/packages/bajomo_product_inventory.yaml:
inventory:
validation:
min_stock: 0
max_stock: 1000
CSV Format Strictness
inventory:import-stock command expects exact column names: sku, branch, stock. Missing or misnamed columns will silently fail. Validate files pre-import:
// Custom validator for CSV
public function validateCsv(array $data): bool {
return isset($data['sku'], $data['branch'], $data['stock']);
}
Race Conditions
$entityManager->beginTransaction();
try {
$stock->setQuantity($stock->getQuantity() + $delta);
$entityManager->flush();
$entityManager->commit();
} catch (\Exception $e) {
$entityManager->rollBack();
throw $e;
}
Migration Dependencies
Stock table with columns id, sku, branch, quantity. Custom migrations may break compatibility. Check the bundle’s Migration class for schema details.ImportStockCommand:
$this->logger->info('Importing stock for SKU: ' . $sku, ['branch' => $branch]);
x-action: inventory-import is set; missing headers may route requests to default controllers.Custom Stock Events
StockUpdatedEvent to trigger actions (e.g., notifications):
// config/services.yaml
BajomoDavid\ProductInventoryBundle\EventListener\StockListener:
tags:
- { name: kernel.event_listener, event: stock.updated, method: onStockUpdated }
Override Default Behavior
Stock entity to add fields (e.g., reserved_quantity):
#[ORM\Column(type: 'integer', nullable: true)]
private ?int $reservedQuantity = null;
StockType form class to include new fields.API Security
inventory-import actions to trusted IPs or roles:
# config/packages/security.yaml
access_control:
- { path: ^/api/inventory, roles: ROLE_INVENTORY_MANAGER }
How can I help you explore Laravel packages today?