Can’t convert entity content to CSV [Symfony]

i’d like to serialize any entity of my project without knowing the getters inside. And, then, export it to CSV or Json.

To do such a simple thing i made this function below, where "entity" is the entity that i’ve received.

       $donnees = $this->getDoctrine()->getRepository($entity)->findAll();
       $rows = array();

       $getters = array_filter(get_class_methods($entity), function($method) {
           return 'get' === substr($method, 0, 3);
       });
       foreach ($getters as $getter) {
           echo $entity->{$getter}();
           foreach ($donnees as $donnee) {
               $data = array($donnee->$entity->{$getter}());
               $rows[] = implode('#', $data);
           }
       }
       $content = implode("n", $rows);
       $response = new Response($content);
       $response->headers->set('Content-Type', 'text/csv');
       $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
       return $response;

The problem is probably come from this line $data = array($donnee->$entity->{$getter}());

I’m working with php 7 and Symf 5.

I hope somebody can help me beacause i’m losing my mind at this since this morning. that’s maybe why my problem is propably ridiculous.

Thank you

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *