如何检索从 MySQL 到 JSON 的数据?
How to retrieve data from MySQL to JSON?
我有一个 Symfony 项目,我想在其中存储从 MySQL table 到 JSON 的所有行。目前我的 table 中有五行,但在我的浏览器中只有 returns 五个空值 {"results":[{},{},{},{},{}]}
我想我做对了一些事情,但不是全部。我的代码中缺少什么?
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$results = $repository->findAll();
return $this->json(['results' => $results]);
}
你可以像那样使用序列化器或re-create数组
$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
$jsonCourses[$key]['title'] = $course->getTitle();
}
```
例如,您可以使用 Serializer to convert the array of objects into JSON. There are other ways to achieve this like using jsonResponse 来实现。但是序列化程序是 imo 中最健壮的方式。
仅示例:
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;
}
试试 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]);
}
我有一个 Symfony 项目,我想在其中存储从 MySQL table 到 JSON 的所有行。目前我的 table 中有五行,但在我的浏览器中只有 returns 五个空值 {"results":[{},{},{},{},{}]}
我想我做对了一些事情,但不是全部。我的代码中缺少什么?
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$results = $repository->findAll();
return $this->json(['results' => $results]);
}
你可以像那样使用序列化器或re-create数组
$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
$jsonCourses[$key]['title'] = $course->getTitle();
}
```
例如,您可以使用 Serializer to convert the array of objects into JSON. There are other ways to achieve this like using jsonResponse 来实现。但是序列化程序是 imo 中最健壮的方式。
仅示例:
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;
}
试试 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]);
}