我尝试从数据库中获取json变量:
$table = $this->em->getRepository('App\\Entity\\Data')->findAll();
$encoders = [new JsonEncoder()];
$normalizers = [new DateTimeNormalizer(array('datetime_format' => 'd.m.Y')), new ObjectNormalizer($classMetadataFactory)];
$serializer = new Serializer($normalizers, $encoders);
$groupsArray = $serializerGroups->getSerializerGroups($slug);
$groups = $groupsArray['groups'];
if( $groups != null) {
$context['groups'] = $groups;
}
$output = $serializer->serialize($table, 'json', $context);
dump($output);
页面加载非常长,直到我看到输出结果。如何使它更快?
这实际上取决于您需要的输出,它花费的时间太长,因为在我看来,您在DATA实体中具有嵌套属性,以使其能够更快地使用序列化组https://symfony.com/doc/current/components/serializer.html#component-serializer-attributes-groups-annotations,并且为了获得更快的输出,请避免使用findAll 您可以使用存储库并选择所需的数据,最后您可以使用缓存组件将您的输出存储一段时间