Fix bug with empty threads

This commit is contained in:
Dmitriy Simushev 2013-01-30 09:02:39 +00:00
parent 7e70d5b14b
commit a0161a2c72

View File

@ -169,50 +169,37 @@ Class Thread {
protected $updatedFields = array(); protected $updatedFields = array();
/** /**
* Load thread from database or create a new one in the database if $id is empty. * Forbid create instance from outside of the class
*
* @param int $id ID of the thread to load
*/ */
protected function __construct($id = null) { protected function __construct() {}
// Get database object
$db = Database::getInstance();
if (! empty($id)) {
// Load thread
$thread_info = $db->query(
"select * from {chatthread} where threadid = :threadid",
array(
':threadid' => $id
),
array('return_rows' => Database::RETURN_ONE_ROW)
);
if (! $thread_info) {
return;
}
$this->threadInfo = $thread_info;
} else {
// Create thread
$db->query("insert into {chatthread} (threadid) values (NULL)");
// Set initial values
// In this case Thread::$threadInfo array use because id of a thread should not be update
$this->threadInfo['threadid'] = $db->insertedId();
}
}
/** /**
* Create new empty thread in database * Create new empty thread in database
* *
* @return boolean|Thread Returns an object of the Thread class or boolean false on failure * @return boolean|Thread Returns an object of the Thread class or boolean
* false on failure
*/ */
public static function create() { public static function create() {
// Get database object
$db = Database::getInstance();
// Create new empty thread // Create new empty thread
$thread = new self(); $thread = new self();
// Create thread
$db->query("insert into {chatthread} (threadid) values (NULL)");
// Set thread Id
// In this case Thread::$threadInfo array use because id of a thread
// should not be update
$thread->threadInfo['threadid'] = $db->insertedId();
// Check if something went wrong // Check if something went wrong
if (empty($thread->id)) { if (empty($thread->id)) {
return false; return false;
} }
// Set initial values
$thread->lastToken = self::nextToken(); $thread->lastToken = self::nextToken();
$thread->created = time(); $thread->created = time();
return $thread; return $thread;
@ -221,9 +208,11 @@ Class Thread {
/** /**
* Create thread object from database info. * Create thread object from database info.
* *
* @param array $thread_info Associative array of Thread info from database. It must contains ALL thread table's * @param array $thread_info Associative array of Thread info from database.
* It must contains ALL thread table's
* FIELDS from the database. * FIELDS from the database.
* @return boolean|Thread Returns an object of the Thread class or boolean false on failure * @return boolean|Thread Returns an object of the Thread class or boolean
* false on failure
*/ */
public static function createFromDbInfo($thread_info) { public static function createFromDbInfo($thread_info) {
// Create new empty thread // Create new empty thread
@ -247,7 +236,8 @@ Class Thread {
* Load thread from database * Load thread from database
* *
* @param int $id ID of the thread to load * @param int $id ID of the thread to load
* @return boolean|Thread Returns an object of the Thread class or boolean false on failure * @return boolean|Thread Returns an object of the Thread class or boolean
* false on failure
*/ */
public static function load($id, $last_token = null) { public static function load($id, $last_token = null) {
// Check $id // Check $id
@ -255,13 +245,34 @@ Class Thread {
return false; return false;
} }
// Get database object
$db = Database::getInstance();
// Create new empty thread
$thread = new self();
// Load thread // Load thread
$thread = new self($id); $thread_info = $db->query(
"select * from {chatthread} where threadid = :threadid",
array(
':threadid' => $id
),
array('return_rows' => Database::RETURN_ONE_ROW)
);
// There is no thread with such id in database
if (! $thread_info) {
return;
}
// Store thread properties
$thread->threadInfo = $thread_info;
// Check if something went wrong // Check if something went wrong
if ($thread->id != $id) { if ($thread->id != $id) {
return false; return false;
} }
// Check last token // Check last token
if (! is_null($last_token)) { if (! is_null($last_token)) {
if ($thread->lastToken != $last_token) { if ($thread->lastToken != $last_token) {