基础数据库建设,数据中心对接数据库
This commit is contained in:
104
Moncter/app/controller/UserController.php
Normal file
104
Moncter/app/controller/UserController.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
namespace app\controller;
|
||||
|
||||
use support\Request;
|
||||
|
||||
use app\model\User;
|
||||
|
||||
|
||||
class UserController
|
||||
{
|
||||
public function hello(Request $request)
|
||||
{
|
||||
$default_name = 'webman';
|
||||
// 从get请求里获得name参数,如果没有传递name参数则返回$default_name
|
||||
$name = $request->get('name', $default_name);
|
||||
// 向浏览器返回字符串
|
||||
return response('hello ' . $name);
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
// 单条创建
|
||||
$user = User::create([
|
||||
'name' => 'webman',
|
||||
'age' => 3,
|
||||
'email' => 'webman@example.com',
|
||||
'tags' => ['php', 'webman', 'mongodb'] // MongoDB 原生数组
|
||||
]);
|
||||
|
||||
// 批量创建
|
||||
// $users = User::insert([
|
||||
// ['name' => 'user1', 'age' => 20],
|
||||
// ['name' => 'user2', 'age' => 22]
|
||||
// ]);
|
||||
|
||||
return json([
|
||||
'success' => true,
|
||||
'id' => $user->_id, // 自动转为字符串
|
||||
'created_at' => $user->created_at->format('Y-m-d H:i:s')
|
||||
]);
|
||||
}
|
||||
|
||||
// 2. 查询文档(支持所有 Eloquent 查询方法)
|
||||
public function get(Request $request)
|
||||
{
|
||||
// 单条查询
|
||||
$user = User::where('name', 'webman')->first();
|
||||
// 或通过 ID 查询
|
||||
// $user = User::find('66e7d8...'); // _id 字符串
|
||||
|
||||
// 多条查询(分页、排序、筛选)
|
||||
$users = User::where('age', '>=', 1)
|
||||
->whereIn('tags', ['php']) // 数组字段查询
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(10); // 自动分页(返回 total、per_page 等字段)
|
||||
|
||||
// 聚合查询(统计、分组)
|
||||
$ageCount = User::where('age', '>=', 18)
|
||||
->groupBy('age')
|
||||
->selectRaw('age, count(_id) as total')
|
||||
->get();
|
||||
|
||||
return json([
|
||||
'user' => $user,
|
||||
'users' => $users->items(),
|
||||
'total' => $users->total(),
|
||||
'age_count' => $ageCount
|
||||
]);
|
||||
}
|
||||
|
||||
// 3. 更新文档
|
||||
public function update(Request $request)
|
||||
{
|
||||
$user = User::find($request->input('id'));
|
||||
if (!$user) {
|
||||
return json(['success' => false, 'msg' => '用户不存在'], 404);
|
||||
}
|
||||
|
||||
// 单个更新
|
||||
$user->age = 4;
|
||||
$user->save();
|
||||
|
||||
// 批量更新
|
||||
// User::where('name', 'webman')->update(['age' => 4]);
|
||||
|
||||
return json(['success' => true, 'updated_at' => $user->updated_at]);
|
||||
}
|
||||
|
||||
// 4. 删除文档
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$user = User::find($request->input('id'));
|
||||
if (!$user) {
|
||||
return json(['success' => false, 'msg' => '用户不存在'], 404);
|
||||
}
|
||||
|
||||
$user->delete();
|
||||
|
||||
// 批量删除
|
||||
// User::where('age', '<', 18)->delete();
|
||||
|
||||
return json(['success' => true]);
|
||||
}
|
||||
}
|
||||
37
Moncter/app/model/User.php
Normal file
37
Moncter/app/model/User.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// app/model/User.php
|
||||
namespace app\model;
|
||||
|
||||
use MongoDB\Laravel\Eloquent\Model;
|
||||
use MongoDB\Laravel\Relations\HasMany; // 若需关联查询(可选)
|
||||
|
||||
class User extends Model
|
||||
{
|
||||
// 对应 MongoDB 集合名(默认复数,可自定义)
|
||||
protected $collection = 'users';
|
||||
|
||||
// 主键(MongoDB 默认 _id,无需修改,自动转为字符串)
|
||||
protected $primaryKey = '_id';
|
||||
|
||||
// 主键类型(官方推荐显式声明)
|
||||
protected $keyType = 'string';
|
||||
|
||||
// 允许批量赋值的字段(白名单)
|
||||
protected $fillable = ['name', 'age', 'email', 'avatar'];
|
||||
|
||||
// 自动转换字段类型(ObjectId 转字符串、日期转 Carbon)
|
||||
protected $casts = [
|
||||
'_id' => 'string',
|
||||
'age' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'tags' => 'array', // 支持数组类型(MongoDB 原生支持数组)
|
||||
];
|
||||
|
||||
// 自动维护时间戳(created_at/updated_at,默认启用)
|
||||
// 若不需要可关闭:public $timestamps = false;
|
||||
|
||||
// 自定义时间戳字段名(可选)
|
||||
// const CREATED_AT = 'create_time';
|
||||
// const UPDATED_AT = 'update_time';
|
||||
}
|
||||
@@ -26,7 +26,8 @@
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"workerman/webman-framework": "^2.1",
|
||||
"monolog/monolog": "^2.0"
|
||||
"monolog/monolog": "^2.0",
|
||||
"mongodb/laravel-mongodb": "^4.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
|
||||
2006
Moncter/composer.lock
generated
2006
Moncter/composer.lock
generated
File diff suppressed because it is too large
Load Diff
27
Moncter/config/database.php
Normal file
27
Moncter/config/database.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
return [
|
||||
// 默认数据库连接(可选改为 mongodb)
|
||||
'default' => 'mysql', // 若需全局用 MongoDB,改为 'mongodb'
|
||||
|
||||
'connections' => [
|
||||
// ... 其他连接(如 mysql)保持不变
|
||||
|
||||
// MongoDB 官方连接配置
|
||||
'mongodb' => [
|
||||
'driver' => 'mongodb',
|
||||
'dsn' => 'mongodb://127.0.0.1:27017', // 集群可写:mongodb://node1:27017,node2:27017
|
||||
'database' => 'Moncter', // 目标数据库名
|
||||
'username' => 'Moncter', // 无认证则省略
|
||||
'password' => '123456', // 无认证则省略
|
||||
'options' => [
|
||||
'replicaSet' => '', // 副本集名称(无则留空)
|
||||
'ssl' => false, // 是否启用 SSL
|
||||
'connectTimeoutMS' => 3000, // 连接超时
|
||||
'socketTimeoutMS' => 5000, // 读写超时
|
||||
// 认证相关(若 MongoDB 启用认证)
|
||||
'authSource' => 'admin', // 认证数据库(默认 admin)
|
||||
'authMechanism' => 'SCRAM-SHA-256', // 认证机制(默认推荐)
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
25
Moncter/数据库列表.md
Normal file
25
Moncter/数据库列表.md
Normal file
@@ -0,0 +1,25 @@
|
||||
KR
|
||||
KR_KR
|
||||
KR_LinkedIn
|
||||
KR_存客宝
|
||||
KR_存客宝_四表重构KR_KR版
|
||||
KR_国外
|
||||
KR_户口
|
||||
KR_京东
|
||||
KR_酒店
|
||||
KR_卡套私域
|
||||
KR_快递
|
||||
KR_魔兽世界
|
||||
KR_企业
|
||||
KR_企业名录
|
||||
KR_人才库
|
||||
KR_商城
|
||||
KR_手机
|
||||
KR_顺丰
|
||||
KR_淘宝
|
||||
KR_腾讯
|
||||
KR_投资
|
||||
KR_微博
|
||||
KR_香港在大陆投资企业名录
|
||||
KR_销售额3000万元-5000万元企业名录
|
||||
KR_游戏
|
||||
Reference in New Issue
Block a user