Inventory control for Symfony-based online store

Hey everyone! I’m building an online shop using Symfony and I’m stuck on how to manage product inventory. I want to make sure customers can’t add items to their cart when they’re out of stock. Also, I’d like the stock to decrease by 1 each time someone adds a product to their cart. Here’s what I’ve got so far:

// BasketService
public function addItem(int $itemId)
{
    $basket = $this->session->get('basket', []);
    if (isset($basket[$itemId])) {
        $basket[$itemId]++;
    } else {
        $basket[$itemId] = 1;
    }
    $this->session->set('basket', $basket);
}

// BasketController
public function addItem($itemId, Request $request)
{
    $item = $this->itemRepository->find($itemId);
    if (!$item) {
        throw $this->createNotFoundException("Item $itemId not found");
    }
    $this->basketService->addItem($itemId);
    // ... more code for redirects and flash messages
}

// Item entity
private $inventory;

public function getInventory(): ?int
{
    return $this->inventory;
}

public function setInventory(int $inventory): self
{
    $this->inventory = $inventory;
    return $this;
}

Any ideas on how to implement the stock management? Thanks in advance!

hey, try checking inventory b4 adding item to cart and subtract one only if available. wrap this in a transaction to avoid race condishuns. reserving item for checkout may help too. hope it helps!

I’ve tackled a similar issue in my Symfony projects. One approach that worked well was implementing a service to handle inventory checks and updates. Here’s a basic outline:

class InventoryService
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function checkAndUpdateInventory(Item $item): bool
    {
        if ($item->getInventory() > 0) {
            $item->setInventory($item->getInventory() - 1);
            $this->entityManager->flush();
            return true;
        }
        return false;
    }
}

Then, in your BasketController, you could use this service before adding the item to the basket. This keeps your inventory logic separate and reusable. Remember to handle potential exceptions and consider implementing a database lock for high-traffic scenarios to prevent overselling.

Hey RollingThunder! That’s an awesome project you’ve got going there. I’m curious, have you considered using Doctrine’s ORM for handling the inventory updates? It could make things a bit smoother.

Maybe something like this could work:

public function addItem(int $itemId)
{
    $entityManager = $this->doctrine->getManager();
    $item = $entityManager->getRepository(Item::class)->find($itemId);

    if (!$item || $item->getInventory() <= 0) {
        throw new \Exception('Item not available');
    }

    $item->setInventory($item->getInventory() - 1);
    $entityManager->flush();

    // Add to basket logic here
}

What do you think about this approach? Have you run into any specific challenges with race conditions or performance? I’d love to hear more about how your project is coming along!