/** * @method add a message to a thread. * @param $MSG the message * @param string subject of the message * @param string text/body of the message * @param date date of the message * @param number $threadId the thread id to add msg */ public function addMessage($subject, $text, $date, $threadId) { $this->pdo->beginTransaction(); $msgId = $this->getNextVal(self::$SEQ_MSG_ID); $sql = $this->getQuery('INSERT INTO ' . self::$SCHEMA . '.MSG ("ID", "Subject", "Text", "Date", "Thread_ID") VALUES (' . $msgId . ', ?, ?, ?, ' . $threadId . ')'); $stmt = $this->pdo->prepare($sql); $this->executeQueryRollbackOnException($stmt, array($subject, $text, $date)); $sql = $this->getQuery('INSERT INTO ' . self::$SCHEMA . '.MSG_BOX ("MSG_ID", "To_User_ID", "From_User_ID", "Status") VALUES (' . $msgId . ', ?, ?, ?)'); $stmt = $this->pdo->prepare($sql); $this->executeQueryRollbackOnException($stmt, array($this->getConversationPartner($threadId), $this->userId, self::$NOT_READED)); $this->pdo->commit(); }
/** * 提交事务 */ public function commitTrans() { $this->pdo->commit(); }
<?php $db = new pdo('sqlite::memory:'); $db->beginTransaction(); $db->query('CREATE TABLE IF NOT EXISTS foobar (id INT AUTO INCREMENT, name TEXT)'); $db->commit(); $db->beginTransaction(); $db->query('INSERT INTO foobar VALUES (NULL, "PHP")'); $db->query('INSERT INTO foobar VALUES (NULL, "PHP6")'); $db->rollback(); $r = $db->query('SELECT COUNT(*) FROM foobar'); var_dump($r->rowCount()); $db->query('DROP TABLE foobar');