此库提供了一种简单而强大的方法来从PDO数据库中设置和获取记录。

1.8.0 2024-09-17 22:29 UTC

README

此库提供了一种简单而强大的方法来从PDO数据库中设置和获取记录。它具有许多在过去10年里开发出来的强大的数据库交互方法。

安装

使用composer安装

composer require truecastdesign/hopper

需要PHP 5.5或更高版本。

用法

以下是一个基本用法示例

# composer autoloader
require '/path/to/vendor/autoload.php';

# create a new instance of the Hopper class
$DB = new \Truecast\Hopper(['type'=>'mysql', 'username'=>'', 'password'=>'', 'database'=>'']);

# insert a record
$DB->set('table_name', ['first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live']);  # id:1
$DB->set('table_name', ['first_name'=>'Tim', 'last_name'=>'Baldwin', 'phone'=>'541-555-5551', 'status'=>'live']); # id:2

# update a record
$DB->set('table_name', ['id'=>1, 'phone'=>'541-555-5556']);

# execute your own update or insert query and check if it was successful
if ($DB->execute('update tablename set field1=?, field2=? where field3=?', ['val1', 'val2', 'val3'])) {
	# updated row
} else {
	# didn't update row
}

# execute your own update or insert query and check if it was successful
if ($DB->execute('insert into tablename set field1=?, field2=? where field3=?', ['val1', 'val2', 'val3'])) {
	# updated row
} else {
	# didn't update row
}

# get single record
$recordObj = $DB->get('select * from table_name where id=?', [1], 'object');
$recordArray = $DB->get('select * from table_name where id=?', [1], 'array');

# output:
stdClass Object ('id'=>1, first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live')
Array ('id'=>1, first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live')

# get a single value
$value = $DB->get('select first_name from table_name where id=?', [1], 'value');

# output
string 'John'

# get several records
$recordList = $DB->get('select id,first_name,last_name from table_name where status=?', ["live"], '2dim');

# output
Array (	[0]=>Array ('id'=>1, first_name'=>'John', 'last_name'=>'Doe')
		[1]=>Array ('id'=>2, 'first_name'=>'Tim', 'last_name'=>'Baldwin'))

创建配置文件,并使用truecastdesign/config类将其转换为对象,以便传递给构造函数。这允许您将配置存储在ini文件中。

; MySQL database config

config_title = 'mysql'
type = 'mysql'
hostname = 'localhost'
username = 'root'
password  = 'password'
database  = 'dbname'
port = 3306
persistent = true
emulate_prepares = false
compress = true
charset = 'utf8'
buffer = true
sslCertAuthority = '' // The file path to the SSL certificate authority.
sslCaCertificates = '' // The file path to the directory that contains the trusted SSL CA certificates, which are stored in PEM format.
sslCert = '' // The file path to the SSL certificate.
sslCipher = '' // A list of one or more permissible ciphers to use for SSL encryption, in a format understood by OpenSSL. For example: DHE-RSA-AES256-SHA:AES128-SHA
sslKey = '' // The file path to the SSL key.
sslVerifyCert = '' // Provides a way to disable verification of the server SSL certificate.
multiStatements = '' // Disables multi query execution in both PDO::prepare() and PDO::query() when set to false.

使用配置文件实例化。

$DB = new \Truecast\Hopper($Config->mysql);

删除记录

$DB->delete('table_name', 1); # deletes record with id=1

$DB->delete('table_name', 'value', 'field_name'); # deletes records with field_name='value'

有一个方法可以将记录设置到可伸缩的键值表中。

可伸缩的键值表是一个具有诸如id、record_id、key、value等字段名的表。每个记录中的键值对存储在其自己的表行中。这样,您可以动态地添加和删除要存储的字段。

$settings = ['record_id_field'=>'record_id', 'record_id'=>1, 'key_field'=>'field_name', 'value_field'=>'value'];

$DB->setScalableKeyValue('table_name', ['first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live'], $settings)

随着我腾出时间来编写,将提供更多方法文档。

清空表中的数据

$DB->emptyTable('table_name');