lfphp / porm
PHP 数据库ORM抽象库,用于PHP程式独立使用数据库时,方便以ORM方式进行设计。
Requires
- php: >=7.1
- ext-json: *
- ext-mbstring: *
- ext-mysqli: *
- ext-pdo: *
- lfphp/func: ^2.0
- lfphp/logger: ^2.0
- lfphp/pdodsn: ^1.0
- dev-master
- 1.0.1
- 1.0.0
- 0.0.69
- 0.0.68
- 0.0.67
- 0.0.66
- 0.0.65
- 0.0.64
- 0.0.63
- 0.0.62
- 0.0.61
- 0.0.60
- 0.0.59
- 0.0.58
- 0.0.57
- 0.0.56
- 0.0.55
- 0.0.54
- 0.0.53
- 0.0.52
- 0.0.51
- 0.0.50
- 0.0.49
- 0.0.48
- 0.0.47
- 0.0.46
- 0.0.45
- 0.0.44
- 0.0.43
- 0.0.42
- 0.0.41
- 0.0.40
- 0.0.39
- 0.0.38
- 0.0.37
- 0.0.36
- 0.0.35
- 0.0.34
- 0.0.33
- 0.0.32
- 0.0.31
- 0.0.30
- 0.0.29
- 0.0.28
- 0.0.27
- 0.0.26
- 0.0.25
- 0.0.24
- 0.0.23
- 0.0.22
- 0.0.21
- 0.0.20
- 0.0.19
- 0.0.18
- 0.0.17
- 0.0.16
- 0.0.15
- 0.0.14
- 0.0.13
- 0.0.12
- 0.0.11
- 0.0.10
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.2
- 0.0.1
This package is auto-updated.
Last update: 2024-11-23 12:30:37 UTC
README
#PORM
PORM is developed and tested based on PHP5.6 and above
PHP database ORM abstraction library, used for PHP programs to use databases independently, convenient for coding in ORM mode. PORM currently only supports MySQL, and is linked in MySQLi or PDO mode (PDO is recommended). Please ensure that the corresponding extension is correctly installed in PHP.
1. Quick Installation
composer require lfphp/porm
2. Usage
PORM supports calling by defining the ORM Model class, and also supports calling by directly linking to the database.
2.1 ORM approach
<?php use LFPhp\PORM\ORM\Model; class User extends Model { static public function getTableName(){ // TODO: Implement getTableName() method. } static public function getAttributes(){ // TODO: Implement getAttributes() method. } static protected function getDBConfig($operate_type = self::OP_READ){ // TODO: Implement getDBConfig() method. }} //Query object $user = UserTable::findOneByPK(1); var_dump($user); //Change the object $user->name = 'Jack'; $user->save(); //Add new object $user = new UserTable(); $user->name = 'Michel'; $user->save(); echo $user->id;
2.2 Database Operations
<?php //Create database configuration $cfg = new MySQL([ 'host'=>'localhost', 'user'=>'root', 'password'=>'123456', 'database'=>'database' ]); //Create a database link instance $ins = DBAbstract::instance($config); //Create a query object (query statement) $query = (new Query())->select()->field('id', 'title')->from('blog_article'); //Get the results $ret = $ins->getPage($query); //Get the count $count = $ins->getCount($query);
3. Others
3.1 Annotation Rules
By introducing the
AttributeAnnotation
class in the Model, theDBAttribute
annotation can be automatically generated based on the current class annotation.
At this point, you need to ensure that the class annotation rules include the following type annotations:
- @property string $name name (name note)
3.2 Database reconnection support
Add the max_reconnect_count
item in the database configuration to set the database to automatically reconnect to the database when the connection is lost (not the first connection).
<?php //Create database configuration $cfg = new MySQL([ 'host'=>'localhost', 'user'=>'root', 'password'=>'123456', 'database'=>'database', 'max_reconnect_count' => 10, //Set the maximum number of reconnections ]); $ins = DBAbstract::instance($config); //...