在我的数据库“ shoppinp-cart-system ”中我有4个表: “购物车”,“ cart_product ”,“用户”和“产品”我想获得当前用户购物车和打印产品,但不仅仅是产品名称,而是完整的特征,即名称,价格,图像和数量。
这是我的CartController中的函数listCartAction():
/**
* @Route("/cart/show/{id}", name="cart_list")
*/
public function listCartAction($id)
{
$cart = $this->getDoctrine()->getRepository('AppBundle:CartProduct')->find($id);
$cartProducts = $cart->getProduct();
return $this->render('cart/cart.view.html.twig', array(
'cart' => $cartProducts
));
}
和Twig模板:
`{% extends 'base.html.twig' %}
{% block body %}
<div style="text-align: center;">
<h1>Products In My Cart</h1>
</div>
{% for c in cart %}
<h1>{{ c.product }}</h1>
{% endfor %}
{% endblock %}`
购物车实体:`
<?
php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cart
*
* @ORM\Table(name="cart")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CartRepository")
*/
class Cart
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $userId;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_updated", type="datetime")
*/
private $dateUpdated;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* @param integer $userId
*
* @return Cart
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return int
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
*
* @return Cart
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* @param \DateTime $dateUpdated
*
* @return Cart
*/
public function setDateUpdated($dateUpdated)
{
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* @return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
}
` 购物车产品实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* CartProduct
*
* @ORM\Table(name="cart_product")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CartProductRepository")
*/
class CartProduct
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Cart
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Cart")
* @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
*/
private $cart;
/**
* @var \stdClass
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set cart
*
* @param \stdClass $cart
*
* @return CartProduct
*/
public function setCart($cart)
{
$this->cart = $cart;
return $this;
}
/**
* Get cart
*
* @return Cart
*/
public function getCart()
{
return $this->cart;
}
/**
* Set product
*
* @var Product
*
* @return CartProduct
*/
public function setProduct($product)
{
$this->product = $product;
return $this;
}
/**
* Get product
* @var Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set quantity
*
* @param integer $quantity
*
* @return CartProduct
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
}
答案 0 :(得分:2)
考虑到您的实体和映射是正确的,您需要返回Cart
实体而不是CartProduct
。请记住,您可以使用symfony的参数转换器,如下所示:
/**
* @Route("/cart/show/{id}", name="cart_list")
*/
public function listCartAction(Cart $cart) //Remember to import the Cart class!
{
return $this->render('cart/cart.view.html.twig', array(
'cart' => $cart
));
}
嫩枝:
{% extends 'base.html.twig' %}
{% block body %}
<div style="text-align: center;">
<h1>Products In My Cart</h1>
</div>
{% for p in cart.products %}
<h1>{{ p.name }}</h1>
<p>{{ p.price }}</p>
//Render other products properties here...
{% endfor %}
{% endblock %}
Symfony paramconverter docs:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
编辑 - 发布实体后
/**
* @Route("/cart/show/{id}", name="cart_list")
*/
public function listCartAction($id)
{
$cartProducts = $this->getDoctrine()->getRepository('AppBundle:CartProduct')
->findByCart($id);
return $this->render('cart/cart.view.html.twig', array(
'cartProducts' => $cartProducts
));
}
嫩枝:
{% extends 'base.html.twig' %}
{% block body %}
<div style="text-align: center;">
<h1>Products In My Cart</h1>
</div>
{% for cp in cartProducts %}
<h1>{{ cp.product.name }}</h1>
<p>{{ cp.product.price }}</p>
<p>{{ cp.quantity }}</p>
//Render other products properties here...
{% endfor %}
{% endblock %}