setName('voip:import:data') ->setDescription('Imports voip data from Flowdat 2') ->setHelp('Imports Voip data from Flowdat 2.') ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2') ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root') ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"') ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of origin database. Default: localhost') ; } protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getContainer(); $this->em = $container->get('doctrine.orm.entity_manager'); // Get data $host = $input->getOption('host'); $dbname = $input->getOption('dbname'); $user = $input->getOption('user'); $password = $input->getOption('password'); // Set defaults if (!$dbname) { $dbname = 'flowdat2'; } if (!$user) { $user = 'root'; } if (!$password) { $password = ''; } if (!$host) { $host = 'localhost'; } // Presentation $io = new SymfonyStyle($input, $output); $io->title('Flowdat3 VoIP Data Importer'); // Ask for confirmation if (!$input->getOption('no-interaction')) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion("This will IMPORT DATA FROM a Flowdat2 database. The CABLEMODEM TABLE will be ALTERED by OVERWRITING existing data, leading to potential DATA LOSS. Are you really sure you want to proceed? ", false); if (!$helper->ask($input, $output, $question)) { return; } } $io->newLine(); $conn = new mysqli($host, $user, $password, $dbname); $sql = "SELECT * FROM Voip;"; $rows = $conn->query($sql); // Progress bar $progressBar = new ProgressBar($output, $rows->num_rows); $errors = []; while ($row = $rows->fetch_assoc()) { if ($row['cablemodem_id'] != null) { $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']); if (!is_null($object)) { $voip = $object->getVoip(); if (is_null($voip)) { $voip = array(); } $found = null; // Look for an item containing the number foreach ($voip as $key => $item) { if ($item['number'] == $row['number']) { $found = $key; } } if (is_null($found)) { // If number wasn't found, append. $voip[] = array( 'number' => $row['number'], 'password' => $row['password'] ); } else { // If number was found, update password. $voip[$found]['password'] = $row['password']; } // Persist $object->setVoip($voip); $this->em->persist($object); $this->em->flush(); // Detach the object from the entity manager $this->em->detach($object); } } else { $errors[] = "[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped."; } $progressBar->advance(); } $output->writeln(['']); $output->writeln($errors); $output->writeln(['', 'Done!']); } }