1. 程式人生 > >PHP之Model類封裝(PDO)

PHP之Model類封裝(PDO)

PHP之Model類封裝(PDO)

<?php
/**
 * 對資料庫的增刪改查
 */
class Model extends PDO
{
	protected $tabName = ''; //儲存表名
	protected $sql = '';//儲存最後執行的sql語句
	protected $limit = '';//儲存limit條件
	protected $order = '';//儲存order排序條件
	protected $field = '*';//儲存要查詢的欄位
	protected $where = ''; //儲存where條件
	protected $allFields = [];//儲存當前表的所有欄位

	/**
	 * 構造方法
	 * @param string $tabName 要操作表名
	 */
	public function __construct($tabName)
	{
		//連線資料庫
		parent::__construct('mysql:host='.HOST.';dbname='.DB.';charset=utf8;port='.PORT, USER, PWD);

		//儲存表名
		$this->tabName = PRE.$tabName;

		//獲取當前資料表中有哪些欄位
		$this->getFields();
	}

	protected function getFields()
	{
		$sql = "desc {$this->tabName}";
		$stmt = $this->query($sql);
		if ($stmt) {
			$arr = $stmt->fetchAll(2);
			//從二維陣列中取出指定下標的列
			$this->allFields = array_column($arr, 'Field');
		} else {
			die('表名錯誤');
		}
	}

	/**
	 * 新增資料
	 * @param array $data 要新增的陣列
	 */
	public function add($data)
	{
		//過濾非法欄位:將資料表裡面木有的欄位幹掉
		foreach ($data as $k=>$v) {
			if (!in_array($k, $this->allFields)) {
				unset($data[$k]);
			}
		}

		//判斷是否全是非法欄位
		if (empty($data)) die('非法資料');
		
		$keys = join(',', array_keys($data));
		$vals = join("','", $data);
		$sql = "insert into {$this->tabName}({$keys}) values('{$vals}')";
		$this->sql = $sql;
		return $this->exec($sql);
	}

	/**
	 * 根據id刪除資料
	 * @param  int $id 要刪除的id
	 * @return int 返回受影響行數
	 */
	public function delete($id = false)
	{
		if ($id) {
			$where = 'where id='.$id;
		} else {
			$where = $this->where;
		}
		$sql = "delete from {$this->tabName} {$where}";
		return (int)$this->exec($sql);
	}

	/**
	 * 修改資料
	 * @param  array $data 要改的資料
	 * @return int    返回受影響的行數
	 */
	public function save($data)
	{
		$str = '';
		//過濾非法欄位
		foreach ($data as $k=>$v) {
			if ($k == 'id') {
				$this->where = 'where id='.$v;
				unset($data[$k]);
				continue;
			}
			if (in_array($k, $this->allFields)) {
				$str .= "`$k`='$v',";
			} else {
				unset($data[$k]);
			}
		}
		// echo $str;exit;
		//判斷是否全特麼是非法欄位
		if (empty($data)) {
			die('全是非法欄位');
		}
		//判斷是否傳了條件
		if (empty($this->where)) {
			die('請傳入修改條件');
		}

		//去除右邊的,
		$str = rtrim($str, ',');

		$sql = "update {$this->tabName} set $str {$this->where}";

		$this->sql = $sql;
		return (int)$this->exec($sql);
	}

	/**
	 * 查詢並返回二維陣列
	 * @return array 查到了返回二維陣列,沒查到返回空陣列
	 */
	public function select()
	{
		$sql = "select {$this->field} from {$this->tabName} {$this->where} {$this->order} {$this->limit}";
		$this->sql = $sql;
		//傳送查詢sql
		$stmt = $this->query($sql);
		if ($stmt) {
			return $stmt->fetchAll(2);
		}

		return [];
	}

	/**
	 * 查詢並返回1條資料的一維陣列
	 * @param  int $id 要查詢的id
	 * @return array   返回查到的資料
	 */
	public function find($id)
	{
		$sql = "select {$this->field} from {$this->tabName} where id={$id} limit 1";
		$this->sql = $sql;
		//傳送查詢sql
		$stmt = $this->query($sql);
		if ($stmt) {
			return $stmt->fetch(2);
		}

		return [];
	}

	/**
	 * 統計總條數
	 * @return int 返回查到的條數
	 */
	public function count()
	{
		$sql = "select count(*) from {$this->tabName} {$this->where} limit 1";
		$this->sql = $sql;
		//傳送查詢sql
		$stmt = $this->query($sql);
		if ($stmt) {
			return (int)$stmt->fetch()[0];
		}

		return 0;
	}

	/**
	 * 獲取最後執行的sql語句
	 * @return string sql語句
	 */
	public function _sql()
	{

		return $this->sql;
	}

	/**
	 * 處理limit條件
	 * @param  string $str limit條件
	 * @return object 返回自己,保證連貫操作
	 */
	public function limit($str)
	{
		$this->limit = 'limit '.$str;
		return $this;
	}

	/**
	 * 處理order排序條件
	 * @param  string $str order條件
	 * @return object 返回自己,保證連貫操作
	 */
	public function order($str)
	{
		$this->order = 'order by '.$str;
		return $this;
	}

	/**
	 * 設定要查詢的欄位資訊
	 * @param  string $str 要查詢的欄位
	 * @return object 返回自己,保證連貫操作
	 */
	public function field($str)
	{
		$this->field = $str;
		return $this;
	}

	/**
	 * 設定where條件
	 * @param  string $str where條件
	 * @return object 返回自己,保證連貫操作
	 */
	public function where($str)
	{
		$this->where = 'where '.$str;
		return $this;
	}
}
?>