一、实现功能,数据表前缀、alias、model、table、join方法实现
文章来源:https://www.toymoban.com/news/detail-692245.html
文章来源地址https://www.toymoban.com/news/detail-692245.html
二、表前缀实现
1、config.php增加表前缀
'DB_PEX'=>'fa_',//数据库前缀
2、增加表前缀方法function.php
function model($table){
$model=new ModelBase($table,config("DB_PEX"));
return $model;
}
function table($table){
return new ModelBase($table);
}
3、PDO数据表增加表前缀ModelBase.php
private $pex="";//表前缀
public function __construct($table=null,$pex=""){
$this->pex=$pex;
if($table){
$this->table=$this->pex.$table;
}
if(!$this->table){
die("no table" );
}
$this->_connect();
$this->_opt();
}
三、alias数据表别名和join实现实现ModelBase.php
private function _opt(){
$this->opt=array(
'filed'=>'*',
'where'=>'',
'group'=>'',
'having'=>'',
'order'=>'',
'limit'=>'',
'alias'=>'',
'join'=>'',
);
}
public function alias($as){
$this->opt['alias']= ' as '.$as." ";
return $this;
}
public function join($join,$condition,$type=''){
$this->opt['join']= " {$type} join ".$this->pex.trim($join)." on {$condition}";
return $this;
}
public function select()
{
$sql = "SELECT ".$this->opt['filed']. " FROM ".$this->table.$this->opt['alias'].$this->opt['join'].$this->opt['where']
.$this->opt['group'].$this->opt['having'].$this->opt['order'].$this->opt['limit'];
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
四、控制器实现,数据表前缀、alias、model、table、join方法查询
public function index(){
$data= model("test")
->filed('t.*,r.role')
->alias('t')
->join('role r','t.id=r.testId','left')
->select();
var_dump($data);
$data= table("fa_test")->select();
var_dump($data);
}
五、完整ModelBase.php代码
<?php
class ModelBase
{
public $pdo = NULL;
public $table = NULL;
public $opt;
private $pex="";//表前缀
public function __construct($table=null,$pex=""){
$this->pex=$pex;
if($table){
$this->table=$this->pex.$table;
}
if(!$this->table){
die("no table" );
}
$this->_connect();
$this->_opt();
}
private function _connect(){
if($this->pdo){
return true;
}
$host = config('DB_HOST');
$db = config('DB_DATABASE');
$user = config('DB_USER');
$pass =config('DB_PWD');
$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
try {
$this->pdo = new PDO($dsn, $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
}
private function _opt(){
$this->opt=array(
'filed'=>'*',
'where'=>'',
'group'=>'',
'having'=>'',
'order'=>'',
'limit'=>'',
'alias'=>'',
'join'=>'',
);
}
public function alias($as){
$this->opt['alias']= ' as '.$as." ";
return $this;
}
public function join($join,$condition,$type=''){
$this->opt['join']= " {$type} join ".$this->pex.trim($join)." on {$condition}";
return $this;
}
public function where($where){
$this->opt['where']= ' WHERE '.$where;
return $this;
}
public function order($order){
$this->opt['order']= ' ORDER BY '.$order;
return $this;
}
public function group($group){
$this->opt['group']= ' GROUP BY '.$group;
return $this;
}
public function having($having){
$this->opt['having']= ' having '.$having;
return $this;
}
public function filed($filed){
$this->opt['filed']= $filed;
return $this;
}
public function limit($limit){
$this->opt['limit']= ' limit '.$limit;
return $this;
}
public function select()
{
$sql = "SELECT ".$this->opt['filed']. " FROM ".$this->table.$this->opt['alias'].$this->opt['join'].$this->opt['where']
.$this->opt['group'].$this->opt['having'].$this->opt['order'].$this->opt['limit'];
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function find(){
$data=$this->limit(1)->select();
return current($data);
}
public function delete()
{
$sql = "DELETE FROM ".$this->table.$this->opt['where'];
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
public function insert( $data) {
// 准备SQL语句
$fields = implode(', ', array_keys($data));
$values = ':' . implode(', :', array_keys($data));
$sql = "INSERT INTO ". $this->table." (".$fields.") VALUES (".$values.")";
// 绑定参数并执行SQL语句
$stmt = $this->pdo->prepare($sql);
foreach ($data as $key => $value) {
$stmt->bindValue(':' . $key, $value);
}
return $stmt->execute();
}
public function update( $data)
{
if(empty($this->opt['where'])) die('更新语句必须有were条件');
$up='';
foreach ($data as $k=>$v){
$up.="`".$k."`='".$v."'";
}
$sql="update ".$this->table.' set '.$up.$this->opt['where'];
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
}
到了这里,关于PHP自己的框架PDO数据表前缀、alias、model、table、join方法实现(完善篇九--结束)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!