Symfony2 购物车 [英] Shopping cart with Symfony2
问题描述
我正在 Symfony2 学习我的方法,同时为一家家庭经营的葡萄酒进口商建立一个小型电子商店.慢慢地,我开始了解 Symfony2 概念,但是在继续构建购物车的过程中,我不太确定正确(至少根据 Sf2 标准)的实施方式是什么这个.
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.
我正在寻找的是一个简单的 Basket
存储 BasketItems
和它们在会话中的数量,然后在结账时使用这些数据.现在,在我开始以某种方式将它们粘合在一起之前,我想听听有关如何正确完成和分离的建议.
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.
顺便说一句,Symfony2 是否已经有一些经过验证且有效的购物篮?
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.
一个简单的例子:
如果您想将商品的价格从美元转换为欧元,您可以使用
BasketItem::convertCurrencyTo($currencyType)
.这是在单个实体上运行的代码.
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.
如果您想查找价格在 10 美元到 20 美元之间的所有 BasketItems
,您可以使用 BasketItemRepository::findByPriceRange($min, $max)
.这是对一组实体进行操作的代码.
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,在处理具有一对多或多对多关系的实体时,事情可能会有些混乱.我会想象一个Basket
有很多BasketItems
,一个User
有一个Basket
.因此,我认为可以合理地假设,为了获取 BasketItems
一个 User
感兴趣的内容,您可以执行以下操作:$user->getBasket()->getBasketItems()
.
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()
.
请注意,此示例中没有使用 Repository,因为它是一个非常基本的查询.如果你需要做一些更具体的事情,比如找到 BasketItems
一个 User
对碰巧正在出售的感兴趣,你可以使用类似 BasketItemRepository:: 的东西findWhereOnSaleForBasket($BasketId)
.没有什么能阻止您直接向 User
或 Basket
类添加类似的方法,但我觉得因为您的查询的主要目标"是一个 BasketItem
,它属于该实体的 Repository 类.
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.
您提到实施购物车并非易事,您说得对.但是您提出的问题并不仅限于 Symfony2(在这种情况下,更多的是关于 Doctrine 2),所以我认为如果您查看其他开源购物车并研究他们在做什么可能会有所帮助.我认为 Symfony2 真正具有的唯一影响是迫使您决定如何在包中拆分代码.
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.
希望这会有所帮助!
这篇关于Symfony2 购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!