/** * Transfers issues and history from another ticket into this one * * We're only migrating the issue and history * Once we're done we delete the other ticket * * @param Ticket $ticket */ public function mergeFrom(Ticket $ticket) { if ($this->getId()) { $zend_db = Database::getConnection(); $zend_db->getDriver()->getConnection()->beginTransaction(); try { $zend_db->query('update ticketHistory set ticket_id=? where ticket_id=?')->execute([$this->getId(), $ticket->getId()]); $zend_db->query('update issues set ticket_id=? where ticket_id=?')->execute([$this->getId(), $ticket->getId()]); $zend_db->query('delete from tickets where id=?')->execute([$ticket->getId()]); } catch (\Exception $e) { $zend_db->getDriver()->getConnection()->rollback(); throw $e; } $zend_db->getDriver()->getConnection()->commit(); $search = new Search(); $search->delete($ticket); $search->add($this); $search->solrClient->commit(); } }
<?php /** * @copyright 2012-2014 City of Bloomington, Indiana * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt * @author Cliff Ingham <*****@*****.**> */ use Application\Models\Search; use Application\Models\Ticket; use Blossom\Classes\Database; include '../../configuration.inc'; $search = new Search(); $search->solrClient->deleteByQuery('*:*'); $search->solrClient->commit(); $sql = 'select * from tickets'; $zend_db = Database::getConnection(); $result = $zend_db->query($sql)->execute(); $count = count($result); foreach ($result as $c => $row) { $ticket = new Ticket($row); $search->add($ticket); echo "{$c}/{$count}: {$ticket->getId()}\n"; } echo "Committing\n"; $search->solrClient->commit(); echo "Optimizing\n"; $search->solrClient->optimize(); echo "Done\n";
/** * Transfers all data from a person, then deletes that person * * This person will end up containing all information from both people * I took care to make sure to update the search index as well * as the database. * * @param Person $person */ public function mergeFrom(Person $person) { if ($this->getId() && $person->getId()) { if ($this->getId() == $person->getId()) { // can not merge same person throw exception throw new \Exception('mergerNotAllowed'); } $zend_db = Database::getConnection(); // Look up all the tickets we're about to modify // We need to remember them so we can update the search // index after we've updated the database $id = (int) $person->getId(); $sql = "select distinct t.id from tickets t\n\t\t\t\t\tleft join ticketHistory th on t.id=th.ticket_id\n\t\t\t\t\tleft join issues i on t.id= i.ticket_id\n\t\t\t\t\tleft join issueHistory ih on i.id=ih.issue_id\n\t\t\t\t\tleft join media m on i.id= m.issue_id\n\t\t\t\t\tleft join responses r on i.id= r.issue_id\n\t\t\t\t\twhere ( t.enteredByPerson_id={$id} or t.assignedPerson_id={$id} or t.referredPerson_id={$id})\n\t\t\t\t\t or (th.enteredByPerson_id={$id} or th.actionPerson_id={$id})\n\t\t\t\t\t or ( i.enteredByPerson_id={$id} or i.reportedByPerson_id={$id})\n\t\t\t\t\t or (ih.enteredByPerson_id={$id} or ih.actionPerson_id={$id})\n\t\t\t\t\t or m.person_id={$id} or r.person_id={$id}"; $result = $zend_db->query($sql)->execute(); $ticketIds = []; foreach ($result as $row) { $ticketIds[] = $row['id']; } $zend_db->getDriver()->getConnection()->beginTransaction(); try { // These are all the database fields that hit the Solr index $zend_db->query('update responses set person_id=? where person_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update media set person_id=? where person_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update issueHistory set enteredByPerson_id=? where enteredByPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update issueHistory set actionPerson_id=? where actionPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update issues set enteredByPerson_id=? where enteredByPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update issues set reportedByPerson_id=? where reportedByPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update ticketHistory set enteredByPerson_id=? where enteredByPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update ticketHistory set actionPerson_id=? where actionPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update tickets set enteredByPerson_id=? where enteredByPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update tickets set assignedPerson_id=? where assignedPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update tickets set referredPerson_id=? where referredPerson_id=?')->execute([$this->getId(), $person->getId()]); // Fields that don't hit the Solr index $zend_db->query('update clients set contactPerson_id=? where contactPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update departments set defaultPerson_id=? where defaultPerson_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update peopleAddresses set person_id=? where person_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update peoplePhones set person_id=? where person_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('update peopleEmails set person_id=? where person_id=?')->execute([$this->getId(), $person->getId()]); $zend_db->query('delete from people where id=?')->execute([$person->getId()]); } catch (Exception $e) { $zend_db->getDriver()->getConnection()->rollback(); throw $e; } $zend_db->getDriver()->getConnection()->commit(); foreach ($ticketIds as $id) { $search = new Search(); $ticket = new Ticket($id); $search->add($ticket); } } }