私はPHPとSymfony2を初めて使いましたが、私は自分のやり方を学んでいます。twig /コントローラで 'products'を表示/取得する方法
私のユーザーのカートに関連付けられた商品を「ショーカート」ページに表示することに苦労しています。
これらは私の団体です:
これは私のshowCartAction
機能である:何の意味、
<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-->
何も示しています
/**
* 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,
);
}
これは小枝ファイルですページの「商品」。
EDIT:数量のVARダンプ:あなたの変数名を混同されている1つの場合
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 ▶}
}
あなたのコントローラに 'var_dump($ cart-> getQuantities()')を実行することで、あなたのビューに送られたデータをデバッグしようとしましたか? – alexf
はい、私はそれを上記の編集に含めますあなたはそれが間違っていることを解読するのを助けることができますか? – user6145149
オブジェクト "Doctrine \ ORM \ PersistentCollection"のメソッド "user"が存在しません...しかし、コレクションと配列は基本的に同じものではありませんコレクションにアクセスするにはループを使用する必要がありますか? – user6145149