PHP实体层基础类
class BaseModel { private $tableName; private $fields=array(); function __construct() { $this->tableName=rtrim(get_class($this),'M'); } function __get($n) { return($this->$n); } function __set($n, $value) { $this->$n = $value; $this->fields[$n]=$value; } function Add() { $f=''; $f1=''; $arrKeys=array_keys($this->fields); foreach($arrKeys as $row) { $f.=$row.','; $f1.=':'.$row.','; } $f=rtrim($f,','); $f1=rtrim($f1,','); $sql='insert into '.$this->tableName.'('.$f.') values('.$f1.')'; //echo $sql; global $dbh; return $dbh->edit($sql,$this->fields); } function Update($sql_where,$params=array()) { $f=''; $f1=''; $arrKeys=array_keys($this->fields); foreach($arrKeys as $row) { $f.=$row.'=:'.$row.','; } $f=rtrim($f,','); $sql='update '.$this->tableName.' set '.$f.' where '.$sql_where; $arrKeys1=array_keys($params); foreach($arrKeys1 as $row) { $this->fields[$row]=$params[$row]; } global $dbh; return $dbh->Edit($sql,$this->fields); } function Delete($sql_where,$params=array()) { $sql='delete from '.$this->tableName.' where '.$sql_where; //echo $sql; global $dbh; return $dbh->Edit($sql,$params); } }