gorgo / golibdatabase
数据库层
0.2.1
2021-01-15 09:39 UTC
Requires
- php: 8.*
- ext-mysqli: *
- gorgo/golib: 0.2.1
Requires (Dev)
- phpunit/phpunit: 9.4.3
This package is auto-updated.
Last update: 2024-09-10 12:57:29 UTC
README
golib 数据库层
安装
composer composer require gorgo/golibdatabase
基本用法
连接到 MySQL 数据库
use golibdatabase\Database\MySql; $connect = new MySql\ConnectInfo( 'username','password','hostname','default_shema' ); $db = new MySql( $connect ); $result = $db->select( 'SELECT * FROM Tablename' ); if ($result->getErrorNr()) { echo " --- mysql error:" . $result->getError(); } else { echo " --- got " . $result->count() . 'entries '; var_dump( $result->getResult() ); } }
连接管理器
用于无法确定连接是否已经存在的场景(例如通过替换单例数据库实现)
// run the whole code 3 times just to explain what the connection-manager is doing for ($i = 0; $i < 3; $i++) { $connect = new MySql\ConnectInfo( 'username','password','hostname','default_shema' ); $connectManager = new Database\ConnectManager(); if ($connectManager->connectionIsStored( $connect )) { $db = $connectManager->getStoredConnection( $connect ); echo ' --- use existing connection --- '; } else { echo ' ---- create a new connection --- '; $db = new MySql( $connect ); $connectManager->registerConnection( $db ); } $result = $db->select( 'SELECT * FROM Tablename' ); if ($result->getErrorNr()) { echo " --- mysql error:" . $result->getError(); } else { echo " --- got " . $result->count() . 'entries '; var_dump( $result->getResult() ); } }
表格示例
通过示例说明。
表格结构和内容。
CREATE TABLE `golib-db` ( `primId` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `Content` VARCHAR( 250 ) NOT NULL , `DateExample` DATETIME NOT NULL , `ExampleValue` MEDIUMINT NOT NULL ) ENGINE = InnoDB; INSERT INTO `golib-db` ( `primId` , `Content` , `DateExample` , `ExampleValue` ) VALUES ( NULL , 'test content', '2017-09-30 00:00:00', '450' ), ( NULL , 'second content', '2017-09-19 00:00:00', '9887' );
步骤 1: 创建一个反映数据库结构的属性类。这个类必须从 golib\Types\PropsFactory
继承。
如下所示
/** * the property Class descriptes the expected fields */ use golib\Types; /** * the property Class descriptes the expected fields */ class exampleProp extends Types\PropsFactory { /** * autoinc, primary * @var int */ public $primId = NULL; /** * * @var string */ public $Content = ''; /** * a date example * @var Types\Timer */ public $DateExample = Types\MapConst::TIMER; /** * just a integer * @var int */ public $ExampleValue = 0; }
步骤 2: 定义指向表的表类,并设置属性类和表名。
use golib\Types; /** * the table class * that maps to the table in the database. * * they need to know about the structure by using * the property class * * and (of course) the table name */ class exampleTable extends MySql\Table { /** * defines the content. * how the rows looks like * @return \exampleProp */ public function getPropFactory () { return new exampleProp( 'primId' ); } /** * just the tablename * @return string */ public function getTableName () { return 'golib-db'; } }
这就是设置基本表模型所需的所有内容。要从此表读取,请创建这些类的实例,并通过现有的数据库连接获取数据。
// initiate the modell of the table $golibTable = new exampleTable(); // get the content by using a existing database connection $golibTable->fetchData( $db ); // one way to iterate the content. $golibTable->foreachCall( function(exampleProp $row) { var_dump( $row ); } );
现在,我们有一个数据库表的模型作为 PHP 对象,它将读取表的全部内容并将其分配给行对象。
WhereSet
但通常您不需要全部内容。在 MySQL 中,您可以通过添加一个 where
语句来过滤结果来处理这种情况。同样可以通过使用 WhereSet
来完成。
因此,更改示例中的代码
$where = new MySql\WhereSet(); $where->isEqual( 'ExampleValue', 9887 ); $golibTable = new exampleTable( $where );
现在我们将只获取匹配的值。
Limit
Limit
类对于限制条目数量非常有用,就像 MySQL 中的常规 Limit
一样。Limit 实例必须是第二个参数。
$Limit = new MySql\Limit(); $Limit->count = 1; $golibTable = new exampleTable( NULL, $limit );
此对象只有两个属性。`start` 定义偏移量,`count` 定义条目数量。
$Limit = new MySql\Limit(); $Limit->count = 1; $Limit->start = 1; // equal to LIMIT 1,1 $Limit->count = 100; $Limit->start = 0; // equal to LIMIT 0,100