<?php
! defined ( 'P_W' ) && exit ( 'Forbidden' );
class PW_STopicCategoryDB extends BaseDB {
	
	var $_tableName = "pw_stopiccategory";
	
	function add($fieldData) {
		$fieldData = $this->_checkData ( $fieldData );
		if (! $fieldData)
			return null;
		$this->_db->update ( "INSERT INTO " . $this->_tableName . " SET " . $this->_getUpdateSqlString ( $fieldData ) );
		return $this->_db->insert_id ();
	}
	
	function update($fieldData, $id) {
		$fieldData = $this->_checkData ( $fieldData );
		if (! $fieldData)
			return null;
		$this->_db->update ( "UPDATE " . $this->_tableName . " SET " . $this->_getUpdateSqlString ( $fieldData ) . "WHERE id=" . $this->_addSlashes ( $id ) . " LIMIT 1" );
		return $this->_db->affected_rows ();
	}
	
	function delete($id) {
		$this->_db->update ( "DELETE FROM " . $this->_tableName . " WHERE id=" . $this->_addSlashes ( $id ) . " LIMIT 1" );
		return $this->_db->affected_rows ();
	}
	
	function get($id) {
		return $this->_db->get_one ( "SELECT * FROM " . $this->_tableName . " WHERE id=" . $this->_addSlashes ( $id ) . " LIMIT 1" );
	}
	
	function getByName($name) {
		return $this->_db->get_one ( "SELECT * FROM " . $this->_tableName . " WHERE title=" . $this->_addSlashes ( $name ) . " LIMIT 1" );
	}
	
	function gets() {
		$query = $this->_db->query ( "SELECT * FROM " . $this->_tableName . " " );
		return $this->_getAllResultFromQuery ( $query );
	}
	
	function count() {
		return $this->_db->get_value ( "SELECT COUNT(*) FROM " . $this->_tableName . " LIMIT 1" );
	}
	
	/**
	 * ·ÖÀàÔÊÐí×Ö¶Î
	 *
	 * @return array
	 */
	function getStruct() {
		return array ('id', 'title', 'status', 'num', 'creator', 'createtime' );
	}
	
	function _checkData($data) {
		if (! is_array ( $data ) || ! count ( $data ))
			Showmsg ( 'data_is_not_array' );
		$data = $this->_checkAllowField ( $data, $this->getStruct () );
		return $data;
	}
}
?>