模块

Model_Node
继承自 Model

节点模型。

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

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

属性

protected $_db

private $_loaded

private $_values

方法

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

创建新节点

参数

  • 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('nodes', $values);
	}
	return false;
}

public createSlug( ) (在 Model_Node 中定义)

创建别名

返回值

  • string - 别名

源代码

public function createSlug($title)
{
	// 别名最大长度为 32
	$title = mb_substr(trim($title), 0, 32);
	$slug = $temp = Functions::toLink($title);
	$i = 0;
	do
	{
		$node = $this->loadBySlug($slug);
		if ($node->id)
		{
			$i++;
			$slug = $temp . $i;
		}
	}
	while ($node->id);
	return $slug;
}

public delete( ) (在 Model_Node 中定义)

删除节点

返回值

  • mixed - 执行结果

源代码

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

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

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

返回值

  • 对象

源代码

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

public findAll( ) (在 Model_Node 中定义)

取得所有单页

源代码

public function findAll($order_by = '')
{
	$result = array();
	$order_by = $order_by ? $order_by : 'created DESC';
	$sql = "SELECT * FROM `" . $this->_db->tablePrefix . "nodes` WHERE `type` = 1 ORDER BY $order_by";
	$result = $this->_db->selectArray($sql);
	return $result;
}

public findByType( ) (在 Model_Node 中定义)

取得所有主页

源代码

public function findByType($type)
{
	$result = NULL;
	if ($type)
	{
		$sql = "SELECT * FROM `" . $this->_db->tablePrefix . "nodes` WHERE `type` = $type AND `status` = 1";
		$result = $this->_db->select($sql);
	}
	return $result;
}

public getPagesByPage( [ string $where = string(0) "" , string $orderBy = string(0) "" , integer $start = integer 0 , integer $limit = integer 10 ] ) (在 Model_Node 中定义)

取得包括 Homepage 在内的所有单页

参数

  • string $where = string(0) "" - 查询条件
  • string $orderBy = string(0) "" - 排序方式
  • integer $start = integer 0 - 开始
  • integer $limit = integer 10 - 数量

返回值

  • array - 数据

源代码

public function getPagesByPage($where = '', $orderBy = '', $start = 0, $limit = 10)
{
	$orderBy = $orderBy ? $orderBy : 'n.id';
	$sql = "SELECT n.*, u.username FROM `" . $this->_db->tablePrefix . "nodes` n LEFT JOIN `" . $this->_db->tablePrefix . "users` u ON n.author_id = u.id WHERE 1 $where ORDER BY $orderBy LIMIT $start, $limit";
	$result = $this->_db->selectArray($sql);
	$sql = "SELECT COUNT(*) AS num FROM `" . $this->_db->tablePrefix . "nodes` n LEFT JOIN `" . $this->_db->tablePrefix . "users` u ON n.author_id = u.id WHERE 1 $where";
	$total = $this->_db->select($sql)->num;
	return array($result, $total);
}

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

数据验证

参数

  • array $values required - $values

返回值

  • Validation

源代码

public static function getValidation($values)
{
	return Validation::factory($values)
			->rule('slug', 'not_empty')
			->rule('slug', 'max_length', array(':value', 32))
			->rule('node_title', 'not_empty')
			->rule('node_title', 'max_length', array(':value', 60))
			->rule('node_content', 'not_empty')
			->rule('keywords', 'max_length', array(':value', 120))
			->rule('descript', 'max_length', array(':value', 250));
}

public load( ) (在 Model_Node 中定义)

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

返回值

  • 对象

源代码

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

public loadBySlug( ) (在 Model_Node 中定义)

源代码

public function loadBySlug($slug)
{
	$result = NULL;
	if ($slug)
	{
		$sql = "SELECT * FROM `" . $this->_db->tablePrefix . "nodes` WHERE `slug` = '$slug' AND `status` = 1";
		$result = $this->_db->select($sql);
	}
	return $result;
}

搜索单页

参数

  • string $keyword required - 搜索关键字
  • unknown $start = integer 0
  • unknown $limit = integer 10

返回值

  • array - 结果数组

源代码

public function search($keyword, $start = 0, $limit = 10)
{
	$result = array();
	$totla = 0;
	if ($keyword)
	{
		$sql = "SELECT * FROM `" . $this->_db->tablePrefix . "nodes` WHERE ( `type` = 1 OR `type` = 2 ) AND `status` = 1 AND (`node_title` LIKE '%$keyword%' OR `node_content` LIKE '%$keyword%') LIMIT $start, $limit";
		$result = $this->_db->selectArray($sql);
		$sql = "SELECT COUNT(*) AS num FROM `" . $this->_db->tablePrefix . "nodes` WHERE ( `type` = 1 OR `type` = 2 ) AND `status` = 1 AND (`node_title` LIKE '%$keyword%' OR `node_content` LIKE '%$keyword%')";
		$total = $this->_db->select($sql)->num;
	}
	return array($result, $total);
}

public setHomepage( [ string $nodeId = integer 0 ] ) (在 Model_Node 中定义)

设置首页

参数

  • string $nodeId = integer 0 - 搜索关键字

返回值

  • array - 结果数组

源代码

public function setHomepage($nodeId = 0)
{
	if (intval($nodeId) > 0)
	{
		$set = array('type' => 1);
		$where = "`type` = 2";
		$this->_db->update('nodes', $set, $where);
		$set = array('type' => 2);
		$where = "`id` = $nodeId AND `type` = 1";
		return $this->_db->update('nodes', $set, $where);
	}
	return false;
}

public update( ) (在 Model_Node 中定义)

更新节点

返回值

  • 执行结果

源代码

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('nodes', $this->_values, $where);
	}
	return false;
}

public __construct( ) (在 Model 中定义)

源代码

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