liftkit / database
LiftKit 数据库库
v2.24.0
2023-09-12 12:50 UTC
Requires
- ext-json: *
- ext-pdo: *
- liftkit/collection: ^1.0
- liftkit/dependency-injection: ^1.0
Requires (Dev)
- phpunit/dbunit: ^1.2
- phpunit/phpunit: ^4.5
- dev-master
- v2.24.0
- v2.23.1
- v2.23.0
- v2.22.0
- v2.21.0
- v2.20.1
- v2.20.0
- v2.19.2
- v2.19.1
- v2.19.0
- v2.18.2
- v2.18.1
- v2.18.0
- v2.17.7
- v2.17.6
- v2.17.5
- v2.17.4
- v2.17.3
- v2.17.2
- v2.17.1
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.1
- v2.14.0
- v2.13.2
- v2.13.1
- v2.13.0
- v2.12.0
- v2.11.2
- v2.11.1
- v2.11.0
- v2.10.0
- v2.10.0-alpha
- v2.9.0
- v2.9.0-alpha10
- v2.9.0-alpha9
- v2.9.0-alpha8
- v2.9.0-alpha7
- v2.9.0-alpha6
- v2.9.0-alpha5
- v2.9.0-alpha4
- v2.9.0-alpha3
- v2.9.0-alpha.2
- v2.9.0-alpha.1
- v2.8.0
- v2.7.1
- v2.7.0
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.1.0-alpha
- v1.0.1
- v1.0
This package is auto-updated.
Last update: 2024-09-12 14:46:57 UTC
README
安装
composer require liftkit/database
让我们直接进入一些示例。
连接
建立连接
use LiftKit\Database\Connection\MySql; use LiftKit\DependencyInjection\Container\Container; use LiftKit\Database\Cache\Cache; use PDO; $connection = new MySql( new Container, new Cache, new PDO('connectionString', 'username', 'password') );
运行原始 SQL 查询
$results = $connection->query( " SELECT * FROM tbl " );
使用占位符
$connection->query( " SELECT * FROM tbl WHERE col1 = ? AND col2 = ? ", [ 'val1', 'val2', ] );
结果对象
循环遍历结果
// NOTE: // Results are not loaded into memory. Instead they are // wrapped by an object of the class // \LiftKit\Database\Result\Result $results = $connection->query( " SELECT * FROM tbl " ); foreach ($results as $row) { echo 'column "name" = ' . $row['name'] . PHP_EOL; echo 'column "id" = ' . $row['id'] . PHP_EOL; }
作为数组获取单个列
foreach ($results->fetchColumn('id') as $id) { echo $id . PHP_EOL; } // '1' // '2' // ...
作为实体数组获取所有行
foreach ($results->fetchAll() as $row) { // Do something with $row['column'] }
作为关联数组数组获取所有行
foreach ($results->flatten() as $row) { // Do something with $row['column'] }
查询构建器
新查询
use LiftKit\Database\Query\Query; /** * @var Query $query */ $query = $connection->createQuery();
简单的选择查询
// SELECT field1, field2 // FROM tbl // WHERE field1 = 'val1' $results = $query->select('field1', 'field2') ->from('tbl') ->whereEqual('field1', 'val1') ->execute();
更复杂的选择查询
请注意,方法 $connection->quoteIdentifier()
被应用于正确的参数。这是因为预期的参数是值。如果是 SQL 标识符,则必须引用。
此示例显示了使用 MySQL 样式的标识符引号查询来阐述这一点。请注意 JOIN
条件和 WHERE
条件之间的差异。
use LiftKit\Database\Query\Condition\Condition; // SELECT `field1`, `field2` // FROM `tbl` // LEFT JOIN `other_tbl` ON ( // `tbl`.`field1` = `other_tbl`.`field1` // OR `tbl`.`field2` > `other_tbl`.field2` // ) // WHERE `tbl`.`field1` = 'val1' // OR `other_tbl`.`field2` = 'val2' // GROUP BY `tbl`.`field3`, `tbl`.`field4` // HAVING `tbl`.`field1` < 1 // ORDER BY `tbl`.`field5` ASC, `tbl`.`field6` DESC $results = $query->select('field1', 'field2') ->from('tbl') ->leftJoin( 'other_tbl', $connection->createCondition() ->equal( 'tbl.field1', $connection->quoteIdentifier('other_tbl.field1') ) ->orGreaterThan( 'tbl.field2', $connection->quoteIdentifier('other_tbl.field2') ) ) ->whereEqual('tbl1.field1', 'val1') ->orWhereEqual('other_tbl.field2', 'val2') ->groupBy('tbl.field3') ->groupBy('tbl.field4') ->havingLessThan('tbl.field1', 1) ->orderBy('tbl.field5', Query::ORDER_ASC) ->orderBy('tbl.field6', Query::ORDER_DESC) ->execute();
更新查询
请注意,更新查询可以像选择语句一样使用条件。
// UPDATE tbl // SET field2 = 'val2', field3 = 'val3' // WHERE tbl.id = 2 $query->update() ->table('tbl') ->set( [ 'field2' => 'val2', 'field3' => 'val3', ] ) ->whereEqual('tbl.id', 2) ->execute();
插入查询
插入查询返回其插入 ID。
// INSERT INTO tbl // SET field2 = 'val2', field3 = 'val3' $id = $query->insert() ->into('tbl') ->set( [ 'field2' => 'val2', 'field3' => 'val3', ] ) ->execute();
删除查询
请注意,删除查询可以使用与选择查询相同的方式使用条件。
// DELETE tbl.* // FROM tbl // WHERE id = 1 $query->delete() ->from('tbl') ->whereEqual('id', 1) ->execute();
子查询
子查询几乎可以替代任何值或标识符的位置。
注意:这也是一个示例,说明如何在查询中使用原始 SQL 而不是转义值,使用方法 $connection->createRaw()
。选择参数,如条件左侧,除非另外指定,否则将作为标识符引用。
// SELECT * // FROM tbl1 // WHERE // ( SELECT COUNT(*) // FROM tbl2 // WHERE tbl1.id = tbl2.tbl1_id // ) = 1 $results = $query->select('*') ->from('tbl1') ->whereEqual( $connection->createQuery() ->select($connection->createRaw('COUNT(*)')) ->from('tbl2') ->whereEqual( 'tbl1.id', $connection->quoteIdentifier('tb2.tbl1_id') ), 1 ) ->execute();
组合查询部分
这适用于提取经常使用的查询的一部分,同时保留与其他查询组合的能力。
假设您有一个返回 tbl
中所有行的函数。
function getAllTblRows () { return $connection->createQuery() ->select('*') ->from('tbl') ->execute(); } // SELECT * // FROM tbl $results = getActiveTblRows();
现在您需要另一个查询,该查询仅从 tbl
选择活动记录。注意对 getAllTblRows
的添加。
function getAllTblRows (Query $inputQuery = null) { return $connection->createQuery() ->select('*') ->from('tbl') ->composeWith($inputQuery) ->execute(); } function getActiveTblRows () { $query = $connection->createQuery() ->whereEqual('active', 1); return getAllTblRows($query); } // SELECT * // FROM tbl // WHERE active = 1 $results = getActiveTblRows();
表对象
表对象旨在减少您需要在查询构建器查询中放置的样板代码。
获取多行
use LiftKit\Database\Schema\Schema; use LiftKit\Database\Schema\Table\Table; // We'll get back to schemas in a moment $table = new Table( $connection, new Schema($connection), 'tbl' ); // SELECT * // FROM tbl $results = $tbl->getRows();
使用组合查询获取多行
// SELECT * // FROM tbl // WHERE active = 1 $results = $table->getRows( $connection->createQuery() ->whereEqual('active', 1) );
获取单行
// SELECT * // FROM tbl // WHERE id = 1 // LIMIT 1 $row = $table->getRow(1); // 'val1' echo $row['field1']; // 'val2' echo $row['field2'];
插入新行
// INSERT INTO tbl // SET field1 = 'val1', field2 = 'val2' $id = $table->insertRow( [ 'field1' => 'val1', 'field2' => 'val2', ] ); ``` ### Updating a row NOTE: The library will auto-detect the primary key column and create an equal condition on that column. ```php // UPDATE tbl // SET field1 = 'val1', field2 = 'val2' // WHERE id = 1 $table->updateRow( [ 'id' => 1, 'field1' => 'val1', 'field2' => 'val2', ] ); ``` ### Deleting a row ```php // DELETE FROM tbl // WHERE id = 1 $table->deleteRow(1); ``` More info on table objects, relations, and entities coming soon!