如何创建自定义查询以在 Repository Symfony 中获取用户 ID

How to create custom query for getting user id in Repository Symfony

我的任务是获取当前用户的产品。我在 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();
    }

当我尝试在 twig 模板中显示产品时,出现异常:

Call to a member function getUser() on integer

有什么办法可以防止这个错误吗? 在 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 'yQVBSg.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 'yQVBSg.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)`

我认为问题很简单:findByUser 函数获取了用户的 int id(通过 $this->getUser()->getId()),但仍然想在其上调用一些函数,这当然行不通。相反,findByUser 函数中的行应该是:

->setParameter(1, $user);