I want to get data from a table and encode it in json. I'm using doctrine and the querybuilder to select the data I want (which is everything from the table basically). The problem is that in dev, everything work fine, but when I go to prod I get the error : PHP Fatal error : Out of memory ...
The data i'm retrieving is 300KB at most. In this case I'm using a route to retrieve a list of project (600B) , it's one of the smallest table. This table has two OneToMany associations.
The data i'm retrieving looks like this :
[
{
id: "bar",
name: "bar",
description: "bar",
project_date_start: "2022-01-01 00:00:00",
project_date_end: "2019-05-21 12:19:27"
},
{
id: "foo",
name: "foo",
description: "foo",
project_date_start: "2021-02-02 00:00:00",
project_date_end: "2022-02-02 00:00:00"
}
]
The doctrine's querybuilder and the function :
public function findAllProject()
{
return $this->createQueryBuilder('p')
->select('p.id', 'p.name', 'p.description', 'p.project_date_start', 'p.project_date_end')
->getQuery()
->getResult()
;
}
public function project(Request $request)
{
$em = $this->getDoctrine()->getEntityManager();
$projectRepo = $this->getDoctrine()->getRepository(Project::class);
$project = $projectRepo->findAllProject();
$data = $this->get('jms_serializer')->serialize($project, 'json');
$response = new Response($data);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
}
I've tried :
- reinstalling Apache on the server
- changing the value of the memory_limit in the php.ini
- not using jms_serialiser and using json_encode instead
EDIT
So the error is definitly not comming from the serializer, since the error is thrown while i'm getting the data from the repository (findAllProject()).
I've managed to make it work by reducing the data i'm retrieving, but it's not a solution since I need all the data and as I said project is one of the smallest table, so what about when I need the retrieve bigger data ?
If i'm getting data back from symfony, maybe it's not symfony but apache, what could cause this error ?
Also, the generated SQL comming from the queryBuilder is good (juste a normal select)
END EDIT
I don't think it's a problem with the json encoding part nor an apache server configuration problem (the memory limit is at -1).
I think the association of the project table is messing up everything and doctrine is trying to go throught all the database. Maybe a problem with the design of my database ?
For exemple, the table type is the same size as project but don't have the out of memory error.
I don't know what else I can try, any idea ?
select
and usegetResult(Query::HYDRATE_ARRAY/HYDRATE_SIMPLEOBJECT)
. More importantly you are running serializer on everything (all projects) you are fetching from DB which is more like a "disaster waiting to happen" type of practise - don't do that! – BentCodergetResult(Query::HYDRATE_ARRAY/HYDRATE_SIMPLEOBJECT)
but coudn't make it work, have you a link that explain how to use it ? I'm fearly new to symfony/doctrine. What is the best way to handle serialization then ? Cause the 'project' array is rly small and the object in itself is rly simple, I don't see how this could be a problem for jms_serialiser. Thank you – Fantin