我是PHP和Symfony2的新手,但我正在学习自己的方式。
我正在努力让与我的用户购物车相关联的产品显示在“购物车”上。页。
这些是我的协会:
这是我的showCartAction
功能:
/**
* Shows Empty Cart
*
* @Route("/showCart", name="product_showCart")
* @METHOD("GET")
* @Template()
*/
public function showCartAction(Request $request) {
// --------------------------- show only what the user added to their cart --------------------------- //
// show products associated with userCartID
$em = $this->getDoctrine()->getManager();
// $id = $this->getId();
$user = $this->getUser();
$cart = $em->getRepository('ShopBundle:UserCart')->findOneBy(['user' => $this->getUser()], $submitted=NULL);
if (!$cart) {
throw $this->createNotFoundException(
'ERROR: No product found for id '
);
}
$totalCostOfAllProducts = 0;
if (empty($price) && empty($quantity) && empty($totalCostOfAllProducts) && empty($cartArray)) {
$price = 0; $quantity = 0; $totalCostOfAllProducts = 0; $cartArray = [];
}
return array(
// 'user' => $user,
// 'products' => $cart->getProduct(), // for :Quantity
// 'products' => $cart->getUserCart(), // for :Quantity
'user' => $cart->getUser(), // for :UserCart
'quantity' => $cart->getQuantities(), // for :UserCart
// 'product' => $->getProduct(), // for :UserCart
'totalCostOfAllProducts' => $totalCostOfAllProducts,
);
}
这是枝条文件:
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price Per Unit</th>
<th>Remove Product</th>
</tr>
</thead>
<tbody>
{% for key, quantities in quantity %}
<tr>
{# <td>{{ key }}</td> <!--Product--> #}
<td>{{ quantity.product }}</td> <!--Product-->
<td>
<input class="spinner" value="0" style="width:30px">
</td> <!--Quantity-->
{# <td>${{ product.price }}</td> <!--Price Per Unit--> #}
<td>
<a href="{{ path('product_remove', {'id': key }) }}">
<button name="REMOVE" type="button" class="btn btn-danger" id="removeButton">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
</a>
</td><!--Remove-->
</tr>
{% endfor %}
</tbody>
</table> <!--top table-->
没有显示任何内容,这意味着没有&#39;产品&#39;在页面中。
编辑:var转储数量:
ProductController.php on line 428:
PersistentCollection {#1179 ▼
-snapshot: []
-owner: UserCart {#1161 ▼
-id: 3
-timestamp: null
-submitted: null
-user: User {#1053 ▶}
-quantities: PersistentCollection {#1179}
}
-association: array:15 [ …15]
-em: EntityManager {#68 …10}
-backRefFieldName: "userCart"
-typeClass: ClassMetadata {#1162 …}
-isDirty: false
-initialized: false
-coll: ArrayCollection {#1180 ▶}
}
答案 0 :(得分:0)
如果您正在混合变量名称。
# showCartAction
return array(
...
'quantity' => $cart->getQuantities()
...
);
# showcart.html.twig
{% for key, quantities in quantity %}
...
<td>{{ quantity.product }}</td>
...
{% endfor %}
在您的模板中,“数量”是您购物车中的数量集合,而“数量”是您的for循环中的这些项目之一。 因此,“数量”没有产品属性,但“数量”应该具有它(取决于您的数量实体,可能是外键): #showcart.html.twig {{quantity.product}}
或者甚至更好地标记您的操作'数量'返回的数组,以使您的代码更具可读性。
# showCartAction
$quantities = $cart->getQuantities();
return array(
'quantities' => $quantities;
)
# showcart.html.twig
{% for key, quantity in quantities %}
...
<td>{{ quantity.product }}</td>
...
{% endfor %}
此外,您的数量收集似乎是空的。 检查模板中的集合大小:
# showcart.html.twig
{{ dump(quantities|length) }}
您的ORM配置有任何不足之处,或者您没有将任何灯具加载到您的项目中。
一些小建议: 保持控制器的操作苗条。 外包从模型中获取您的购物车以及异常处理(使用'try..catch'而不是'if'那里)到一个额外的方法。 使用app_dev.php进行开发。它更严格,而是显示详细的错误消息,而不是显示空白表。