phpdatabasesymfonysymfony-2.4

PHP/Symfony 2 - Fetch data from db, edit it and resend to another server


In my symfony 2 app by using a console command I want to fetch data from database table. Looping through I want to change them a bit (e.g multiply some values) and then send it to database on another server (by curl).

How can I set new names of the columns and assign those data to them? And also how to send it as an .sql file?

Just to give you brief notion here is a raw version of my command:

class MyCommand extends ContainerAwareCommand
{
      private $input;
      private $output;

      protected function configure()
      {
        // my configs
      }

     protected function execute(InputInterface $input, OutputInterface $output)
      {
       $entityManager = $this->getContainer()->get('doctrine')->getManager('default');

       $queryBuilder = $entityManager->createQueryBuilder();
       $query = $queryBuilder
                -> // my query params



       foreach ($query->iterate() as $e)
      {
       // loop through results of the query in order to create a new DB table 
      }

     $this->sendData($output);

      }


}

Thank you in advance for any help and advises!


Solution

  • You can create another entity manager which connects to a different database. You can see that here, it's a fairly simple yaml config.

    Just bring both entity managers (your 'default' and your 'other') then take what data you need and persist / flush to your other db.

    Like so:

    class MyCommand extends ContainerAwareCommand
    {
          private $input;
          private $output;
    
          protected function configure()
          {
            // my configs
          }
    
         protected function execute(InputInterface $input, OutputInterface $output)
          {
           $entityManager = $this->getContainer()->get('doctrine')->getManager('default');
           $otherEntityManager = $this->getContainer()->get('doctrine')->getManager('other');
    
           $queryBuilder = $entityManager->createQueryBuilder();
           $query = $queryBuilder
                    -> // my query params
    
    
           foreach ($query->iterate() as $e)
           {
               // loop through results 
               $otherEntity = new OtherEntity()
                                    ->setFirstProperty($first)
                                    ->setSecondProperty($second)
                          ;
               $otherEntityManaer->persist($otherEntity);
           }
    
          $otherEntityManager->flush();
    
          }   
    
    }