PHP-MVC框架搭建之数据库操作类和公共模型类文件

  介绍完公共控制器类之后,我们的控制器继承公共控制器来进行业务操作,这时就需要模型类来进行对数据库的操作,这需要dao数据库操作类和公共模型类让模型类继承来实现这一操作。

  数据库操作类dao,其实就是利用pdo进行二次封装,从而实现对数据库的操作。

  

<?php 
	namespace core;
	use \PDO;
    use \PDOstatement;
    use \PDOexception;

class Dao{

	//属性
	private $dao;
	private $fetch_model;

	//构造函数,实例化pdo对象
	public function __construct($info = array(),$drives = array()){
		$type = $info['type'] ? $info['type'] : 'mysql';
		$host = $info['host'] ? $info['host'] :'localhost';
		$port = $info['port'] ? $info['port']: '3306';
		$dbname = $info['dbname'] ? $info['dbname'] : 'test';
		$user = $info['user'] ? $info['user'] : 'root';
		$password = $info['password'] ? $info['password'] : '123456';
		$drives[PDO::ATTR_ERRMODE] = $drives[PDO::ATTR_ERRMODE] ? $drives[PDO::ATTR_ERRMODE] :  PDO::ERRMODE_EXCEPTION;
		$charset = $info['charset'] ? $charset = $info['charset'] : 'utf8';

		$this->fetch_model = $info['fetch_model'] ? $info['fetch_model'] : PDO::FETCH_ASSOC;



	//连接数据库
		try {
			$this->dao = @new pdo("$type:host=$host;port=$port;dbname=$dbname","$user","$password",$drives);
		} catch (pdoException $e) {
			echo '数据库连接错误';
			echo '<br />';
			echo  '错误文件为 ' . $e->getFile();
			echo '<br />';
			echo  '错误行数为 ' . $e->getLine();
			echo '<br />';
			echo  '错误行数为 ' . $e->getMessage();
			die();
		}

	//设置字符集
		try {
			$this->dao->exec("set names {$charset}");
		} catch (pdoException $e) {
			$this->pdo_exception($e);
		}
	}

	//异常处理函数
	private function pdo_exception(pdoException $e){
		echo 'SQL执行错误';
		echo '<br />';
		echo  '错误文件为 ' . $e->getFile();
		echo '<br />';
		echo  '错误行数为 ' . $e->getLine();
		echo '<br />';
		echo  '错误行数为 ' . $e->getMessage();
		die();
	}


	//写操作
	public function exec($sql){
		try {
			return $this->dao->exec($sql);
		} catch (pdoException $e) {
			$this->pdo_exception($e);
		}
	}

	//读操作
	public function query($sql,$all = true){
		try {
			$stmt = $this->dao->query($sql);

			if($all == true){
				$res = $stmt->fetchAll($this->fetch_model);
				//if(!$res) throw new pdoexception('当前未查询到数据');
				return $res;
			}
			else{
				$res = $stmt->fetch($this->fetch_model);
				//if(!$res) throw new pdoexception('当前未查询到数据');
				return $res;
			}
		} catch (pdoException $e) {
			$this->pdo_exception($e);
		}
	}

}

 ?>

 

  然后公共模型类对dao类创建实例化对象。

<?php 
//定义命名空间
namespace core;

class model{
	//属性
	//保存dao对象
	protected $dao;
	protected $table;
	protected $fields;

	//实例化方法
	public  function __construct(){
		//加载配置文件
		global $config;

		$this->dao = new Dao($config['database'],$config['drivers']);
	}

	//数据库写操作
	public function exec($sql){
		return $this->dao->exec($sql);
	}

	//数据库读操作
	public  function query($sql){
		return $this->dao->query($sql);
	}

	//构造全表名
	protected function getTable(string $table){
		//配置
		global $config;

		//确定表名
		$table = empty($table) ? $this->table : $table;

		//构造表名
		return $config['database']['prefix'] . $table;

	}

	//获取表字段
	private function get_Fields(){
		//通过desc获取表字段
		$sql = "desc {$this->getTable()}";

		$rows = $this->query($sql);

		//遍历,选出字段和主键
		foreach ($rows as $row) {
			$this->fields[] = $row['Field'];

			//保存主键
			if($row['Key'] == 'PRI')
			$this->fields['key'] = $row['Field'];
		}

	}


	//通过主键获取记录
	public function getByID($id){
		//判定主键是否存在
		if(!isset($this->fields['key'])) return false;

		$sql = "select * from {$this->getTable()} where {$this->fields['key']} = $id";

		return $this->query($sql);
	}






}




 ?>

  最后我们来写一个测试IndexModel.php,测试基本流程能否走通,功能是否能实现。

<?php 
namespace home\model;
use \core\model;

class IndexModel extends Model{

    //属性
    protected $table = 'student';


}
 ?>

然后在IndexControll.php中实例化调用。

<?php 

//定义命名空间
namespace home\controller;
use \core\controller;


class IndexController extends controller {
  	//默认方法
  	public function index(){
  		//var_dump($this->smarty);
  		echo '欢迎使用MVC<br />';

  		//$this->success('Welcome to our system,please to login in first','login');

  		$index = new \home\model\IndexModel();
  		//var_dump($index);
  		$res = $index->query('select * from student');
        echo '<pre>';
  		var_dump($res);

  	}

  	//登陆方法
  	public function login(){
  		echo 'Login in successfully';
  	}
}

 ?>

 来看看结果。

  这样我们就完成了一个MVC框架的基本搭建,希望可以帮到大家,谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值