chrisbaltazar / dbconn
用于非框架项目的简单快速的数据库查询库
2.0.3
2021-01-28 17:01 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-29 01:53:19 UTC
README
这是一个用于处理MySQL/MariaDB数据库引擎常见且快速操作的实用库,采用对象表示法,并从Laravel Eloquent中汲取灵感。
安装
只需通过运行composer安装此库
composer require chrisbaltazar/dbconn
要求
- PHP >= 7.0
初始配置。
所有必要的配置都在类构造函数内完成,因此您只需要在实例化您的DB对象之前准备好所需的CONSTANTS/ENV值。
配置字段列表如下
- DB_HOST (字符串)
- DB_USER (字符串)
- DB_PWD (字符串)
- DB_NAME (字符串)
- DB_DEBUG (布尔值)
- SESSION_ID (字符串) 应用程序会话中变量的名称,用于获取当前用户
- TIME_ZONE (字符串) GMT+1
- SUMMER_TIME (布尔值) 自动计算时间变化
- CHARSET (字符串) utf8
其他配置。
默认情况下,库还尝试处理流行的 tablestamps
- updated_by
- updated_at
- deleted_at
并在每次查询中填写它们的值,这也配置在 constructor
中,因此您可以通过扩展类并重写这部分来实现可能需要的任何更改。
要记住的最重要的事情是,如果您扩展了库,则必须在完成类配置后调用
init
方法。
使用
use chrisbaltazar/dbconn; $db = new DBConn();
示例
从一个单独的表中获取结果列表...
$data = $db->from('tablename')->getArray();
对于更常见的情况,JSON可能更有用
$data = $db->from('tablename')->getJSON();
在咨询中选择您需要的字段(用逗号分隔)
$data = $db->select('field1, field2, field3') ->from('tablename') ->getJSON();
每次您需要调试您的咨询时,您都可以在结束时使用 getSQL
方法来获取要执行的查询体。
$data = $db->select('field1, field2, field3') ->from('tablename') ->getSQL();
让我们使用多个表进行JOIN操作
$data = $db->select('field1, field2, field3', 'field4', 'field5') ->from('maintable') ->join('table2') ->join('table3', 1, 'LEFT') ->getJSON(); // In this case you can declare on the "join" statement the following: // 1. Table name for join to // 2. Index of target table in the tables array to join with, // for this example we have 3 tables in total, counting the main source(from table) // In this case the index 1 will point to table2 instead of the maintable which is index o // 3. The type of join, INNER by default
默认情况下,连接是通过公共表的主键(例如
id
和foreign_id
)完成的
如果您需要为每个JOIN指定ON子句,可以使用
$data = $db->select('field1, field2, field3', 'field4') ->from('maintable') ->join('table2')->on('local_id', 'foreign_id') ->getJSON();
现在我们正在分别提取每个表的字段
$data = $db->select('field1, field2, field3', 'field4', 'field5') ->from('maintable') ->join('table2') ->join('table3', 1, 'LEFT') ->getJSON(); // This way, we are extracting fields 1, 2 and 3 from table 0 or maintable // and field4 from the fisrt join, table1 in this case // the same for field5 which will come from table3 in that order
关于添加ORDER和GROUP子句
$data = $db->select('field1, field2, field3') ->from('tablename') ->order('somefield1, somefield2') ->group('someotherfield') ->getJSON(); // Also you can specify the scope of the ORDER or GROUP // using array notation and index declaration like: ->order([1 => 'table1_field', 0 => 'table0_field']);
“WHERE”子句呢?让我们看看...
$data = $db->select(['field1, field2', 'field3', 'field4']) ->from('tablename') ->join(['othertablename', 0, 'other_id = main_id']) ->join(['anothertablename', 1, 'another_id = other_id', 'LEFT']) ->where(['table0field1 = somevalue, table0field2 = 0', 'table1field = somethingelse']) ->getJSON(); // The result of the above statement would be: ...where table0_name.field1 = 'somevalue' and table0_name.field2 = '0' and table1_name.field = 'someothervalue'
使用SAVE方法进行INSERT和UPDATE
$db->save('tablename', ['fieldname' => 'value'...], ['fieldname' => 'value']); // Here, you can set an array of values to INSERT or UPDATE the table, // which will be auto evaluated depending on the third parameter, the "where" part
此方法的另一个用途...
$db->save('tablename', $_POST, ['id' => $_POST['id']); // You can see how can be more dynamic than previous using if you like // In this case, maybe the 'id' could be present or not and the method will // evaluate it as well.
最后,DELETE...
$db->delete('tablename', $id); // The DELETE method, will detect if you are passing only a numeric value as condition // and use it with the table id automatically $db->delete('tablename', ['field' => 'value', ...]); // Or you can either set all the condition fields to make the delete
请记住,您可以在之前设置“删除标志”,以避免永久删除