购物车整体所需的Symfony2深度帮助
Symfony2 in-depth help needed for shopping cart as a whole
首先我想说这个购物车不是我创建的,但是它按照我需要的方式进行了更改并且我了解大部分代码及其工作原理以及所有功能和方法都是我的.不懂的地方希望和大家一起学习。这将是一个漫长而深入的 post,但我希望您能像我向您学习一样有兴趣教我。
背景:
我有一个电子商务项目,我需要为要存储的产品创建一个购物车。过程是这样的:indexpage(包含所有产品)->detailspage(所选产品的详细信息product)->summary(包含所选产品的购物车)。
题目:
我在我的代码中评论了一些问题,这些问题只是关于我没有创建的购物车的简单问题。我希望你能帮助我理解它们。
问题:
代码有一些问题,但一旦解决了最大的问题,我可能就能解决所有问题:
当我将我的第一个产品添加到购物车时,它就像一个魅力,我能够显示价格、折扣和我需要的一切,我什至可以删除它。添加第二个产品后,循环加倍。这意味着我得到的不是 Product1、Product2,而是 Product1、Product2、Product1、Product2。 3产品等也是如此。这是最大的问题。还有一件奇怪的事。在购物车和产品上进行转储(稍后您将了解它们)我按照预期的方式在数组中只获得了 2 个值。这意味着问题出在树枝的某个地方?
现在有了代码。这是我的索引页的控制器。在这里,我简单地获取所有产品并使用会话获取我的购物车:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll(); // I get the products
$session = $this->getRequest()->getSession();
if (!$session->get('cart')) {
$session->set('cart', array()); // QUESTION#1 : I am checking if the cart is empty? If ith empty I am making it empty again?
}
return $this->render('MpShopBundle:Frontend:index.html.twig', array(
'products'=>$products
));
}
第二个控制器用于我的产品详细信息页面。这是有趣的开始:
public function viewAction($id)
{
// fetch the cart
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('MpShopBundle:Product')->find($id); // I am getting the product based on the id.
$session = $this->getRequest()->getSession();
$cart = $session->get('cart', array()); // I am getting the cart
// check if the $id already exists in it.
/**if ( $product == NULL ) { // QUESTION#2: I am checking if the
return $this->redirect($this->generateUrl('cart'));
}
else { **/
if( isset($cart[$id]) ) { // Checking if the product is already in the cart. If so, increase the product by 1
$qtyAvailable = $product->getStock(); // I am getting the quantity of that prouct
if( $qtyAvailable >= $cart[$id] = $cart[$id] + 1 ) {
$cart[$id] = $cart[$id] + 1;;
} else {
return $this->redirect($this->generateUrl('cart'));
}
}
/**else {
// if it doesnt make it 1
$cart = $session->get('cart', array()); // IF not just
$cart[$id] = 1;
}
**/
$session->set('cart', $cart);
return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
'product'=>$product
));
}
最终控制者是所有选定产品的摘要:
public function summaryAction()
{
$session = $this->getRequest()->getSession();
$cart = $session->get('cart', array());
// fetch the information using query and ids in the cart
if( $cart != '' ) {
$em = $this->getDoctrine()->getEntityManager();
foreach( $cart as $id => $quantity ) { //QUESTION#3 For each product in the cart I am setting a new key and a value?
// and then im creating a new array where I store the products ids?
$productIds[] = $id;
}
if( isset( $productIds ) ) // QUESTION#4 What am I checking here?
{
$em = $this->getDoctrine()->getEntityManager();
$product = $em->getRepository('MpShopBundle:Product')->findById( $productIds ); // I am getting all of the products in the cart by their id?
} else {
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'empty' => true,
));
} //QUESTION#5 What is going on with these renders?
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'product' => $product,
));
} else {
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'empty' => true,
));
}
}
最后,这就是我尝试在我的树枝中显示购物车的方式:
更新
我发现物品翻倍有问题,但我不知道如何解决。所以问题出在我的树枝上。如果我删除循环 {{ for key in cart }}
,它会修复加倍,所以这个循环很糟糕。但是我需要那个循环才能从购物车中删除商品...也许还有其他方法??:
{% if product is defined %}
{% for key in cart %} /// this loop causes the problem.
{% for info in product %}
<tr>
<td> <img width="60" src="" alt=""/></td>
<td>{{ info.model }}</td>
<td>
<div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text">
<button class="btn" type="button"><i class="icon-minus"></i></button>
<button class="btn" type="button"><i class="icon-plus"></i></button>
<button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>
</div>
</td>
<td>{{ info.price }}</td>
<td>{{ info.discount }}</td>
<td>{{ info.value }}</td>
<td>{{ info.getFinal }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endif %}
购物车转储:
array:3 [▼
12 => 3
10 => 1
11 => 1
]
产品转储:
array:3 [▼
0 => Product {#668 ▼
-id: 10
-model: "Test1"
}
1 => Product {#757 ▼
-id: 11
-model: "Test2"
}
2 => Product {#858 ▼
-id: 12
-model: "Test3"
}
]
我想就您代码中的某些方面提出我的建议/意见。这可能会帮助您进一步调查问题的原因(重复),并希望扩大您对 Symfony 2 的理解水平。
问题 1 :
是的,您正在通过调用 $session->set('cart', array())
将会话变量 cart
设置为空。 (实际上它会从会话存储中删除 cart
会话变量)
问题 2 :
是的,您正在检查实体 $product
return 是否有任何结果。
if ( $product == NULL ) {
。 (我会使用 === 而不是 ==)。你也可以简单地这样做 if (!$product) {
并且它是有效的。
不是问题: 我会在你的函数 summaryAction()
.
中用 if($session->has('cart') && count($session->get('cart')) > 0 ) {
改变这个 if( $cart != '' ) {
只是根据经验提出的建议。如果您正在执行这样的操作 $cart[$id] = $cart[$id] + 1
,请不要使用 Product
table 的 id
填充 $cart
数组。因为当你访问 Product
table 中填入 $cart
数组的产品时,你会遇到 Product not found 等意外情况。仅仅是因为您在代码中将 id 增加 1,而 table 中可能不存在该值。 (希望我在这里没有遗漏任何东西。我这样假设的原因是您一直在使用相同的 $id
从 Product
实体访问记录以及填充 cart
会话变量)
问题3:这种情况和我上面的解释完全吻合。在这里,您正在遍历 $cart
数组,假设 $cart
数组中的值反映了 product
table 的 id,但可能不是。否则,迭代没有问题。
foreach( $cart as $id => $quantity ) { //QUESTION#3 For each product in the cart I am setting a new key and a value?
// and then im creating a new array where I store the products ids?
$productIds[] = $id;
}
问题 4 : 在这里,if( isset( $productIds ) )
您检查 $productIds
变量是否已设置且不为 NULL。由于仅当 cart
会话变量中存储了 id 时,您才设置 $productIds
变量(这是一个数组),因此如果cart
会话变量为空。 (http://php.net/manual/en/function.isset.php)
问题5:这里,
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'product' => $product,
));
您所做的是 return 响应的一个分支,因此它可以呈现分支文件中的内容。 product
是 twig 文件中的 twig 变量 (MpShopBundle:Frontend:product_summary.html.twig
)。因此,在这种情况下,它使用它来遍历产品,正如您现在可能已经理解的那样。
我希望这至少能在一定程度上帮助到你。如果有其他 Symfony 2 开发人员可以在必要时进一步增强我的回复,我更愿意。非常感谢。干杯!!!
更新: 我想我在这里发现了问题。在树枝中,您有一个嵌套的 for 循环。首先,它会遍历 cart
会话中的所有项目,并且对于每次迭代(购物车中的项目),它会迭代 product
中的内容,这将导致重复。因此,在执行操作之前,您可以做的是像这样的简单检查。
{% for key in cart %}
{% for info in product %}
{% if key == info.id %}
// do your bits here
{% endif %}
{% endfor %}
{% endfor %}
希望这对您有所帮助。干杯!
首先我想说这个购物车不是我创建的,但是它按照我需要的方式进行了更改并且我了解大部分代码及其工作原理以及所有功能和方法都是我的.不懂的地方希望和大家一起学习。这将是一个漫长而深入的 post,但我希望您能像我向您学习一样有兴趣教我。
背景:
我有一个电子商务项目,我需要为要存储的产品创建一个购物车。过程是这样的:indexpage(包含所有产品)->detailspage(所选产品的详细信息product)->summary(包含所选产品的购物车)。
题目:
我在我的代码中评论了一些问题,这些问题只是关于我没有创建的购物车的简单问题。我希望你能帮助我理解它们。
问题:
代码有一些问题,但一旦解决了最大的问题,我可能就能解决所有问题:
当我将我的第一个产品添加到购物车时,它就像一个魅力,我能够显示价格、折扣和我需要的一切,我什至可以删除它。添加第二个产品后,循环加倍。这意味着我得到的不是 Product1、Product2,而是 Product1、Product2、Product1、Product2。 3产品等也是如此。这是最大的问题。还有一件奇怪的事。在购物车和产品上进行转储(稍后您将了解它们)我按照预期的方式在数组中只获得了 2 个值。这意味着问题出在树枝的某个地方?
现在有了代码。这是我的索引页的控制器。在这里,我简单地获取所有产品并使用会话获取我的购物车:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll(); // I get the products
$session = $this->getRequest()->getSession();
if (!$session->get('cart')) {
$session->set('cart', array()); // QUESTION#1 : I am checking if the cart is empty? If ith empty I am making it empty again?
}
return $this->render('MpShopBundle:Frontend:index.html.twig', array(
'products'=>$products
));
}
第二个控制器用于我的产品详细信息页面。这是有趣的开始:
public function viewAction($id)
{
// fetch the cart
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('MpShopBundle:Product')->find($id); // I am getting the product based on the id.
$session = $this->getRequest()->getSession();
$cart = $session->get('cart', array()); // I am getting the cart
// check if the $id already exists in it.
/**if ( $product == NULL ) { // QUESTION#2: I am checking if the
return $this->redirect($this->generateUrl('cart'));
}
else { **/
if( isset($cart[$id]) ) { // Checking if the product is already in the cart. If so, increase the product by 1
$qtyAvailable = $product->getStock(); // I am getting the quantity of that prouct
if( $qtyAvailable >= $cart[$id] = $cart[$id] + 1 ) {
$cart[$id] = $cart[$id] + 1;;
} else {
return $this->redirect($this->generateUrl('cart'));
}
}
/**else {
// if it doesnt make it 1
$cart = $session->get('cart', array()); // IF not just
$cart[$id] = 1;
}
**/
$session->set('cart', $cart);
return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
'product'=>$product
));
}
最终控制者是所有选定产品的摘要:
public function summaryAction()
{
$session = $this->getRequest()->getSession();
$cart = $session->get('cart', array());
// fetch the information using query and ids in the cart
if( $cart != '' ) {
$em = $this->getDoctrine()->getEntityManager();
foreach( $cart as $id => $quantity ) { //QUESTION#3 For each product in the cart I am setting a new key and a value?
// and then im creating a new array where I store the products ids?
$productIds[] = $id;
}
if( isset( $productIds ) ) // QUESTION#4 What am I checking here?
{
$em = $this->getDoctrine()->getEntityManager();
$product = $em->getRepository('MpShopBundle:Product')->findById( $productIds ); // I am getting all of the products in the cart by their id?
} else {
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'empty' => true,
));
} //QUESTION#5 What is going on with these renders?
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'product' => $product,
));
} else {
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'empty' => true,
));
}
}
最后,这就是我尝试在我的树枝中显示购物车的方式:
更新
我发现物品翻倍有问题,但我不知道如何解决。所以问题出在我的树枝上。如果我删除循环 {{ for key in cart }}
,它会修复加倍,所以这个循环很糟糕。但是我需要那个循环才能从购物车中删除商品...也许还有其他方法??:
{% if product is defined %}
{% for key in cart %} /// this loop causes the problem.
{% for info in product %}
<tr>
<td> <img width="60" src="" alt=""/></td>
<td>{{ info.model }}</td>
<td>
<div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text">
<button class="btn" type="button"><i class="icon-minus"></i></button>
<button class="btn" type="button"><i class="icon-plus"></i></button>
<button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>
</div>
</td>
<td>{{ info.price }}</td>
<td>{{ info.discount }}</td>
<td>{{ info.value }}</td>
<td>{{ info.getFinal }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endif %}
购物车转储:
array:3 [▼
12 => 3
10 => 1
11 => 1
]
产品转储:
array:3 [▼
0 => Product {#668 ▼
-id: 10
-model: "Test1"
}
1 => Product {#757 ▼
-id: 11
-model: "Test2"
}
2 => Product {#858 ▼
-id: 12
-model: "Test3"
}
]
我想就您代码中的某些方面提出我的建议/意见。这可能会帮助您进一步调查问题的原因(重复),并希望扩大您对 Symfony 2 的理解水平。
问题 1 :
是的,您正在通过调用 $session->set('cart', array())
将会话变量 cart
设置为空。 (实际上它会从会话存储中删除 cart
会话变量)
问题 2 :
是的,您正在检查实体 $product
return 是否有任何结果。
if ( $product == NULL ) {
。 (我会使用 === 而不是 ==)。你也可以简单地这样做 if (!$product) {
并且它是有效的。
不是问题: 我会在你的函数 summaryAction()
.
if($session->has('cart') && count($session->get('cart')) > 0 ) {
改变这个 if( $cart != '' ) {
只是根据经验提出的建议。如果您正在执行这样的操作 $cart[$id] = $cart[$id] + 1
,请不要使用 Product
table 的 id
填充 $cart
数组。因为当你访问 Product
table 中填入 $cart
数组的产品时,你会遇到 Product not found 等意外情况。仅仅是因为您在代码中将 id 增加 1,而 table 中可能不存在该值。 (希望我在这里没有遗漏任何东西。我这样假设的原因是您一直在使用相同的 $id
从 Product
实体访问记录以及填充 cart
会话变量)
问题3:这种情况和我上面的解释完全吻合。在这里,您正在遍历 $cart
数组,假设 $cart
数组中的值反映了 product
table 的 id,但可能不是。否则,迭代没有问题。
foreach( $cart as $id => $quantity ) { //QUESTION#3 For each product in the cart I am setting a new key and a value?
// and then im creating a new array where I store the products ids?
$productIds[] = $id;
}
问题 4 : 在这里,if( isset( $productIds ) )
您检查 $productIds
变量是否已设置且不为 NULL。由于仅当 cart
会话变量中存储了 id 时,您才设置 $productIds
变量(这是一个数组),因此如果cart
会话变量为空。 (http://php.net/manual/en/function.isset.php)
问题5:这里,
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'product' => $product,
));
您所做的是 return 响应的一个分支,因此它可以呈现分支文件中的内容。 product
是 twig 文件中的 twig 变量 (MpShopBundle:Frontend:product_summary.html.twig
)。因此,在这种情况下,它使用它来遍历产品,正如您现在可能已经理解的那样。
我希望这至少能在一定程度上帮助到你。如果有其他 Symfony 2 开发人员可以在必要时进一步增强我的回复,我更愿意。非常感谢。干杯!!!
更新: 我想我在这里发现了问题。在树枝中,您有一个嵌套的 for 循环。首先,它会遍历 cart
会话中的所有项目,并且对于每次迭代(购物车中的项目),它会迭代 product
中的内容,这将导致重复。因此,在执行操作之前,您可以做的是像这样的简单检查。
{% for key in cart %}
{% for info in product %}
{% if key == info.id %}
// do your bits here
{% endif %}
{% endfor %}
{% endfor %}
希望这对您有所帮助。干杯!