I'm learning my ways with Symfony2 while building a small eshop for a family-run wine importer. Slowly I'm gaining understanding of the Symfony2 concept but while moving on to building a shopping cart, I'm not quite sure what would be the correct (at least according to the Sf2 standards) way to implement this.
What I'm looking for is a simple Basket
storing BasketItems
and their amount in sessions and later use this data in the checkout. Now before I go and glue this somehow together, I'd like to hear an advice on how this should be done and separated correctly.
So far, I've created the two as entities and now I'm not sure if I should put the logic directly in the Entity classes or Repositories or where preferably?
I hope this is not a too wide question. I'm well aware of the fact that a truly robust shopping cart implementation is no small job.
On a sidenote, are there already some proven and working shopping baskets for Symfony2 around?
The general rule when creating Entities and Repositories is that an Entity class should contain the logic needed to operate on a single Entity, and Repository classes contain the logic needed to operate on groups of Entities.
A simple example:
If you wanted to convert the price of an item from dollars to euros, you would have BasketItem::convertCurrencyTo($currencyType)
. This is code that operates on a single Entity.
If you wanted to find all BasketItems
whose price is between $10 and $20, you would use BasketItemRepository::findByPriceRange($min, $max)
. This is code that operates on a group of Entities.
IMO, things can get a little confusing when dealing with Entities that have a one-to-many or many-to-many relationship. I would imagine that one Basket
has many BasketItems
, and one User
has one Basket
. Therefore, I think it's reasonable to assume that in order to fetch the BasketItems
a User
is interested in, you could do something like this:$user->getBasket()->getBasketItems()
.
Note that there is no Repository being used in this example because it's a very basic query. If you needed to do something more specific, like find the BasketItems
a User
is interested in that happen to be on sale, you could use something like BasketItemRepository::findWhereOnSaleForBasket($BasketId)
. There's nothing stopping you from adding a similar method directly to the User
or Basket
classes, but I feel that because your query's main 'target' is a BasketItem
, it belongs in that Entity's Repository class.
You mention that implementing a shopping cart is no small task, and you're right. But the questions you're asking aren't really limited to Symfony2 (in this case it's more about Doctrine 2), so I think it might be helpful if you check out other open source shopping carts and study what they're doing. I think the only impact Symfony2 would really have is forcing you to decide how to split up your code within bundles.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With