【问题标题】:Symfony query - retrive all items of one userSymfony 查询 - 检索一个用户的所有项目
【发布时间】:2018-05-30 12:46:36
【问题描述】:

我写了一个查询来返回特定用户购买的所有卡。我不知道我做错了什么。我在卡片表中定义了 user_id 并手动添加了几张卡片进行测试。

我的服务..

public function getUserCardAction($userId)
{

    $card = $this->getCardRepository()
        ->createQueryBuilder('c')
        ->select('c')
        ->where('c.userId = :userId')
        ->setParameter('userId', $userId)
        ->getQuery()
        ->getResult();

    return $card;
}

我的控制器..

public function getUserCard()
{

    $this->get('card.configuration')->getUserCardAction($this->getUser());

    return $this->success();

}

【问题讨论】:

  • 你得到了什么错误?
  • 它返回 TRUE 但没有数据。我在 db 中输入了 user_id 1 的卡片。
  • 向我们展示你的实体类
  • /** * @ORM\Column(name="user_id", type="integer", nullable=true) */ private $userId;

标签: php mysql function symfony


【解决方案1】:

你回来了

$this->success();

这是什么功能?

您不应该返回查询结果吗?喜欢:

return $this->get('card.configuration')->getUserCardAction($this->getUser());

【讨论】:

  • 控制器必须返回一个响应(Array(0 => Object(Bundle\Base\Entity\Card), 1 => Object(Bundle\Base\Entity\Card)) given)。跨度>
  • 是的,在 Symfony 中,控制器必须返回一个响应对象。问题是你期望的回报。例如,您是通过 ajax 调用它并想要一个 JsonResponse 还是要将数据传递给渲染函数。这就是为什么我问你的成功功能里面有什么。你能提供那个代码吗?
  • 你也可以直接通过学说得到结果来缩短事情 $this->em->getRepository('App:Cart')->findBy(['userId' => $this-> getUser()])
  • 我正在尝试归还特定用户拥有的所有卡。 (ID 为 1 的用户)
【解决方案2】:

我设法找到了解决方案。

我刚刚编辑了我的控制器。

 $card = $this->get('card.configuration')->getUserCardAction($this->getUser());

    return $this->success($card);

【讨论】:

  • 尝试添加'->getUserCardAction($this->getUser()->getId())'
猜你喜欢
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多