php设计模式(五)数据对象映射模式
作者:
mafa1993 | 来源:发表于
2022-06-29 21:34 被阅读0次
数据对象映射模式 orm
- 对象和数据存储映射,对对象的操作映射为对数据的存储操作
// 映射到user表
class User {
public $id;
public $name;
public $regtime;
private $db;
public function __counstruct($id){
$this->db = (new Factory())->createDB);
$this->db->connect($host,$user,$pwd,$dbname); // 这里为工厂模式创建,可以改为这册器模式,进一步进行优化,例如一次业务中需要实例化这个类多次。 这里不能用单利模式,因为每个id应该为不同的实例
$rlt = $this->db->query("select * from user where id ='$id'")->fetchAll();
// 建立映射关系
$this->id = $rlt['id'];
$this->name = $rlt['name'];
$this->regtime = $rlt['regtime'];
}
public function __destruct(){
$this->db->exec("update user set name = $this->name regtime=$this->regtime where id = $id");
}
}
$user = new User(1); // 操作id为1的用户
$user->name="aa";
$user->regtiem = time();
本文标题:php设计模式(五)数据对象映射模式
本文链接:https://www.haomeiwen.com/subject/viexbrtx.html
网友评论