How to implement persistence layer in php

墨辰丷
Release: 2023-03-31 11:54:02
Original
1681 people have browsed it

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:

Copy after login

The following is a simple encapsulation of database access:

Copy after login

In fact, the two files above were written before, and the persistence layer is as follows:

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); } ?>
Copy after login

The following is an example of use:

name = 'TRB'; $user1->age = 22; $user1->email = 'trbbadboy@qq.com'; SinORM_SaveObject($user1); // 把对象保存到数据库中 // 保存之后会自动给id的 $id = $user1->id; echo $id . '
'; $user2 = SinORM_GetObject('User', $id); // 通过ID从数据库创建一个对象 echo $user2->name . '
'; $user1->name = 'trb'; // 改变一下 SinORM_Update($user1); // 更新到数据库 $user3 = SinORM_GetObject('User', $id); // 重新读出 echo $user3->name . '
'; ?>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!