-2
打印有關產品的信息在我的車在我的數據庫「shoppinp-購物車系統」我有4個表: 「車」,「cart_product」,「用戶」和「產品」我想獲得當前的用戶手推車並打印產品,但不僅僅是產品名稱,而是完整的特徵,例如名稱,價格,圖像和數量。如何使用Symfony的
這裏是我的功能listCartAction()在我的CartController:
/**
* @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
));
}
而且枝條模板:
`{% 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;
}
}
我在我的CartProduct中有getProducts()函數,所以改爲購物車$購物車我使用CartProduct $ cart。無論如何,當我嘗試在樹枝中使用p.name時,它會引發此問題: 未找到AppBundle \ Entity \ CartProduct對象。 –
所以你的實體和映射可能是錯誤的。你應該在'Cart'實體中有'getProducts()',而不是在'CartProduct'中。 –
我編輯我的問題,現在你可以看到我的2個實體,我認爲他們沒有錯 –