This article mainly introduces the method of implementing persistence layer in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The example in this article describes how PHP implements the object persistence layer based on the MySQL database. The details are as follows:
On a whim, I made a simple persistence layer from PHP objects to the database.
I don’t often use PHP, and I am not familiar with PHP. Most of the content about PHP reflection is learned now.
The current function is relatively weak. It only completes some simple tasks. The relationship between objects cannot be mapped, and the members of the object can only support two types: string or integer.
The values of member variables are not escaped. . .
The code is posted below:
The first is the relevant definition of the database. This file defines the connection properties of the database:
<?php /* * Filename: config.php * Created on 2012-9-29 * Created by RobinTang * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ // About database define('DBHOST', 'localhost'); // 数据库服务器 define('DBNAME', 'db_wdid'); // 数据库名称 define('DBUSER', 'root'); // 登陆用户名 define('DBPSWD', 'trb'); // 登录密码 ?>
The following is a simple encapsulation of database access:
<?php /* * Filename: database.php * Created on 2012-9-29 * Created by RobinTang * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ include_once("config.php"); $debug = false; $g_out = false; function out($s){ global $g_out; $g_out .= $s; $g_out .= "\r\n"; } function db_openconnect(){ $con = mysql_connect(DBHOST, DBUSER, DBPSWD); if(!mysql_set_charset("utf8", $con)){ out("set mysql encoding fail"); } if (!$con){ out('Could not connect: ' . mysql_error()); } else{ if(!mysql_select_db(DBNAME, $con)){ $dbn = DBNAME; out("Could select database '$dbn' : " . mysql_error()); } $sql = "set time_zone = '+8:00';"; if(!db_onlyquery($sql, $con)){ out("select timezone fail!" . mysql_error()); } } return $con; } function db_colseconnect($con){ mysql_close($con); } function db_onlyquery($sql, $con){ $r = mysql_query($sql, $con); if(!$r){ out("query '$sql' :fail"); return false; } else{ return $r; } } function db_query($sql){ $con = db_openconnect(); $r = db_onlyquery($sql, $con); $res = false; if($r){ $res = true; } db_colseconnect($con); return $r; } function db_query_effect_rows($sql){ $con = db_openconnect(); $r = db_onlyquery($sql, $con); $res = false; if($r){ $res = mysql_affected_rows($con); if($res==0){ $res = -1; } } else{ $res = false; } db_colseconnect($con); return $res; } function db_getresult($sql){ $con = db_openconnect(); $r = db_onlyquery($sql, $con); $res = false; if($r && $arr = mysql_fetch_row($r)){ $res = $arr[0]; } db_colseconnect($con); return $res; } function db_getarray($sql){ $con = db_openconnect(); $r = db_onlyquery($sql, $con); $ret = false; if($r){ $row = false; $len = 0; $ret = Array(); $i = 0; while($arr = mysql_fetch_row($r)){ if($row == false || $len==0){ $row = Array(); $len = count($arr); for($i=0;$i<$len;++$i){ $key = mysql_field_name($r, $i); array_push($row, $key); } } $itm = Array(); for($i=0;$i<$len;++$i){ $itm[$row[$i]]=$arr[$i]; } array_push($ret, $itm); } } db_colseconnect($con); return $ret; } ?>
In fact, the two files above were written before, and the persistence layer is as follows:
<?php /* * Filename: sinorm.php * Created on 2012-11-4 * Created by RobinTang * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ include_once("database.php"); function SinORM_ExecSql($sql) { return db_query($sql); } function SinORM_ExecArray($sql) { return db_getarray($sql); } function SinORM_ExecResult($sql){ return db_getresult($sql); } function SinORM_GetClassPropertys($class) { $r = new ReflectionClass($class); if (!$r->hasProperty('tablename')) { throw new Exception("Class '$class' has no [tablename] property"); } $table = $r->getStaticPropertyValue('tablename'); if (!$r->hasProperty('id')) { throw new Exception("Class '$class' has no [id] property"); } $mpts = Array (); $pts = $r->getProperties(ReflectionProperty :: IS_PUBLIC); foreach ($pts as $pt) { if (!$pt->isStatic()) { array_push($mpts, $pt); } } return Array ( $table, $mpts ); } function SinORM_GetPropertyString($pts, $class, $obj = false, $noid = false) { if (is_null($pts)) { list ($tb, $pts) = SinORM_GetClassPropertys($class); } $s = false; $v = false; $l = false; foreach ($pts as $pt) { $name = $pt->name; if ($noid == false || $name != 'id') { if ($l) { $s = $s . ','; } $s = $s . $name; if ($obj) { if ($l) { $v = $v . ','; } $val = $pt->getValue($obj); if (is_null($val)) $v = $v . 'null'; if (is_string($val)) $v = $v . "'$val'"; else $v = $v . $val; } $l = true; } } return Array ( $s, $v ); } function SinORM_GetTableName($class){ $r = new ReflectionClass($class); if (!$r->hasProperty('tablename')) { throw new Exception("Class '$class' has no [tablename] property"); } $table = $r->getStaticPropertyValue('tablename'); if (!$r->hasProperty('id')) { throw new Exception("Class '$class' has no [id] property"); } return $table; } function SinORM_ResetORM($class) { list ($tb, $pts) = SinORM_GetClassPropertys($class); $sql = "CREATE TABLE `$tb` (`id` int NOT NULL AUTO_INCREMENT"; $r = new ReflectionClass($class); $obj = $r->newInstance(); foreach ($pts as $pt) { $val = $pt->getValue($obj); $name = $pt->name; if ($name != 'id') { $sql = $sql . ','; } else { continue; } if (is_null($val)) throw new Exception($class . '->' . "name must have a default value"); if (is_string($val)) $sql = $sql . "`$name` text NULL"; else $sql = $sql . "`$name` int NULL"; } $sql = $sql . ",PRIMARY KEY (`id`));"; $dsql = "DROP TABLE IF EXISTS `$tb`;"; return SinORM_ExecSql($dsql) && SinORM_ExecSql($sql); } function SinORM_SaveObject($obj) { $class = get_class($obj); list ($tb, $pts) = SinORM_GetClassPropertys($class); list ($names, $vals) = SinORM_GetPropertyString($pts, $class, $obj, true); $sql = "INSERT INTO `$tb`($names) values($vals)"; if(SinORM_ExecSql($sql)){ $q = "SELECT `id` FROM `$tb` ORDER BY `id` DESC LIMIT 1;"; $id = SinORM_ExecResult($q); if($id){ $obj->id = $id; } } return false; } function SinORM_GetObjects($class) { list ($tb, $pts) = SinORM_GetClassPropertys($class); $sql = "SELECT * from `$tb`;"; $ary = SinORM_ExecArray($sql); $res = false; if (is_array($ary)) { $res = Array (); $ref = new ReflectionClass($class); foreach ($ary as $a) { $obj = $ref->newInstance(); foreach ($pts as $pt) { $name = $pt->name; $olv = $pt->getValue($obj); $val = $a[$name]; if (is_string($olv)) $pt->setValue($obj, $val); else $pt->setValue($obj, intval($val)); } array_push($res, $obj); } } else { echo 'no'; } return $res; } function SinORM_GetObject($class, $id) { list ($tb, $pts) = SinORM_GetClassPropertys($class); $sql = "SELECT * from `$tb` where `id`=$id;"; $ary = SinORM_ExecArray($sql); $res = null; if (is_array($ary) && count($ary) > 0) { $res = Array (); $ref = new ReflectionClass($class); $a = $ary[0]; $obj = $ref->newInstance(); foreach ($pts as $pt) { $name = $pt->name; $olv = $pt->getValue($obj); $val = $a[$name]; if (is_string($olv)) $pt->setValue($obj, $val); else $pt->setValue($obj, intval($val)); } return $obj; } return null; } function SinORM_Update($obj) { $class = get_class($obj); list ($tb, $pts) = SinORM_GetClassPropertys($class); $sql = "UPDATE `$tb` SET "; $l = false; foreach ($pts as $pt) { $name = $pt->name; $val = $pt->getValue($obj); if ($name == 'id') continue; if ($l) $sql = $sql . ','; if (is_string($val)) $sql = $sql . "$name='$val'"; else $sql = $sql . "$name=$val"; $l = true; } $sql = $sql . " WHERE `id`=$obj->id;"; return SinORM_ExecSql($sql); } function SinORM_SaveOrUpdate($obj) { if (SinORM_GetObject(get_class($obj), $obj->id) == null) { SinORM_SaveObject($obj); } else { SinORM_Update($obj); } } function SinORM_DeleteObject($obj){ $class = get_class($obj); $tb = SinORM_GetTableName($class); $sql = "DELETE FROM `$tb` WHERE `id`=$obj->id;"; return SinORM_ExecSql($sql); } function SinORM_DeleteAll($class){ $tb = SinORM_GetTableName($class); $sql = "DELETE FROM `$tb`;"; return SinORM_ExecSql($sql); } ?>
The following is an example of use:
<?php /* * Filename: demo.php * Created on 2012-11-4 * Created by RobinTang * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ include_once("sinorm.php"); // 下面是一个持久对象的类的定义 // 每个持久对象类都必须有一个叫做$tablename静态成员,它表示数据库中存储对象的表名 // 类的每个成员都必须初始化,也就是必须给它一个初始值 // 成员变量只能为字符串或者整型,而且请定义成public的,只有public的成员变量会被映射 class User{ public static $tablename = 't_user'; // 静态变量,对象的表名,必须的 public $id = 0; // 对象ID,对应表中的主键,必须的,而且必须初始化为0 public $name = ''; // 姓名,必须初始化 public $age = 0; // 年龄,必须初始化 public $email = ''; // 必须初始化 } // 注意:下面的语句一定要在定义好类之后运行一下,修改了类也需要运行一下,它完成创建表的工作 // SinORM_ResetORM('User'); // 这一句只是一开始执行一次,执行之后就会自动在数据库中建立User对应的表 $user1 = new User(); // 创建一个对象 $user1->name = 'TRB'; $user1->age = 22; $user1->email = 'trbbadboy@qq.com'; SinORM_SaveObject($user1); // 把对象保存到数据库中 // 保存之后会自动给id的 $id = $user1->id; echo $id . '<br/>'; $user2 = SinORM_GetObject('User', $id); // 通过ID从数据库创建一个对象 echo $user2->name . '<br/>'; $user1->name = 'trb'; // 改变一下 SinORM_Update($user1); // 更新到数据库 $user3 = SinORM_GetObject('User', $id); // 重新读出 echo $user3->name . '<br/>'; ?>
Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.
Related recommendations:
php process control and mathematical operation methods
PHP variables And date processing cases
Related issues with PHP garbage collection mechanism
The above is the detailed content of How to implement persistence layer in php. For more information, please follow other related articles on the PHP Chinese website!