如何从 MySQL 检索数据到 JSON?

发布于 2025-01-11 15:13:57 字数 389 浏览 0 评论 0原文

我有一个 Symfony 项目,我想将 MySQL 表中的所有行存储到 JSON。目前我的表中有五行,但在我的浏览器中它只返回五个空值 {"results":[{},{},{},{},{}]}

I我想我做对了一些事情,但不是全部。我的代码中缺少什么?

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
  $results = $repository->findAll();
  return $this->json(['results' => $results]);
}

I have a Symfony project where I want to store the all the rows from my MySQL table to JSON. Currently there are five rows in my table, but in my browser it only returns five empty values as {"results":[{},{},{},{},{}]}

I guess I have done something right, but not everything. What am I missing in my code?

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
  $results = $repository->findAll();
  return $this->json(['results' => $results]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪 2025-01-18 15:13:57

尝试 createQueryBuilder 它很有用。

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
  $qb = $repository->createQueryBuilder("b");
  $results = $qb->getQuery()->getArrayResult();
  return $this->json(['results' => $results]);
}

Try createQueryBuilder its usefull.

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
  $qb = $repository->createQueryBuilder("b");
  $results = $qb->getQuery()->getArrayResult();
  return $this->json(['results' => $results]);
}
倾城泪 2025-01-18 15:13:57

您可以使用序列化器或像这样自己重新创建数组

$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
    $jsonCourses[$key]['title'] = $course->getTitle();
}
```

You can use the serializer or re-create the array yourself like that

$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
    $jsonCourses[$key]['title'] = $course->getTitle();
}
```
逆流 2025-01-18 15:13:57

您可以通过使用 Serializer 将对象数组转换为 JSON 来实现此目的。还有其他方法可以实现此目的,例如使用 jsonResponse

仅示例:

use Symfony\Component\Serializer\SerializerInterface;

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository, SerializerInterface $serializer)
{
  $results = $repository->findAll();
  $jsonResults = $serializer->serialize($results, 'json');
  
  //If you need to handle any circular references, you can use this..
  $jsonResults = $serializer->serialize($results, 'json', array(
        'circular_reference_handler' => function ($object) { return $object; },
  ));

  return $jsonResults;
}

You can achieve this by Using a Serializer to convert the array of objects into JSON. There are other ways to achieve this like using jsonResponse for example. But the serializer is the most robust way imo.

Example only:

use Symfony\Component\Serializer\SerializerInterface;

#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository, SerializerInterface $serializer)
{
  $results = $repository->findAll();
  $jsonResults = $serializer->serialize($results, 'json');
  
  //If you need to handle any circular references, you can use this..
  $jsonResults = $serializer->serialize($results, 'json', array(
        'circular_reference_handler' => function ($object) { return $object; },
  ));

  return $jsonResults;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文