轻量级且高效的 MySQL PHP 请求框架

v0.2.3 2020-02-11 07:37 UTC

This package is auto-updated.

Last update: 2024-09-25 16:24:09 UTC


README

该框架旨在最小化在 MySQL 服务器和 PHP 脚本之间交互中 SQL 代码的使用;将 PHP 对象更改与 DB 更改绑定。

为什么它很棒?

  • 响应式模型。您更改 PHP 模型 - 数据库将自动更改。
  • 根据现有表生成 PHP 类,帮助您的 IDE 提供建议。
  • 保持连接高效,因为它在整个交互中只使用一个活动的 MySQL 连接。
  • 保持对多个数据库的连接组织有序。
  • 自动防止 SQL 注入。

安装

只需使用 composer

composer require alexdremov/daim

使用方法

您可以使用它来保持您的数据库连接有组织和单一。首先,您需要设置该框架

use DAIM\Core\Connection;
use DAIM\Core\Credentials;

$cred = new Credentials();
$cred->setHost(host);
$cred->setUsername(username);
$cred->setDBname(DBname);
$cred->setPassword(password);
$cred->setPort(port);

Connection::setCredentials($cred);
Connection::initConnection();

Connection::getConnection(); # returns active MySQL connection (instance of mysqli class);

# To set up a second connection (maybe to the second database),
# you can create additional connection mode:

/**
 * Set up $cred2 as instance of Credentials class for the second connection
 * @var $cred2 Credentials;
 */

Connection::setCredentials($cred2, "secondConnectionName");
Connection::initConnection("secondConnectionName");

现在我们准备好了。

// Basic raw query.
Connection::query('SELECT * FROM `Persons` WHERE 1', "secondConnectionName");

目前,我正在开发查询构建器。该项目的状态是测试版,但一些功能已经可用

SELECT

use DAIM\Core\QueryBuilder;
use DAIM\Syntax\SQLEntities\Conditions;

$qb = new QueryBuilder();
$result = $qb->select('*')->from('Information')->request();

# Or more complicated usage:

$result = $qb->select(
    'Persons.LastName', 'Persons.PersonID', 'Information.Tel'
)->from(
    'Information', 'Persons'
)->where(
    (new Conditions())->field('Information.PersonID')->equal()->field('Persons.PersonID')
)->request();

# Generates SQL
# SELECT Persons.LastName, Persons.PersonID, Information.Tel FROM Information, Persons WHERE Information.PersonID = Persons.PersonID
# final ->request() returns instance of QueryResult class.

INSERT

use DAIM\Core\QueryBuilder;

$qb = new QueryBuilder();

$qb->insertInto('tableName', array(
"field1"=>"value1",
"field2"=>"value2"
));
$response = $qb->request(); // Commit changes

// Or longer version:

$qb->insertInto('tableName')->columns('field1', 'field2', 'field3')->values('value1', 'value2', 'value3')->request();
$qb->insertInto('tableName')->values('value1', 'value2', 'value3')->request();

INSERT 构建器中也可用子查询。

这种有限的库使用是由于项目的测试版状态。