ceus-media/database

PHP数据库访问

0.6.3 2024-02-20 02:06 UTC

This package is auto-updated.

Last update: 2024-09-17 01:13:22 UTC


README

Branch Release PHP version PHPStan level

PHP数据库访问

安装

Composer

使用composer安装此库

composer require ceus-media/Database

在您的代码中加载库

require_once 'vendor/autoload.php';

使用PDO的代码示例

数据库连接

$dbDriver	= 'mysql';
$dbName		= 'myDatabase';
$dbUsername	= 'myDatabaseUser';
$dbPassword	= 'myDatabasePassword';

$dbc	= new \CeusMedia\Database\PDO\Connection(
	new \CeusMedia\Database\PDO\DataSourceName( $dbDriver, $dbName ),
	$dbUsername, $dbPassword
);

现有的数据库表可以声明为表

表类

class MyFirstTable extends \CeusMedia\Database\PDO\Table
{
	protected string $name			= "my_first_table";
	protected array $columns		= [
		'id',
		'maybeSomeForeignId',
		'content',
	];
	protected string $primaryKey	= 'id';
	protected array $indices		= [
		'maybeSomeForeignId',
	];
	protected int $fetchMode		= \PDO::FETCH_OBJ;
}

表实例

有了这个定义的结构,您可以使用表实例从数据库表中读取和写入。因此,您需要先创建数据库连接。

$table	= new MyFirstTable( $dbc );

读取条目

按主键获取条目的示例

$entry	= $table->get( 1 );

由于按表结构设置为按对象方式,因此结果将是一个表列及其值的对象

object stdObject(
	'id'					=> 1,
	'maybeSomeForeignId'	=> 123,
	'content'				=> 'Content of first entry.'
)

如果没有设置fetch模式,将会得到一个关联数组,这是底层表读取器默认的fetch模式。要更改fetch模式,请参阅下面。

提示:还有更多方法可以检索单个条目

  • getByIndex
  • getByIndices

这些方法允许关注外键索引而不是主键。

查找条目

根据外键过滤的条目组

$someEntries	= $table->getAllByIndex( 'maybeSomeForeignId', 123 );

根据多个外键过滤的条目组

$indices		= [
	'maybeSomeForeignId'	=> 123,
	'notExistingKey'		=> 'will result in an exception',
];
$someEntries	= $table->getAllByIndices( $indices );

要获取所有条目,请调用

$allEntries	= $table->getAll();

这可能在扩展性方面表现不佳,因此可以通过定义限制和条件来减少结果集

$conditions	= ['content' => '%test%'];
$orders		= [];
$limits		= [$offset = 0, $limit = 10];

$allEntries	= $table->getAll( $conditions, $orders, $limits );

条件可以是索引或任何其他列。

排序是列和方向的配对,例如

$orders	= [
	'maybeSomeForeignId'	=> 'DESC',
	'content'		=> 'ASC',
];

每种索引方法都有更多可能的参数,这允许

  • 字段:限制结果集中的列
  • 分组:应用GROUP BY
  • HAVING:应用HAVING

计数

按外键计数条目

$number	= $table->countByIndex( 'maybeSomeForeignId', 123 );

按多个外键计数条目

$number	= $table->countByIndices( [
	'maybeSomeForeignId'	=> 123,
	'notExistingKey'		=> 'will result in an exception',
] );

要获取所有条目,请调用

$number	= $table->count();

这可能在扩展性方面表现不佳,因此可以通过定义条件来减少结果集

$Conditions	= [
	'maybeSomeForeignId'	=> 123,
	'content'		=> '%test%',
];
$number	= $table->count( $conditions );

提示:在大型MySQL表中计数可能很慢。有一个快速计数大型表的方法。您会找到它的。

添加条目

$data		= [
	'maybeSomeForeignId'	=> 123,
	'content'				=> 'Second entry.',
];
$entryId	= $table->add( $data );

注意:出于安全原因,所有HTML标签将被删除。如果需要,将第二个参数设置为FALSE以避免这种情况。请确保手动删除非HTML列的HTML标签!

更新条目

$primaryKey	= 2;
$data		= [
	'maybeSomeForeignId'	=> 124,
	'content'				=> 'Second entry - changed.',
];
$result	= $table->edit( $primaryKey, $data );

结果将是更改的条目数。

注意:出于安全原因,所有HTML标签将被删除。如果需要,将第三个参数设置为FALSE以避免这种情况。请确保手动删除非HTML列的HTML标签!

更新多个条目

$indices	= [
	'maybeSomeForeignId'	=> 123,
];
$data		= [
	'maybeSomeForeignId'	=> 124,
];
$result	= $table->editByIndices( $indices, $data );

结果将是更改的条目数。

注意:出于安全原因,所有HTML标签将被删除。如果需要,将第三个参数设置为FALSE以避免这种情况。请确保手动删除非HTML列的HTML标签!

删除条目

$primaryKey	= 2;
$result	= $table->remove( $primaryKey );

结果将是删除的条目数。

删除多个条目

$indices	= [
	'maybeSomeForeignId'	=> 123,
];
$result	= $table->removeByIndices( $indices );

结果将是删除的条目数。

更改fetch模式

在您的表结构类中,设置

	protected int $fetchMode		= \PDO::[YOUR_FETCH_MODE];

其中YOUR_FETCH_MODE是以下标准PDO fetch模式之一

  • FETCH_ASSOC
  • FETCH_NAMED
  • FETCH_NUM
  • FETCH_BOTH
  • FETCH_OBJ

使用OSQL的代码示例

有一个如下的配置文件

driver		= 'mysql';
host		= 'myHost';
port		= 'myPort';
database	= 'myDatabase';
username	= 'myDatabaseUser';
password	= 'myDatabasePassword';

并假设您是这样加载的

require_once 'vendor/autoload.php';

use CeusMedia\Database\PDO\DataSourceName;
use CeusMedia\Database\OSQL\Client;
use CeusMedia\Database\OSQL\Connection;
use CeusMedia\Database\OSQL\Condition;
use CeusMedia\Database\OSQL\Table;
use CeusMedia\Database\OSQL\Query\Select;

$config	= (object) parse_ini_file( 'myConfigFile.ini' );

您可以通过这种方式连接到数据库

$client	= new Client( new Connection( DataSourceName::renderStatic(
	$config->driver,
	$config->database,
	$config->host,
	$config->port,
	$config->username,
	$config->password
), $config->username, $config->password ) );

现在您可以像这样查询数据库

$result	= Select::create( $client )
	->from( new Table( 'galleries', 'g' ) )
	->where( new Condition( 'galleryId', 1, Condition::OP_EQ ) )
	->execute();

结果将包含请求的行(本例中只有一个)

new UI_DevOutput();
print_m( $result );

将产生

[O] 0 -> stdClass
   [S] galleryId => 1
   [S] status => 0
   [S] rank => 1
   [S] path => test
   [S] title => Test
   [S] description => Das ist ein Test.
   [S] timestamp => 1402008611