1
votes

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 database looks like this

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 ?

1
Looks like you are just dealing with a single entity and not any of its associations so start by removing select and use getResult(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!BentCoder
I tried using getResult(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 youFantin
just out of curiosity: have you tried setting the env on your dev machine to "prod" and if that works, checking php.net/memory_get_usage to see how much memory is being used. I mean, maybe your server actually has too little memory. if it's not actually such a big amount of memory: I read once from another person, that had a one-bit-flip in the php executable and it caused out-of-memory whenever he serialized stuff. sooo ... you could try to reinstall php ...Jakumi
if the out of memory appears on the serializer-line: try (for testing purposes) other serializations like just calling json_encode, print_r, var_dump, var_export, ...Jakumi
I've tested the prod config on my local machine and it works fine. As I said in my post i've already tried différent way of serialization, I rly don't think it's a serialization problem cause i'm getting the error when my data is fetch.Fantin

1 Answers

0
votes

So, I reinstalled my server from scratch then my project and it worked, so I don't know what was causing this issue.