Ejemplo n.º 1
0
 static function addCategory($id, $categoryId, $source = null)
 {
     $db = LsDb::getDbConnection();
     $sql = "INSERT INTO os_entity_category (entity_id, category_id, source, created_at, updated_at) " . "VALUES (?, ?, ?, ?, ?) " . "ON DUPLICATE KEY UPDATE updated_at = ?";
     $now = LsDate::getCurrentDateTime();
     return $stmt = $db->execute($sql, array($id, $categoryId, $source, $now, $now, $now));
 }
Ejemplo n.º 2
0
 public function save(Doctrine_Connection $conn = null, $updateList = true, $updateEntity = true)
 {
     if ($conn == null) {
         $conn = $this->_table->getConnection();
     }
     try {
         $conn->beginTransaction();
         $ret = parent::save($conn);
         if ($updateList) {
             $this->LsList->last_user_id = LsVersionableListener::getUserId();
             $this->LsList->updated_at = LsDate::getCurrentDateTime();
             $this->LsList->save();
         }
         if ($updateEntity) {
             $this->Entity->last_user_id = LsVersionableListener::getUserId();
             $this->Entity->updated_at = LsDate::getCurrentDateTime();
             $this->Entity->save();
         }
         $conn->commit();
     } catch (Exception $e) {
         $conn->rollback();
         throw $e;
     }
     return $ret;
 }
 protected function convertRelationship($id, $oldEntity2Id, $newEntity2Id)
 {
     $now = LsDate::getCurrentDateTime();
     //update relationship
     $sql = 'UPDATE relationship r SET entity2_id = ?, last_user_id = ?, updated_at = ? WHERE id = ?';
     if (!$this->testMode) {
         $stmt = $this->db->execute($sql, array($newEntity2Id, 1, $now, $id));
         if (!$stmt->rowCount()) {
             throw new Exception("Couldn't update relationship " . $id);
         }
     }
     //create modification record
     $sql = 'INSERT INTO modification (object_model, object_id, object_name, created_at, updated_at) ' . 'VALUES (?, ?, ?, ?, ?)';
     if (!$this->testMode) {
         $stmt = $this->db->execute($sql, array('Relationship', $id, 'Relationship ' . $id, $now, $now));
         if (!$stmt->rowCount()) {
             throw new Exception("Couldn't insert modification record for relationship " . $id);
         }
     }
     //create modification_field record
     $modId = $this->db->lastInsertId('modification');
     $sql = 'INSERT INTO modification_field (modification_id, field_name, old_value, new_value) VALUES (?, ?, ?, ?)';
     if (!$this->testMode) {
         $stmt = $this->db->execute($sql, array($modId, 'entity2_id', $oldEntity2Id, $newEntity2Id));
         if (!$stmt->rowCount()) {
             throw new Exception("Couldn't insert modification_field record for relationship " . $id);
         }
     }
     if ($this->debugMode) {
         print "Converted relationship " . $id . "\n";
     }
 }
Ejemplo n.º 4
0
 public function executeMatch($request)
 {
     if ($request->isMethod('post')) {
         $this->checkArticle($request);
         //get verified entity ids
         $oldEntities = LsDoctrineQuery::create()->select('ae.entity_id')->from('ArticleEntity ae')->leftJoin('ae.Entity e')->where('ae.article_id = ?', $this->article->id)->andWhere('ae.is_verified = 1')->andWhere('e.is_deleted = 0')->fetchAll(PDO::FETCH_COLUMN);
         //get all submitted entity ids
         $newEntities = array_unique($request->getParameter('entity_ids', array()));
         //compute changes
         $verifiedEntities = array_diff($newEntities, $oldEntities);
         $unverifiedEntities = array_diff($oldEntities, $newEntities);
         //get user and time
         $userId = $this->getUser()->getGuardUser()->id;
         $time = LsDate::getCurrentDateTime();
         //save changes
         LsDoctrineQuery::create()->update('ArticleEntity')->set('is_verified', '?', true)->set('reviewed_by_user_id', $userId)->set('reviewed_at', '?', array($time))->where('article_id = ?', $this->article->id)->andWhereIn('entity_id', $verifiedEntities)->execute();
         LsDoctrineQuery::create()->update('ArticleEntity')->set('is_verified', '?', false)->set('reviewed_by_user_id', $userId)->set('reviewed_at', '?', array($time))->where('article_id = ?', $this->article->id)->andWhereIn('entity_id', $unverifiedEntities)->execute();
         $this->article->description = $request->getParameter('description');
         $this->article->reviewed_by_user_id = $userId;
         $this->article->reviewed_at = $time;
         $this->article->save();
         $this->redirect('article/admin');
     }
 }
Ejemplo n.º 5
0
 public function logCustomFieldChange($key, $oldValue, $newValue, $modificationId = null)
 {
     $object = $this->getInvoker();
     if (!($id = $object->id)) {
         throw Exception("Can't log custom field changes for object without id");
     }
     $model = get_class($object);
     $db = Doctrine_Manager::connection();
     try {
         $db->beginTransaction();
         if (!$modificationId) {
             //insert modification
             $userId = LsVersionableListener::getUserId();
             $time = LsDate::getCurrentDateTime();
             $sql = 'INSERT INTO modification (object_model, object_id, user_id, created_at, updated_at) ' . 'VALUES (?, ?, ?, ?, ?)';
             $stmt = $db->execute($sql, array($model, $id, $userId, $time, $time));
             $modificationId = $db->lastInsertId('modification');
         }
         //insert modification field
         $sql = 'INSERT INTO modification_field (modification_id, field_name, old_value, new_value) VALUES (?, ?, ?, ?)';
         $stmt = $db->execute($sql, array($modificationId, $key, $oldValue, $newValue));
         $db->commit();
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
 }
 static function lockEntityById($id, $userId)
 {
     $db = Doctrine_Manager::connection();
     $sql = 'UPDATE os_entity_transaction SET locked_at = ?, locked_by_user_id = ? WHERE entity_id = ?';
     $stmt = $db->execute($sql, array(LsDate::getCurrentDateTime(), $userId, $id));
 }
Ejemplo n.º 7
0
 static function addAlias($id, $alias)
 {
     $db = LsDb::getDbConnection();
     $sql = "INSERT IGNORE INTO alias (entity_id, name, context, updated_at, created_at) VALUES (?, ?, ?, ?, ?)";
     $now = LsDate::getCurrentDateTime();
     return $stmt = $db->execute($sql, array($id, $alias, "opensecrets", $now, $now));
 }
Ejemplo n.º 8
0
 static function mergeAll(Entity $e1, Entity $e2)
 {
     $db = Doctrine_Manager::connection();
     try {
         $db->beginTransaction();
         //MERGE ENTITIES AND EXTENSIONS
         $e1 = self::mergeBasic($e1, $e2);
         //MERGE CONTACT INFO
         //phones are moved to e1
         foreach ($e2->Phone as $phone) {
             if ($e1->id) {
                 $q = LsDoctrineQuery::create()->from('Phone p')->where('p.entity_id = ? AND p.number = ?', array($e1->id, $phone->number));
                 //not if e1 already has a phone with same number
                 if ($q->count()) {
                     continue;
                 }
             }
             $phone->entity_id = $e1['id'];
             $phone->setMerge(true);
             $phone->save();
         }
         //emails are moved to e1
         foreach ($e2->Email as $email) {
             if ($e1->id) {
                 $q = LsDoctrineQuery::create()->from('Email e')->where('e.entity_id = ? AND e.address = ?', array($e1->id, $email->address));
                 //not if e1 already has the same email
                 if ($q->count()) {
                     continue;
                 }
             }
             $email->entity_id = $e1['id'];
             $email->setMerge(true);
             $email->save();
         }
         //addresses
         foreach ($e2->Address as $address) {
             if ($e1->id) {
                 if ($address->latitude && $address->longitude) {
                     //not if e1 already has an address with the same coords
                     if (AddressTable::getByCoordsQuery($address->longitude, $address->latitude, $e1)->count()) {
                         continue;
                     }
                 }
             }
             $address->entity_id = $e1['id'];
             $address->setMerge(true);
             $address->save();
         }
         //RELATIONSHIPS
         if ($e2->id) {
             //where e2 is first
             foreach ($e2->getRelationshipsQuery(1)->execute() as $rel) {
                 //don't try to merge relationships, there isn't a good way to do this automatically
                 $rel->entity1_id = $e1['id'];
                 $rel->setMerge(true);
                 $rel->save();
             }
             //where e2 is second
             foreach ($e2->getRelationshipsQuery(2)->execute() as $rel) {
                 //don't try to merge relationships, there isn't a good way to do this automatically
                 $rel->entity2_id = $e1['id'];
                 $rel->setMerge(true);
                 $rel->save();
             }
         }
         //LISTS
         //lists are moved to e1
         foreach ($e2->LsListEntity as $listEntity) {
             if ($e1->id) {
                 $q = LsQuery::getByModelAndFieldsQuery('LsListEntity', array('entity_id' => $e1->id, 'list_id' => $listEntity->list_id));
                 //not if e1 is already on the list
                 if ($q->count()) {
                     continue;
                 }
             }
             $listEntity->entity_id = $e1;
             $listEntity->setMerge(true);
             $listEntity->save();
         }
         //IMAGES
         //images are moved to e1
         foreach ($e2->Image as $image) {
             $image->entity_id = $e1;
             $image->setMerge(true);
             $image->save();
         }
         //ALIASES
         //aliases are moved to e1
         foreach ($e2->Alias as $alias) {
             //not if e1 already has that alias
             if ($e1->id && $e1->hasName($alias->name, $matchContext = true, $alias->context)) {
                 continue;
             }
             $alias->entity_id = $e1;
             $alias->is_primary = false;
             $alias->setMerge(true);
             $alias->save();
         }
         //save now so that template merges work
         $e1->last_user_id = LsVersionableListener::getUserId();
         $e1->updated_at = LsDate::getCurrentDateTime();
         $e1->setMerge(true);
         $e1->save();
         //set merged_id for e2
         $e2->merged_id = $e1['id'];
         //TEMPLATES
         foreach ($e2->_table->getTemplates() as $template) {
             //templates must have mergeFrom method
             if (method_exists($template, 'mergeFrom')) {
                 $template->setInvoker($e1);
                 $template->mergeFrom($e2);
             }
         }
         //OTHER RELATIONS
         foreach ($e2->Child as $child) {
             $child->parent_id = $e1->id;
             $child->setMerge(true);
             $child->save();
         }
         foreach ($e2->PartyMember as $member) {
             $member->party_id = $e1->id;
             $member->setMerge(true);
             $member->save();
         }
         foreach ($e2->BusinessIndustry as $bi) {
             $q = LsDoctrineQuery::create()->from('BusinessIndustry bi')->where('bi.business_id = ? AND bi.industry_id = ?', array($e1->id, $bi->industry_id));
             if (!$q->count()) {
                 $bi->business_id = $e1->id;
                 $bi->save();
             }
         }
         foreach ($e2->OverseenPosition as $position) {
             $position->boss_id = $e1->id;
             $position->save();
         }
         foreach ($e2->BundledDonation as $donation) {
             $donation->bundler_id = $e1->id;
             $donation->save();
         }
         foreach ($e2->LobbyFilingLobbyist as $lfl) {
             $lfl->lobbyist_id = $e1->id;
             $lfl->save();
         }
         foreach ($e2->Contact1Transaction as $t) {
             $t->contact1_id = $e1->id;
             $t->save();
         }
         foreach ($e2->Contact2Transaction as $t) {
             $t->contact2_id = $e1->id;
             $t->save();
         }
         //NOTES
         sfLoader::loadHelpers('Url');
         $notes = LsDoctrineQuery::create()->from('Note n')->where('n.entity_ids LIKE ?', '%,' . $e2->id . ',%')->execute();
         foreach ($notes as $note) {
             $note->body = preg_replace('#/(person|org)/' . $e2->id . '/([^"]+)#i', '/\\1/' . $e1->id . '/' . LsSlug::convertNameToSlug($e1->name), $note->body);
             $note->body_raw = preg_replace('#@entity:' . $e2->id . '(\\[([^\\]]+)\\])?#', '@entity:' . $e1->id . '\\1', $note->body_raw);
             $note->entity_ids = str_replace(',' . $e2->id . ',', ',' . $e1->id . ',', $note->entity_ids);
             $note->save();
         }
         //DONATION MATCHES
         //get matches for e2
         if ($e2->id != '') {
             $matches2 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e2->id);
             //get matches for e1
             $matches1 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e1->id);
             $matches1Sorted = array();
             //sort matches for e1
             foreach ($matches1 as $match1) {
                 $matches1Sorted[$match1['cycle']][$match1['transaction_id']] = $match1;
             }
             //loop through e2 matches
             foreach ($matches2 as $match2) {
                 //if e1 matched exists, update as necessary
                 if ($match1 = @$matches1Sorted[$match2['cycle']][$match2['transaction_id']]) {
                     $save = false;
                     if (!$match1['is_verified'] && $match2['is_verified']) {
                         $match1->is_verified = true;
                         $save = true;
                     }
                     if (!$match1['is_processed'] && $match2['is_processed']) {
                         $match1->is_processed = true;
                         $save = true;
                     }
                     if (!$match1['is_synced'] && $matche2['is_synced']) {
                         $match1->is_synced = true;
                         $save = true;
                     }
                     if ($save) {
                         $match1->save();
                     }
                     $match2->delete();
                 } else {
                     //if e1 match doesn't exist, update the entity_id of the e2 match
                     $match2->entity_id = $e1->id;
                     $match2->save();
                 }
             }
             //DONATION MATCHING PREPROCESS LOG
             $cycles2 = Doctrine::getTable('OsEntityPreprocess')->findByEntityId($e2->id);
             $cycles1 = OsEntityPreprocessTable::getCyclesByEntityId($e1->id);
             foreach ($cycles2 as $cycle2) {
                 //if e1 already preprocessed for this cycle, delete
                 if (in_array($cycle2['cycle'], $cycles1)) {
                     $cycle2->delete();
                 } else {
                     //if not, update entity_id
                     $cycle2->entity_id = $e1->id;
                     $cycle2->save();
                 }
             }
         }
         //EXTERNAL KEYS
         foreach ($e2->ExternalKey as $key) {
             $q = LsDoctrineQuery::create()->from('ExternalKey k')->where('k.entity_id = ?', $e1->id)->andWhere('k.domain_id = ?', $key['domain_id']);
             if (!($existingKey = $q->fetchOne())) {
                 $key->entity_id = $e1->id;
                 $key->save();
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
     $e2->clearRelated();
     return $e1;
 }
 static function logUpdate($id)
 {
     $db = LsDb::getDbConnection();
     $now = LsDate::getCurrentDateTime();
     $sql = "INSERT INTO task_meta (task, namespace, predicate, value, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?) " . "ON DUPLICATE KEY UPDATE value = ?";
     $params = array(__CLASS__, $id, self::$logKey, $now, $now, $now, $now);
     return $stmt = $db->execute($sql, $params);
 }
Ejemplo n.º 10
0
 static function logChanges(Doctrine_Record $oldObject, Doctrine_Record $newObject, $new = false)
 {
     $merged = false;
     $modId = null;
     //isMerge() is used to set the modification's is_merge field, which, for non-delete
     //modifications, is only used to identify the modification as part of a larger merge
     if (method_exists($newObject, 'isMerge')) {
         $merged = $newObject->isMerge();
     }
     if (get_class($oldObject) != get_class($newObject)) {
         throw new exception("Can't log modifications between objects of different classes");
     }
     $conn = Doctrine_Manager::connection();
     try {
         $conn->beginTransaction();
         //get clean sets of old and new data
         $oldData = method_exists($oldObject, 'getAllData') ? $oldObject->getAllData() : $oldObject->toArray(false);
         $newData = method_exists($newObject, 'getAllData') ? $newObject->getAllData() : $newObject->toArray(false);
         //if modification is create, set all old data to null
         if ($new) {
             $oldData = array_fill_keys(array_keys($oldData), null);
         }
         //remove null and blank keys
         $oldData = array_filter($oldData, array('self', 'notNullOrBlankCallback'));
         $newData = array_filter($newData, array('self', 'notNullOrBlankCallback'));
         //change false values to 0
         $oldData = array_map(array('self', 'falseToZeroCallback'), $oldData);
         $newData = array_map(array('self', 'falseToZeroCallback'), $newData);
         //create set of possible modified fields
         $fields = array_unique(array_merge(array_keys($oldData), array_keys($newData)));
         $fields = array_diff($fields, array('id', 'created_at', 'updated_at', 'is_deleted', 'last_user_id'));
         $modifiedFields = array();
         foreach ($fields as $field) {
             //field is modified if field was or is no longer null, or if old and new values are different
             if (!array_key_exists($field, $oldData) || !array_key_exists($field, $newData) || !self::areSameValues($oldData[$field], $newData[$field])) {
                 $modifiedFields[] = $field;
             }
         }
         if ($merged || count($modifiedFields)) {
             $db = Doctrine_Manager::connection();
             $sql = 'INSERT INTO modification (object_model, object_id, object_name, is_create, is_merge, user_id, created_at, updated_at) ' . 'VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
             if (!($name = $newObject->getName())) {
                 $name = get_class($newObject);
             }
             $params = array(get_class($oldObject), $newObject->id ? $newObject->id : '', $name, $new, $merged, self::getUserId(), LsDate::getCurrentDateTime(), LsDate::getCurrentDateTime());
             $stmt = $db->execute($sql, $params);
             $modId = $db->lastInsertId('modification');
             if (count($modifiedFields)) {
                 //insert all modification_field records at once
                 $sql = 'INSERT INTO modification_field (modification_id, field_name, old_value, new_value) VALUES';
                 $params = array();
                 foreach ($modifiedFields as $field) {
                     $sql .= ' (?, ?, ?, ?),';
                     $params = array_merge($params, array($modId, $field, isset($oldData[$field]) ? $oldData[$field] : null, isset($newData[$field]) ? $newData[$field] : null));
                 }
                 $sql = substr($sql, 0, strlen($sql) - 1);
                 $stmt = $db->execute($sql, $params);
             }
         }
         $conn->commit();
     } catch (Exception $e) {
         $conn->rollback();
         throw $e;
     }
     unset($oldObject);
     unset($newObject);
     return $modId;
 }
Ejemplo n.º 11
0
 public function executeChat($request)
 {
     $this->checkUser();
     $this->room = $request->getParameter('room', 1);
     if ($this->room < 1 || $this->room > 20) {
         $this->room = 1;
     }
     //log chat user
     $db = Doctrine_Manager::connection();
     $sql = 'REPLACE INTO chat_user (user_id, room, updated_at) VALUES (?, ?, ?)';
     //use REPLACE in order to update timestamp
     $stmt = $db->execute($sql, array($this->getUser()->getGuardUser()->id, $this->room, LsDate::getCurrentDateTime()));
     //get other chat users
     $q = LsDoctrineQuery::create()->from('sfGuardUser u')->leftJoin('u.Profile p')->leftJoin('u.ChatUser cu')->where('cu.room = ?', $this->room)->andWhere('cu.updated_at > ?', date('Y-m-d H:i:s', strtotime('5 minutes ago')))->andWhere('u.id <> ?', $this->getUser()->getGuardUser()->id)->setHydrationMode(Doctrine::HYDRATE_ARRAY);
     $this->users = $q->execute();
 }
Ejemplo n.º 12
0
 protected function scrapeArticles()
 {
     foreach ($this->articleList as $url => $ary) {
         $article = new Article();
         $article->url = $this->cleanArticleUrl($url);
         $article->title = $this->cleanArticleTitle($ary['title']);
         $article->source_id = $this->getArticleSourceId();
         //get authors if listed
         if (preg_match_all('#q=author%3A%22([^%]+)%22#i', $ary['subtitle'], $authorMatches, PREG_PATTERN_ORDER)) {
             $article->authors = urldecode(implode(',', $authorMatches[1]));
         }
         try {
             $article->body = $this->scrapeArticleBody($article->url);
         } catch (Exception $e) {
             $this->printDebug("*** Error scraping article: " . $e->getMessage() . " (" . $url . ")");
             continue;
         }
         $article->published_at = $this->getArticleDateFromBody($article->body);
         $article->reviewed_at = NULL;
         $article->found_at = LsDate::getCurrentDateTime();
         $this->articles[] = $article;
         $this->printDebug("+ Scraped article '" . $article->title . "'");
     }
 }
Ejemplo n.º 13
0
 public function processEntity($id, $newTrans, $oldTrans)
 {
     //get person names so we can make sure added donations are from the right person
     $sql = 'SELECT * FROM person WHERE entity_id = ?';
     $stmt = $this->db->execute($sql, array($id));
     if (!($donorPerson = $stmt->fetch(PDO::FETCH_ASSOC))) {
         if ($this->debugMode) {
             print "* Can't find Person record for donor with entity_id " . $id . "; skipping...";
         }
         return;
     }
     if ($this->debugMode) {
         print "\n=== Processing entity " . $id . " (" . PersonTable::getLegalName($donorPerson) . ") ===\n";
     }
     $recipients = array();
     //get donations from all the newly matched transactions
     $newDonations = $this->getDonations($newTrans);
     foreach ($newDonations as $donation) {
         if (!$this->namesAreCompatible($donorPerson, $donation)) {
             if ($this->debugMode) {
                 print "* Skipping donation with incompatible donor name: " . $donation['donor_name'] . "\n";
             }
             continue;
         }
         $cycle = $donation['cycle'];
         $recipientId = $donation['recipient_id'];
         if (isset($recipients[$cycle][$recipientId]['new'])) {
             $recipients[$cycle][$recipientId]['new'][] = $donation;
         } else {
             if (!isset($recipients[$cycle])) {
                 $recipients[$cycle] = array();
             }
             $recipients[$cycle][$recipientId] = array();
             $recipients[$cycle][$recipientId]['new'] = array($donation);
             $recipients[$cycle][$recipientId]['old'] = array();
         }
     }
     //get donations from all the old transactions
     $oldDonations = $this->getDonations($oldTrans);
     foreach ($oldDonations as $donation) {
         $cycle = $donation['cycle'];
         $recipientId = $donation['recipient_id'];
         if (isset($recipients[$cycle][$recipientId]['old'])) {
             $recipients[$cycle][$recipientId]['old'][] = $donation;
         } else {
             if (!isset($recipients[$cycle])) {
                 $recipients[$cycle] = array();
             }
             $recipients[$cycle][$recipientId] = array();
             $recipients[$cycle][$recipientId]['old'] = array($donation);
             $recipients[$cycle][$recipientId]['new'] = array();
         }
     }
     //if there are NO already-processed matches, and no matches to remove,
     //ie, if we're going from no matches to any number of matches,
     //we can delete existing donation relationships for this entity
     $deleteRels = false;
     if (!count($oldDonations)) {
         $sql = 'SELECT COUNT(*) FROM os_entity_transaction WHERE entity_id = ? AND is_processed = 1';
         $stmt = $this->db->execute($sql, array($id));
         if (!$stmt->fetch(PDO::FETCH_COLUMN)) {
             $deleteRels = true;
         }
     }
     if ($deleteRels) {
         if ($this->debugMode) {
             print "- Removing old donation relationships...\n";
         }
         //first get ids
         $sql = 'SELECT DISTINCT r.id FROM relationship r ' . 'LEFT JOIN fec_filing f ON (f.relationship_id = r.id) ' . 'WHERE r.entity1_id = ? AND r.category_id = ? AND r.is_deleted = 0 ' . 'AND f.id IS NOT NULL';
         $stmt = $this->db->execute($sql, array($id, RelationshipTable::DONATION_CATEGORY));
         $relIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
         if (count($relIds)) {
             //soft delete them
             $sql = 'UPDATE relationship SET is_deleted = 1, updated_at = ? WHERE id IN (' . implode(',', $relIds) . ')';
             $params = array(LsDate::getCurrentDateTime());
             $this->db->execute($sql, $params);
             //create modification records of the deletions
             $sql = 'INSERT INTO modification (object_model, object_id, object_name, is_delete, created_at, updated_at) ' . 'VALUES ';
             $params = array();
             foreach ($relIds as $relId) {
                 $sql .= '(?, ?, ?, ?, ?, ?), ';
                 $now = LsDate::getCurrentDateTime();
                 $params = array_merge($params, array('Relationship', $relId, 'Relationship ' . $relId, true, $now, $now));
             }
             $sql = substr($sql, 0, strlen($sql) - 2);
             $stmt = $this->db->execute($sql, $params);
         }
     }
     //make sure the entity hasn't been deleted in the meantime!
     $sql = 'SELECT id FROM entity WHERE id = ? AND is_deleted = 0';
     $stmt = $this->db->execute($sql, array($id));
     if (!$stmt->fetch(PDO::FETCH_COLUMN)) {
         //skip to the end
         $recipients = array();
     }
     //create filings/relationships for each cycle-recipient pair
     foreach ($recipients as $cycle => $recipients) {
         foreach ($recipients as $recipientId => $donations) {
             //if it's a committee recipient, try to determine
             //whether it belongs to a candidate
             if (strpos($recipientId, 'C') === 0) {
                 $recipientId = $this->getFinalRecipientIdByCycleAndCommitteeId($cycle, $recipientId);
             }
             //find the entity with this recipient id, or generate a new one
             if (!($recipientEntity = $this->getEntityByRecipientId($recipientId))) {
                 if ($this->debugMode) {
                     print "* Couldn't find or create entity for recipient " . $recipientId . "; skipping...\n";
                 }
                 continue;
             }
             //create committee entity and position relationship between it and the candidate, if necessary
             //DISABLED, FOR NOW
             //$this->createCampaignCommittee($recipientEntity['id'], $recipientId);
             if ($this->debugMode) {
                 print "Updating donation relationship with " . $recipientEntity['name'] . "...\n";
             }
             //see if there's already a relationship
             Doctrine_Manager::getInstance()->setCurrentConnection('main');
             $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? AND r.entity2_id = ? AND r.category_id = ?', array($id, $recipientEntity['id'], RelationshipTable::DONATION_CATEGORY));
             $rel = $q->fetchOne();
             //create relationship if there's not already one
             if (!$rel) {
                 //but if there aren't any new donations, then we skip this recipient
                 //THIS SHOULD NOT TYPICALLY HAPPEN, BECAUSE NO NEW DONATIONS MEANS
                 //THERE ARE OLD DONATIONS TO REMOVE, WHICH MEANS THERE SHOULD BE
                 //EXISTING RELATIONSHIPS... they may have been deleted
                 if (!count($donations['new'])) {
                     if ($this->debugMode) {
                         print "* No relationships found, and no new donations to process, so skipping it...\n";
                     }
                     continue;
                 }
                 if ($this->debugMode) {
                     print "+ Creating new donation relationship\n";
                 }
                 $rel = new Relationship();
                 $rel->entity1_id = $id;
                 $rel->entity2_id = $recipientEntity['id'];
                 $rel->setCategory('Donation');
                 $rel->description1 = 'Campaign Contribution';
                 $rel->description2 = 'Campaign Contribution';
                 $rel->save();
             }
             //add new filings and references to the relationship
             foreach ($donations['new'] as $donation) {
                 $filing = new FecFiling();
                 $filing->relationship_id = $rel->id;
                 $filing->amount = $donation['amount'];
                 $filing->fec_filing_id = $donation['fec_id'];
                 $filing->crp_cycle = $donation['cycle'];
                 $filing->crp_id = $donation['row_id'];
                 $filing->start_date = $donation['date'];
                 $filing->end_date = $donation['date'];
                 $filing->is_current = false;
                 $filing->save();
                 if ($this->debugMode) {
                     print "+ Added new FEC filing: " . $donation['fec_id'] . " (" . $donation['amount'] . ")\n";
                 }
                 //add reference if there's an fec_id
                 if ($donation['fec_id']) {
                     $ref = new Reference();
                     $ref->object_model = 'Relationship';
                     $ref->object_id = $rel->id;
                     $ref->source = $this->fecImageBaseUrl . $donation['fec_id'];
                     $ref->name = 'FEC Filing ' . $donation['fec_id'];
                     $ref->save();
                 }
             }
             //remove old filings from the relationship
             foreach ($donations['old'] as $donation) {
                 if ($this->debugMode) {
                     print "- Deleting FEC filing: {$donation['fec_id']}, {$donation['cycle']}, {$donation['row_id']} ({$donation['amount']})\n";
                 }
                 $sql = 'DELETE FROM fec_filing WHERE relationship_id = ? AND crp_cycle = ? AND crp_id = ?';
                 $stmt = $this->db->execute($sql, array($rel->id, $donation['cycle'], $donation['row_id']));
             }
             //recompute fields based on filings
             if (!$rel->updateFromFecFilings()) {
                 if ($this->debugMode) {
                     print "- Deleting donation relationship with no filings: " . $rel->id . "\n";
                 }
                 //no remaining filings for this relationship, so delete it!
                 $rel->delete();
             } else {
                 if ($this->debugMode) {
                     print "Relationship " . $rel->id . " updated with " . $rel->filings . " filings totaling " . $rel->amount . "\n";
                 }
                 //add a reference to OS donation search for the relationship, if necessary
                 $sql = 'SELECT COUNT(*) FROM reference ' . 'WHERE object_model = ? AND object_id = ? AND name = ?';
                 $stmt = $this->db->execute($sql, array('Relationship', $rel->id, 'FEC contribution search'));
                 if (!$stmt->fetch(PDO::FETCH_COLUMN)) {
                     $ref = new Reference();
                     $ref->object_model = 'Relationship';
                     $ref->object_id = $rel->id;
                     $ref->source = sprintf($this->fecSearchUrlPattern, strtoupper($donorPerson['name_last']), strtoupper($donorPerson['name_first']));
                     $ref->name = 'FEC contribution search';
                     $ref->save();
                     if ($this->debugMode) {
                         print "+ Added reference to FEC contribution search\n";
                     }
                 }
             }
             //clear cache for recipient
             LsCache::clearEntityCacheById($recipientEntity['id']);
         }
     }
     //update os_entity_transaction
     $sql = 'UPDATE os_entity_transaction SET is_processed = is_verified, is_synced = 1 WHERE entity_id = ?';
     $stmt = $this->db->execute($sql, array($id));
     //make sure that all removed matches result in deleted fec filings and updated relationships for this entity
     $this->cleanupFecFilings($id, $oldDonations);
     //update opensecrets categories based on matched donations
     $this->printDebug("Updating industry categories based on matched donations...");
     $newCategories = OsPerson::updateCategories($id);
     foreach ($newCategories as $categoryId) {
         $this->printDebug("+ Added industry category: " . $categoryId);
     }
     //clear cache for donor
     LsCache::clearEntityCacheById($id);
 }