php orm框架如何实现,ORM框架与简单代码实现

[php]代码库<?php

abstract class Model{

protected $pk = 'id';

protected $_ID = null;

protected $_tableName;

protected $_arRelationMap;

protected $_modifyMap;

protected $is_load = false;

protected $_blForDeletion;

protected $_DB;

public function __consturct($id = null){

$this->_DB = mysql_connect('127.0.0.1','root','') ;

$this->_tableName = $this->getTableName();

$this->_arRelationMap = $this->getRelationMap();

if(isset($id))$this->_ID = $id;

}

abstract protected function getTableName();

abstract protected function getRelationMap();

public function Load(){

if(isset($this->_ID)){

$sql = "SELECT ";

foreach($this->_arRelationMap as $k => $v){

$sql .= '`'.$k.'`,';

}

$sql .= substr($sql,0,strlen($sql)-1);

$sql .= "FROM ".$this->_tableName." WHERE ".$this->pk." = ".$this->_ID;

$result =$this->_DB->mysql_query($sql);

foreach($result[0] as $k1 => $v1){

$member = $this->_arRelationMap[$key];

if(property_exists($this,$member)){

if(is_numeric($member)){

eval('$this->'.$member.' = '.$value.';');

}else{

eval('$this->'.$member.' = "'.$value.'";');

}

}

}

}

$this->is_load = true;

}

public function __call($method,$param){

$type = substr($method,0,3);

$member = substr($method,3);

switch($type){

case 'get':

return $this->getMember($member);

break;

case 'set':

return $this->setMember($member,$param[0]);

}

return false;

}

public function setMember($key){

if(property_exists($this,$key)){

if(is_numeric($val)){

eval('$this->'.$key.' = '.$val.';');

}else{

eval('$this->'.$key.' = "'.$val.'";');

}

$this->_modifyMap[$key] = 1;

}else{

return false;

}

}

public function getMember($key,$val){

if(!$this->is_load){

$this->Load();

}

if(property_exists($this,$key)){

eval('$res = $this->'.$key.';' );

return $this->$key;

}

return false;

}

public function save(){

if(isset($this->_ID)){

$sql = "UPDATE ".$this->_tableName." SET ";

foreach($this->arRelationMap as $k2 => $v2){

if(array_key_exists( $k2, $this->_modifyMap)){

eval( '$val = $this->'.$v2.';');

$sql_update .= $v2." = ".$val;

}

}

$sql .= substr($sql_update,0,strlen($sql_update));

$sql .= 'WHERE '.$this->pk.' = '.$this->_ID;

}else{

$sql = "INSERT INTO ".$this->_tableName." (";

foreach($this->arRelationMap as $k3 => $v3){

if(array_key_exists( $k3,$this->_modifyMap)){

eval('$val = $this->'.$v3.';');

$field .= "`".$v3."`,";

$values .= $val;

}

}

$fields = substr($field,0,strlen($field)-1);

$vals = substr($values,0,strlen($values)-1);

$sql .= $fields." ) VALUES (".$vals.")";

}

echo $sql;

//$this->_DB->query($sql);

}

public function __destory(){

if(isset($this->ID)){

$sql = "DELETE FROM ".$this->_tableName." WHERE ".$this->pk." = ".$this->_ID;

// $this->_DB_query($sql);

}

}

}

class User extends Model{

protected function getTableName(){

return "test_user";

}

protected function getRelationMap(){

return array(

'id' => USER_ID,

'user_name'=> USER_NAME,

'user_age' => USER_AGE

);

}

public function getDB(){

return $this->_DB;

}

}

$UserIns = new User();

print_r($UserIns);

?>

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值