示例#1
0
 /**
  * Разбирание XML файла
  */
 private function parseXml($fileName)
 {
     $xml = simplexml_load_file($fileName);
     $em = $this->getDoctrine()->getManager();
     echo 'название компании: ' . $xml->shop->company . '<br />';
     echo 'URL компании: ' . $xml->shop->url . '<br />';
     foreach ($xml->shop->categories->category as $category) {
         $cat = $this->getDoctrine()->getRepository('EvrikaMainBundle:MarketCategory')->findOneBy(array('offerId' => $category['id'], 'marketUser' => $this->auth));
         $persist = false;
         if (!$cat) {
             $persist = true;
             $cat = new MarketCategory();
             $cat->setOfferId($category['id']);
             $cat->setChilds(null);
             $cat->setCreated(new \DateTime());
             $cat->setMarketUser($this->auth);
         }
         $cat->setName($category);
         $cat->setUpdated(new \DateTime());
         if ($persist == true) {
             #echo $cat->getName.'<br />';
             $em->persist($cat);
         }
         $em->flush();
     }
     foreach ($xml->shop->offers->offer as $value) {
         $book = $this->getDoctrine()->getRepository('EvrikaMainBundle:Market')->findOneBy(array('offerId' => $value['id'], 'marketUser' => $this->auth));
         $persist = false;
         if (!$book) {
             $persist = true;
             $book = new Market();
             $book->setOfferId($value['id']);
             $book->setCreated(new \DateTime());
             $book->setMarketUser($this->auth);
         }
         $book->setName($value->name);
         $book->setImageURL($value->picture);
         $book->setCategory($this->getDoctrine()->getRepository('EvrikaMainBundle:MarketCategory')->findOneBy(array('offerId' => $value->categoryId, 'marketUser' => $this->auth)));
         $book->setAuthor($value->author);
         $book->setPublicHouse($value->publisher);
         $book->setYear($value->year);
         $book->setIsbn($value->ISBN);
         $book->setLink($value->url);
         $book->setCurrency('RUB');
         $book->setCountList($value->page_extent);
         $book->setDecsription($value->description);
         $book->setCena($value->price);
         $book->setType($value['type']);
         $book->setEnabled($value['available']);
         $book->setUpdated(new \DateTime());
         if ($persist == true) {
             $em->persist($book);
         }
         $em->flush();
     }
     return true;
 }