php doctrine findby

sometimes it will take a very time to run, i got this example:
    public function receiveAction()
    {
     $originData=$this->get('request')->request->get('data');
     if (!isset($originData)){
      $response = new Response(json_encode(array('msg' => 'error,no data')));
      $response->headers->set('Content-Type', 'application/json');
      return $response;
     }
     $data = json_decode( $originData );
     
     if (!is_array($data)){
      $response = new Response(json_encode(array('msg' => 'error,data is not correct:'.$originData)));
      $response->headers->set('Content-Type', 'application/json');
      return $response;
     }
     
     $em=$this->getDoctrine()->getEntityManager('webscraper');
     foreach($data as $row){
      if (!isset($row[0]))
       continue;
      /*
      $ad=$em->getRepository('AcmeDataCollectionBundle:SmallAdds')->findBy(array('url' => $row[0]->string));
      if ($ad)
       continue;
      */
      $ad=new SmallAdds();
      $ad->setSource($this->source);
      $ad->setUrl($row[0]->string);
      if(isset($row[1]))
       $ad->setTitle(trim($row[1]->string));
      if(isset($row[2]))
       $ad->setInfo(trim($row[2]->string));
      if(isset($row[3]))
       $ad->setPrice(trim($row[3]->string));
      $em->persist($ad);
     }
     $em->flush();
     $response = new Response(json_encode(array('msg' => 'saved')));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
    }
if i do not comment the findBy, it will take average 13 sec to run. the for is about 30-40 record. As i comment the findBy, it tak 330ms. well, i have no choice.

评论

热门博文