public function addNewAction()
 {
     $json = array('status' => 0, 'errorPhrases' => '');
     $key = trim($this->request->request->get('key'));
     $phrases = preg_split("/[\r\n]+/", $key);
     if (empty($phrases)) {
         return new JsonResponse($json);
     }
     $errorPhrases = array();
     $successPhrases = array();
     $updatePhrases = array();
     foreach ($phrases as $phrase_line) {
         if (empty($phrase_line)) {
             continue;
         }
         $all = array();
         preg_match_all('|^([a-z\\.\\t,\'\\\\?!\\-’ /]+)(.*)$|i', $phrase_line, $all);
         if (!isset($all[1][0])) {
             continue;
         }
         $phrase = trim(str_replace('’', '\'', $all[1][0]));
         $phrase = str_replace(',', ',', $all[1][0]);
         $mean = isset($all[2][0]) ? $all[2][0] : '';
         $entity = $this->em->getRepository('Eng:PhrasesEntity')->findOneBy(array('name' => $phrase));
         if ($entity === null) {
             $entity = new PhrasesEntity();
             $entity->setName($phrase);
             $entity->setCreateTime(new \Datetime('now'));
         }
         try {
             $entity->setMeans($mean);
             $voiceName = $this->container['phraseVoiceDownloader']->download($entity->getName());
             $entity->setVoice($voiceName);
             $entity->setStatus(PhrasesEntity::NEWONE);
         } catch (\Exception $e) {
             $this->log->addWarning(sprintf("Can not find this word's voice:%s, info:%s", $entity->getName(), $e->getMessage()));
             $errorPhrases[] = $entity->getName();
             continue;
         }
         if ($entity->getId() !== null) {
             $updatePhrases[] = $entity->getName();
         } else {
             $successPhrases[] = $entity->getName();
         }
         $this->em->persist($entity);
         $this->em->flush();
     }
     $message = array();
     if (!empty($successPhrases)) {
         $message[] = 'new phrases: ' . count($successPhrases);
     }
     if (!empty($updatePhrases)) {
         $message[] = 'update phrases: ' . count($updatePhrases);
     }
     if (!empty($errorPhrases)) {
         if (empty($successPhrases) && empty($updatePhrases)) {
             $json['status'] = 1;
         }
         $message[] = 'error phrases: ' . count($errorPhrases);
         $json['errorPhrases'] = implode("\n", $errorPhrases);
     }
     $json['message'] = implode(', ', $message);
     return new JsonResponse($json);
 }
 private function updatePhrase(PhrasesEntity $entity)
 {
     $voiceName = $this->c['phraseVoiceDownloader']->download($entity->getName());
     $entity->setVoice($voiceName);
     $this->em->merge($entity);
     $this->em->flush();
     $this->output->writeln(sprintf("phrase [%s] is updated.", $entity->getName()));
 }