celtak / dog-database
一个类,允许使用 MySQL 数据库并通过方法而非查询调用该数据库
v1.0.1
2019-03-29 18:57 UTC
Requires
- php: ^7.1.0
- ext-pdo: *
- symfony/var-dumper: ^4.2
- symfony/yaml: ^4.2
This package is auto-updated.
Last update: 2024-09-08 01:10:10 UTC
README
Composer 安装
composer require celtak/dog-database
文档
先决条件
require __DIR__ . DIRECTORY_SEPARATOR . 'vendor/autoload.php'; use Celtak\DogDatabase;
连接到数据库
如何连接到数据库?
$database = new DogDatabase([ 'host' => 'localhost:8889', 'dbname' => 'azurWeb', 'username' => 'root', 'password' => 'root' ], true);
第二个参数是一个布尔值,用于控制是否显示错误。
数据库中的一个表("users")
为了理解其工作原理,我们将使用一个表。
表:users
在表上工作
$database->setTable('users');
创建
如何插入新数据?
// Insert new data $database->insert([ ['first', 'Henrique'], ['name', 'Rodrigues'], ['old', 33], ]);
结果是什么?
读取
getAll()
读取整个数据库。
$read = $database->getAll(); // All the contents of the database dump($read);
使用过滤器(where、order by 和 limit)。
示例 1
$read = $database->getAll([ 'where' => ['name', 'Rodrigues'], 'orderBy' => 'old', ]); // Just "Melanie Rodrigues" and "Henrique Rodrigues" dump($read);
示例 2
$read = $database->getAll([ 'limit' => [1, 3] ]); // Just "Melanie Rodrigues", "Anthony Jones" and "Audrey Jones" dump($read);
getFields()
获取一些字段。
$read = $database->getFields(['name', 'first']); // We just get the "name" and "first" fields of only "Anthony Jones" and "Audrey Jones" dump($read);
使用过滤器。
$read = $database->getFields(['name', 'first'], [ 'where' => ['name', 'Jones'] ]); // We just get the "name" and "first" fields from all the database dump($read);
getId()
通过 id 获取。
$read = $database->getId(3); // We get the data related to id 3, "Audrey Jones" dump($read);
更新
update()
使用必需的 "where" 过滤器。
// Modify the first name Henrique by Rik $database->update( [ ['first', 'Rik'] ], [ 'where' => ['first', 'Henrique'], ] );
updateId()
使用 id。
// Modify the first name by John and the name by Taylor from id 3 $database->updateId(3, [ ['first', 'John'], ['name', 'Taylor'], ]);
删除
delete()
使用必需的 "where" 过滤器。
// Delete all data from id 2 $database->delete([ 'where' => ['id', '2'] ]);
deleteId()
// Delete all data from id 1 $database->deleteId(1);
自定义查询
用于读取。
dump($database->getCustomizedQuery('SELECT * FROM users WHERE id = "10"'));
用于写入。
$database->customizedExec('INSERT INTO utilisateurs (name, first, old) VALUES ("Henrique", "Rodrigues", "33")');
许可协议
MIT。