示例#1
0
 public function saveVote()
 {
     if (!$this->votable) {
         throw new Exception("Must set votable before saving vote");
     }
     if (!$this->user) {
         throw new Exception("Must set user before saving vote");
     }
     $user = $this->user;
     $votable = $this->votable;
     $newvote = $this->vote;
     // prepare vote add, or update if this user is amending an
     // earlier vote
     $editor = id(new PhabricatorEdgeEditor())->setUser($user)->addEdge($user->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID(), array('data' => $newvote))->removeEdge($user->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID());
     $conn = $votable->establishConnection('w');
     $trans = $conn->openTransaction();
     $trans->beginReadLocking();
     $votable->reload();
     $curvote = (int) PhabricatorEdgeQuery::loadSingleEdgeData($user->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID());
     if (!$curvote) {
         $curvote = PonderConstants::NONE_VOTE;
     }
     // adjust votable's score by this much
     $delta = $newvote - $curvote;
     queryfx($conn, 'UPDATE %T as t
     SET t.`voteCount` = t.`voteCount` + %d
     WHERE t.`PHID` = %s', $votable->getTableName(), $delta, $votable->getVotablePHID());
     $editor->save();
     $trans->endReadLocking();
     $trans->saveTransaction();
 }
示例#2
0
 public function saveVote()
 {
     $actor = $this->requireActor();
     if (!$this->votable) {
         throw new PhutilInvalidStateException('setVotable');
     }
     $votable = $this->votable;
     $newvote = $this->vote;
     // prepare vote add, or update if this user is amending an
     // earlier vote
     $editor = id(new PhabricatorEdgeEditor())->addEdge($actor->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID(), array('data' => $newvote))->removeEdge($actor->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID());
     $conn = $votable->establishConnection('w');
     $trans = $conn->openTransaction();
     $trans->beginReadLocking();
     $votable->reload();
     $curvote = (int) PhabricatorEdgeQuery::loadSingleEdgeData($actor->getPHID(), $votable->getUserVoteEdgeType(), $votable->getVotablePHID());
     if (!$curvote) {
         $curvote = PonderVote::VOTE_NONE;
     }
     // Adjust votable's score by this much.
     $delta = $newvote - $curvote;
     queryfx($conn, 'UPDATE %T as t
     SET t.voteCount = t.voteCount + %d
     WHERE t.PHID = %s', $votable->getTableName(), $delta, $votable->getVotablePHID());
     $editor->save();
     $trans->endReadLocking();
     $trans->saveTransaction();
 }