/**
  * Save crawler category relations
  *
  * @param Maverick_Crawler_Model_Crawler $crawler
  * @return $this
  */
 public function saveCategories(Maverick_Crawler_Model_Crawler $crawler)
 {
     $categoryIds = $crawler->getCategoryIds();
     $oldCategoryIds = $this->getCategoryIds($crawler);
     $crawler->setIsChangedCategories(false);
     $insert = array_diff($categoryIds, $oldCategoryIds);
     $delete = array_diff($oldCategoryIds, $categoryIds);
     $write = $this->_getWriteAdapter();
     if (!empty($insert)) {
         $data = array();
         foreach ($insert as $categoryId) {
             if (empty($categoryId)) {
                 continue;
             }
             $data[] = array('crawler_id' => (int) $crawler->getId(), 'category_id' => (int) $categoryId);
         }
         if ($data) {
             $write->insertMultiple($this->_crawlerCategTable, $data);
         }
     }
     if (!empty($delete)) {
         foreach ($delete as $categoryId) {
             $where = array('crawler_id = ?' => (int) $crawler->getId(), 'category_id = ?' => (int) $categoryId);
             $write->delete($this->_crawlerCategTable, $where);
         }
     }
     if (!empty($insert) || !empty($delete)) {
         $crawler->setAffectedCategoryIds(array_merge($insert, $delete));
         $crawler->setIsChangedCategories(true);
     }
     return $this;
 }