模块

Model_Forum_Post
继承自 Model

论坛帖子模型

package
BootCMS
category
论坛/模型
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 APPPATH/classes/model/forum/post.php 第 13 行声明。

属性

protected $_db

private $_loaded

private $_values

方法

public create( array $data ) (在 Model_Forum_Post 中定义)

创建新节点

参数

  • array $data required - 键值对

返回值

  • mixed - 插入的数据ID 或 false

源代码

public function create($data)
{
	if (is_object($data))
	{
		// 对象转数组(将来将 Validation 改写成操作对象后,就可以省略此步)
		$values = array();
		foreach ($data as $key => $val)
		{
			$values[$key] = $val;
		}
		// 验证数据
		$validation = self::getValidation($values);
		if (!$validation->check())
		{
			throw new Validation_Exception('user', $validation);
		}
		return $this->_db->insert('bb_posts', $values);
	}
	return false;
}

public delete( ) (在 Model_Forum_Post 中定义)

删除节点

返回值

  • mixed - 执行结果

源代码

public function delete()
{
	if ($this->_loaded)
	{
		$where = "`id` = {$this->_values->id}";
		return $this->_db->delete('bb_posts', $where);
	}
	return false;
}

public static factory( ) (在 Model_Forum_Post 中定义)

创建并返回一个新的模型对象

返回值

  • 对象

源代码

public static function factory($name)
{
	return parent::factory($name);
}

public findByTopic( [ integer $topicId = integer 0 , integer $select = string(0) "" , integer $start = integer 0 , integer $limit = integer 10 ] ) (在 Model_Forum_Post 中定义)

根据主题ID取得帖子信息

参数

  • integer $topicId = integer 0 - 主题ID
  • integer $select = string(0) "" - 额外字段
  • integer $start = integer 0 - 开始
  • integer $limit = integer 10 - 数量

返回值

  • array - 数据

源代码

public function findByTopic($topicId = 0, $select = '', $start = 0, $limit = 10)
{
	$result = array();
	if ($topicId)
	{
		$sql = "SELECT p.id, p.poster_id, p.poster_ip, p.content, p.post_time, p.enable_bbcode, p.enable_smilies, p.enable_html, p.edited_time, p.editor_id, p.editor_name,
		m.nickname, m.level, m.posts, m.regdate" . $select . "
		FROM `{$this->_db->tablePrefix}bb_posts` p
		LEFT JOIN `{$this->_db->tablePrefix}bb_members` m ON p.poster_id = m.user_id
		WHERE p.topic_id = $topicId ORDER BY p.post_time ASC LIMIT $start, $limit";
		$result = $this->_db->selectArray($sql);
	}
	return $result;
}

public getContentById( [ integer $postId = integer 0 , integer $topicId = integer 0 ] ) (在 Model_Forum_Post 中定义)

根据主题ID取得最后一个帖子的ID

参数

  • integer $postId = integer 0 - 帖子ID
  • integer $topicId = integer 0 - 主题ID

返回值

  • object - 帖子内容

源代码

public function getContentById($postId = 0, $topicId = 0)
{
	$sql = "SELECT p.id, p.content, u.nickname FROM `{$this->_db->tablePrefix}bb_posts` p LEFT JOIN `{$this->_db->tablePrefix}bb_members` u ON p.poster_id = u.user_id WHERE p.id = $postId AND p.topic_id = '$topicId'";
	$post = $this->_db->select($sql);
	return $post;
}

public getFirstPostId( [ integer $topicId = integer 0 ] ) (在 Model_Forum_Post 中定义)

根据主题ID取得第一个帖子的ID

参数

  • integer $topicId = integer 0 - 主题ID

返回值

  • object - 帖子ID

源代码

public function getFirstPostId($topicId = 0)
{
	$sql = "SELECT id FROM `{$this->_db->tablePrefix}bb_posts` WHERE topic_id = $topicId ORDER BY post_time ASC LIMIT 1";
	$post = $this->_db->select($sql);
	return $post ? $post->id : 0;
}

public getLastPostId( [ integer $topicId = integer 0 ] ) (在 Model_Forum_Post 中定义)

根据主题ID取得最后一个帖子的ID

参数

  • integer $topicId = integer 0 - 主题ID

返回值

  • object - 帖子ID

源代码

public function getLastPostId($topicId = 0)
{
	$sql = "SELECT id FROM `{$this->_db->tablePrefix}bb_posts` WHERE topic_id = $topicId ORDER BY post_time DESC LIMIT 1";
	$post = $this->_db->select($sql);
	return $post ? $post->id : 0;
}

public static getValidation( array $values ) (在 Model_Forum_Post 中定义)

数据验证

参数

  • array $values required - $values

返回值

  • Validation

源代码

public static function getValidation($values)
{
	return Validation::factory($values)
			->rule('topic_id', 'not_empty')
			->rule('topic_id', 'digit')
			->rule('poster_id', 'not_empty')
			->rule('poster_id', 'digit')
			->rule('content', 'not_empty');
}

public load( ) (在 Model_Forum_Post 中定义)

根据主键加载数据,并返回对象

返回值

  • 对象

源代码

public function load($id = 0)
{
	if (is_numeric($id) && $id > 0)
	{
		$sql = "SELECT * FROM `{$this->_db->tablePrefix}bb_posts` WHERE `id` = $id";
		$this->_values = $this->_db->select($sql);
		$this->_loaded = true;
	}
	return $this->_values;
}

public loadWithTopic( ) (在 Model_Forum_Post 中定义)

根据主键加载数据,并返回对象

返回值

  • 对象

源代码

public function loadWithTopic($id = 0)
{
	if (is_numeric($id) && $id > 0)
	{
		$sql = "SELECT p.*, t.id AS topic_id, t.forum_id, t.topic_title, f.auth FROM `{$this->_db->tablePrefix}bb_posts` p LEFT JOIN `" . $this->_db->tablePrefix . "bb_topics` t ON p.topic_id = t.id LEFT JOIN `" . $this->_db->tablePrefix . "bb_forums` f ON t.forum_id = f.id WHERE p.id = $id";
		$this->_values = $this->_db->select($sql);
		$this->_loaded = true;
	}
	return $this->_values;
}

public update( ) (在 Model_Forum_Post 中定义)

更新节点

返回值

  • 执行结果

源代码

public function update()
{
	if ($this->_loaded)
	{
		// 对象转数组(将来将 Validation 改写成操作对象后,就可以省略此步)
		$values = array();
		foreach ($this->_values as $key => $val)
		{
			$values[$key] = $val;
		}
		// 验证数据
		$validation = self::getValidation($values);
		if (!$validation->check())
		{
			throw new Validation_Exception('user', $validation);
		}
		$where = "`id` = {$this->_values->id}";
		return $this->_db->update('bb_posts', $this->_values, $where);
	}
	return false;
}

public __construct( ) (在 Model 中定义)

源代码

public function __construct()
{
	if (!is_object($this->_db))
	{
		$this->_db = Database::instance();
	}
}