コード例 #1
0
 private function updateWord(WordsEntity $entity)
 {
     $voiceName = $this->c['wordVoiceDownloader']->download($entity->getName());
     $entity->setVoice($voiceName);
     $this->em->merge($entity);
     $this->em->flush();
     $this->output->writeln(sprintf("word [%s] is updated.", $entity->getName()));
 }
コード例 #2
0
 public function addNewAction()
 {
     $json = array('status' => 0, 'errorWords' => '');
     $key = trim($this->request->request->get('key'));
     $words = preg_split("|[\r\n ]+|i", $key);
     if (empty($words)) {
         return new JsonResponse($json);
     }
     $errorWords = array();
     $successWords = array();
     $updateWords = array();
     foreach ($words as $word) {
         $entity = $this->em->getRepository('Eng:WordsEntity')->findOneBy(array('name' => $word));
         if ($entity === null) {
             $entity = new WordsEntity();
             $entity->setCreateTime(new \Datetime('now'));
             $entity->setName($word);
         }
         try {
             $mean = $this->container['wordMeanDownloader']->download($entity->getName());
             $entity->setMeans(implode("\n", $mean->getMeaning()));
             $entity->setPronunciation($mean->getPhonetic());
             $voiceName = $this->container['wordVoiceDownloader']->download($entity->getName());
             $entity->setVoice($voiceName);
             $entity->setStatus(WordsEntity::NEWONE);
         } catch (\Exception $e) {
             $this->log->addWarning(sprintf("Can not find this word:%s, info:%s", $entity->getName(), $e->getMessage()));
             $errorWords[] = $entity->getName();
             continue;
         }
         if ($entity->getId() !== null) {
             $updateWords[] = $entity->getName();
         } else {
             $successWords[] = $entity->getName();
         }
         $this->em->persist($entity);
         $this->em->flush();
     }
     $message = array();
     if (!empty($successWords)) {
         $message[] = 'new words: ' . count($successWords);
     }
     if (!empty($updateWords)) {
         $message[] = 'update words: ' . count($updateWords);
     }
     if (!empty($errorWords)) {
         if (empty($successWords) && empty($updateWords)) {
             $json['status'] = 1;
         }
         $message[] = 'error words: ' . count($errorWords);
         $json['errorWords'] = implode("\n", $errorWords);
     }
     $json['message'] = implode(', ', $message);
     return new JsonResponse($json);
 }