数据对象映射模式 将对象和数据存储起来,对一个对象的操作会映射为对数据存储的操作
在代码中实现数据库映射模式,可以将复杂的SQL语句映射成对象属性的操作
伪代码,当代码执行完毕后会调用析构函数进行数据库的更新操作
<?php
class DataYingshe
{
private $_db;
private $_id;
public $name;
public $age;
public function __construct($id)
{
$this->_id = $id;
$this->_db = Db1::getInstance();
$this->_db->connect('127.0.0.1', '3306', 'root', 'root', 'test');
$data = $this->_db->query('select name,age from user where id = '. $id);
$this->name = $data['name'];
$this->age = $data['age'];
}
public function __destruct()
{
$this->_db->query("update user set name = {$this->name},age = {$this->age} where id = {$this->_id}");
}
}
$yingshe = new DataYingshe(1);
$yingshe->name = 'shui';
$yingshe->age = 12;