microweber-packages / microweber-database-manager
Microweber数据库管理器。
Requires
- jeremeamia/superclosure: *
- laravel/framework: 7.0.0
- microweber-packages/laravel-tagged-file-cache: dev-master
- microweber-packages/microweber-event-manager: dev-master
- microweber-packages/microweber-helpers: dev-master
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-06 22:55:38 UTC
README
https://travis-ci.org/microweber-packages/microweber-database-manager
数据库保存
允许您将数据保存到数据库中的任何表。
摘要
db_save($table, $data);
这是Microweber的核心功能之一,大多数其他保存功能实际上都是这个功能的包装。它允许您在任何数据库表中的任何位置保存数据。
**注意:这是一个原始函数,不验证数据输入或用户权限。**
此功能非常危险,不应直接在模板或模块视图中使用。最好创建自己的包装函数,以验证输入,并在需要时使用它们。
用法
$table='content'; $data = array(); $data['id'] = 0; $data['title'] = 'My title'; $data['content'] = 'My content'; $data['allow_html'] = true; //if true will allow you to save html $saved = db_save($table,$data);
参数
默认情况下,此函数删除HTML标签。要保存纯HTML,您必须在您的数据数组中设置$data['allow_html'] = true;
#######################
数据库获取
允许您从数据库中的任何表获取数据,并缓存结果。
摘要
db_get($params);
用法
$content = db_get('table=content');
foreach ($content as $item) {
print $item['id'];
print $item['title'];
print $item['url'];
print $item['description'];
print $item['content'];
}
参数
您可以将参数作为string
或作为array
传递。它们可以是数据库表中定义的带有table
参数的字段名称。
获取一切
//get 5 users
$users = db_get('table=users&limit=5');
//get next 5 users
$users = db_get('table=users&limit=5&page=2');
//get 5 categories
$categories = db_get('table=categories&limit=5');
//get 5 comments
$comments = db_get('table=comments&limit=5');
#######################
数据库删除
db_delete — 从数据库表中删除记录
摘要
db_delete($table_name, $id = 0, $field_name = 'id')
返回值
true
或false
,如果项目未删除
用法
$category_id = 5;
$delete = db_delete('categories', $category_id);
保存和获取模块数据
定义模式
当安装模块时,Microweber会检查config.php
文件中的$config['tables']
数组。每个键代表一个表名,其值是一个列定义的数组。ID列会自动创建,并且所有列默认为可空。
示例 userfiles/modules/paintings/config.php
$config = array();
$config['name'] = "My Paintings";
$config['author'] = "Pablo Picasso";
$config['ui'] = true;
$config['ui_admin'] = true;
$config['position'] = "98";
$config['version'] = "0.01";
$config['tables'] = array(
'paintings' => array(
'id' => 'integer',
'name' => 'string',
'price' => 'float',
'description' => 'text',
'created_by' => 'integer',
'created_at' => 'dateTime',
)
);
自定义表
有关数据存储的更多选项,请在此处阅读。
获取和保存数据
您可以使用db_get、db_save和db_delete函数来处理那些表中的数据。
// Getting
$data = db_get("table=paintings")
// Saving
$save = array(
'name' => 'Mona Lisa',
'description' => 'Paiting by Leonardo da Vinci'
);
$id = db_save('paintings', $save);
// Deleting
db_delete('paintings', $id);
创建
db_save
函数接受表名作为第一个参数,将行数据作为第二个参数。要在一个表中创建行,只需不指定ID。
示例
$data = array(
'name' => 'Three Musicians',
'price' => 2700,
'description' => 'My greatest work'
);
db_save('paintings', $data);
读取
调用db_get
函数,并在参数数组中将table
键设置为从给定的数据库表中检索行。
示例
$rows = db_get(array('table' => 'paintings'));
在参数数组中将single
键设置为true
,以便函数返回单行。任何非保留键名都视为给定列名的WHERE
条件。有关更多详细信息,请参考db_get
函数文档。
示例
$row = db_get(array(
'table' => 'paintings',
'name' => 'Three Musicians',
'single' => true
));
或者,上述查询可以写成这样db_get('table=paintings&name=Three Musicians&single=true')
更新
如果 db_save
函数接收一个包含 id
键的数组,它将对该行执行更新操作。
示例
// Gets single row with id = 3 $row = db_get(array( 'table' => 'paintings', 'id' => 3, 'single' => true )); $row['title'] = 'My Awesome Painting'; echo 'Updating row with ID ', $row['id']; db_save('paintings', $row);
删除
在成功删除指定 ID 的行之后,db_delete
函数返回 true
。
示例
db_delete('paintings', $id = 3);