When you work with doctrine entity methods they will usually return a collection of objects and if you try to serialize them with JsonResponse it will not work. The solution is to use getArrayResult when fetching the entities.
To return an array of entities with JsonResponse in Symfony using getArrayResult, you can follow these steps:
1. Fetch the entities: Use your repository to fetch the entities you need with getArrayResult.
2. Return the JSON response: Use the JsonResponse class to return the fetched data.
Here is an example:
public function calendarFetchEvents(Request $request): JsonResponse { $company = $this->companyRepository->findOneBy(['userCustomer' => $this->getUser()]); $startOfCalendar = DateTime::createFromFormat('Y-m-d', $request->query->get('startOfCalendar')); $endOfCalendar = DateTime::createFromFormat('Y-m-d', $request->query->get('endOfCalendar')); $appointments = $this->appointmentRepository->findAppointmentsByCompanyByTimeRage($company, $startOfCalendar, $endOfCalendar); return new JsonResponse($appointments); }
In this example, the findAppointmentsByCompanyByTimeRage method in the AppointmentRepository uses getArrayResult to fetch the data as an array. The JsonResponse class is then used to return this array as a JSON response.