我的任务是获取当前的用户产品。我在CartController中有一个listCartProductsAction,代码如下:
public function listCartProductsAction()
{
$cartProducts = $this->getDoctrine()->getRepository('AppBundle:CartProduct')->findByUser($this->getUser()->getId());
return $this->render('cart/cart.view.html.twig', array(
'cartProducts' => $cartProducts
));
}
在CartRepository中我试图创建一个获取当前用户产品的查询,但它绝对不正确:
public function findByUser($user)
{
$query = $this
->getEntityManager()
->createQuery("SELECT u FROM AppBundle:Cart u WHERE u.IDENTITY = ?1")
->setParameter(1, $user->getUser()->getId());
return $query->getResult();
}
当我尝试在树枝模板中显示产品时,我得到了例外:
在整数
上调用成员函数getUser()
有任何想法如何防止此错误? var_dump($ cartProducts);
之后`array (size=2)
0 =>
object(AppBundle\Entity\Cart)[898]
private 'id' => int 1
private 'userId' =>
object(AppBundle\Entity\User)[765]
private 'id' => int 8
private 'email' => string 'rumen@abv.bg' (length=12)
private 'username' => string 'Rumkata97' (length=9)
private 'name' => string 'Rumen Panchev' (length=13)
private 'password' => string '$2y$13$1QVBSg.uPPMdln6N/5/3DOjubxWtpkZYYtWlHEPc2S.JPx24Qz3Ce' (length=60)
private 'image' => string 'c066099438bc7e258cc0ba21fa9d1b31' (length=32)
private 'image_form' => null
private 'products' =>
object(Doctrine\ORM\PersistentCollection)[789]
...
private 'role' => string 'ROLE_ADMIN' (length=10)
private 'cart' =>
object(Doctrine\ORM\PersistentCollection)[815]
...
private 'dateCreated' =>
object(DateTime)[900]
public 'date' => string '2017-04-25 17:21:58.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
private 'dateUpdated' =>
object(DateTime)[904]
public 'date' => string '2017-04-25 18:02:30.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
1 =>
object(AppBundle\Entity\Cart)[899]
private 'id' => int 3
private 'userId' =>
object(AppBundle\Entity\User)[765]
private 'id' => int 8
private 'email' => string 'rumen@abv.bg' (length=12)
private 'username' => string 'Rumkata97' (length=9)
private 'name' => string 'Rumen Panchev' (length=13)
private 'password' => string '$2y$13$1QVBSg.uPPMdln6N/5/3DOjubxWtpkZYYtWlHEPc2S.JPx24Qz3Ce' (length=60)
private 'image' => string 'c066099438bc7e258cc0ba21fa9d1b31' (length=32)
private 'image_form' => null
private 'products' =>
object(Doctrine\ORM\PersistentCollection)[789]
...
private 'role' => string 'ROLE_ADMIN' (length=10)
private 'cart' =>
object(Doctrine\ORM\PersistentCollection)[815]
...
private 'dateCreated' =>
object(DateTime)[901]
public 'date' => string '2017-04-27 14:28:53.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
private 'dateUpdated' =>
object(DateTime)[902]
public 'date' => string '2017-04-27 15:06:50.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)`
答案 0 :(得分:1)
我认为问题非常简单:findByUser
函数获取用户的int
id(通过$this->getUser()->getId()
),但仍想调用其上的某些函数,当然不行。相反,findByUser
函数中的行应为:
->setParameter(1, $user);