当前位置:网站首页>Curd used by hyperf
Curd used by hyperf
2022-07-26 00:40:00 【It's Anbin】
Continued : Pagoda installation hyperf
As mentioned above, how to install in the pagoda hyperf frame , The rest of the operation and maintenance panels are not bad , I won't go back to .
The tools used here phpstorm
tips: If you have laravel perhaps thinkphp5.1(6) Words , You will find that the general operation is consistent , It's easy to get started , Finally, be diligent in reading documents , Read the document in time if you don't understand .
One 、 Plug in installation
start-up hyperf After a successful visit , You also need to install two plug-ins
PHP Annotation
Swoole IDE Helper


Let's talk about the function :
PHP Annotation The purpose of this is to achieve php Annotation definition in , At the same time hyperf There is also a section about the role of routing annotations , Details move : annotation annotation annotation
After installing the plug-in , You can use
domain name :9501/ modular / controller / Method
This way to access
Such as this :
If the route annotation is not installed , Destined to be only config/routes.php The short route name is defined in the route file , Visit again .
Such as this :
Swoole IDE Helper: Used for compiling swoole A plug-in that prompts code when .
Two 、 The basic document explains :
Please configure according to the file directory mysql Connection or other information , If configured env Invalid configuration , Please go config/autoload/ Search for databases.php To configure mysql Information
3、 ... and 、 Code display
<?php
// Strict type checking (php7 To introduce )
declare(strict_types=1);
namespace App\Controller;
// Introducing models admins
use App\Model\Admins;
// Introduce automatic controller class
use Hyperf\HttpServer\Annotation\AutoController;
/**
* adopt @AutoController() Annotations define routes
* Document address :https://hyperf.wiki/2.2/#/zh-cn/quick-start/overview?id=%e9%80%9a%e8%bf%87-autocontroller-%e6%b3%a8%e8%a7%a3%e5%ae%9a%e4%b9%89%e8%b7%af%e7%94%b1
* Be sure to annotate the route , Otherwise, it can't be used domain name / modular / controller / Method This way to access
* Then you can only go by yourself config/routes.php( Routing file ) Custom short route access
*/
/**
* @AutoController()
*/
class HomeController extends AbstractController
{
public function __construct(Admins $admins)
{
$this->admins = $admins;
//$this->admins = new Admins(); // Of course, you can write the above code like this , But this is not dependency injection ...
}
public function say()
{
$name = $this->request->input('name', 'anbin');
$age = 18;
$hobby = ' sleep ';
return [
'age' => $age,
'hobby' => $hobby,
'message' => "Hello {$name}.",
];
}
/**
* increase
* Browser access : http://ip Address :9501/home/add
* file :https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%a3%80%e7%b4%a2%e5%8d%95%e4%b8%aa%e6%a8%a1%e5%9e%8b
*/
public function add()
{
// Model insert data
/*
$res = $this->admins->insert([
'username' => 'anbin',
'password' => '123456',
'sex' => '1',
'remarks' => 'anbin How handsome '
]);
*/
// After inserting data into the model, return the id
$res = $this->admins->insertGetId([
'username' => 'anbin',
'password' => '123456',
'sex' => '1',
'remarks' => 'anbin How handsome '
]);
return $this->rjson($res, ' Insert ');
}
/**
* to update
* Browser access : http://ip Address :9501/home/edit
* file : https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%9b%b4%e6%96%b0
* @return array
*/
public function edit()
{
$id = $this->request->input('id', 19);
//update to update id by 19 Of remarks attribute data
/*
$res = $this->admins
->where('id', $id)
->update(['remarks' => 'hyperf Update data update Method ']);
*/
//save to update id by 19 Of remarks attribute data , You need to check it out before updating
$res = $this->admins->query()->find($id);
$res->save(['remarks' => 'hyperf Update data save Method ']);
return $this->rjson($res, ' to update ');
}
/**
* Inquire about
* Browser access : http://ip Address :9501/home/select
* file :https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%a3%80%e7%b4%a2%e5%8d%95%e4%b8%aa%e6%a8%a1%e5%9e%8b
*/
public function select()
{
//$data = $this->admins->query()->find(1); // Native query Method query id by 1 The data of
//$data = $this->admins->where('id', 1)->first(); // Model first Method query id by 1 The data of
//$data = $this->admins->query()->find([1,8,10]); // Native query Inquire about id by 1,8,10 The data of
//$data = $this->admins->all(); // Model all Method query admins All data in the table
//$data = $this->admins->get(); // Model get Method query admins All data in the table
$data = $this->admins->paginate(2); // Model Auto paging , Each page is displayed 2 strip ; Report errors Class 'Hyperf\Paginator\Paginator' not fonund to this place, please https://hyperf.wiki/2.2/#/zh-cn/paginator?id=%e5%ae%89%e8%a3%85
return $data;
}
/**
* Delete
* Browser access : http://ip Address :9501/home/del
* file : https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e5%88%a0%e9%99%a4%e6%a8%a1%e5%9e%8b
* @return array
*/
public function del()
{
// Model delete single id data , No transmission id Delete by default id by 20 This data of
/*
$id = $this->request->input('id', 20);
$res = $this->admins->where('id', $id)->delete();
*/
// Divide by primary key id data , No transmission id Delete by default id by 20 This data of
/*
$id = $this->request->input('id', 20);
$res = $this->admins->destroy($id);
*/
// Model delete id by 20,21 The data of
$res = $this->admins->whereIn('id', [20, 21])->delete();
// Delete... By primary key id by 20,21 The data of
//$res = $this->admins->destroy([20, 21]);
return $this->rjson($res, ' Delete ');
}
public function rjson($res, $msg)
{
if (empty($res)) {
return ['code' => 450, 'msg' => $msg . ' Failure '];
}
return ['code' => 200, 'msg' => $msg . ' success '];
}
}
The model code , Abstract classes are removed here :

<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Model;
use Hyperf\DbConnection\Model\Model as BaseModel;
// Introduce soft deletion
use Hyperf\Database\Model\SoftDeletes;
class Admins extends BaseModel
{
use SoftDeletes;
}
because hyperf Inside did json Handle , So in return Return the normal array when , No need to be like tp perhaps laravel Same first json Again return
Effect display :
add to :

modify :

Delete :

Inquire about :

More chained queries and aggregate functions 、 Self increment and self decrement of fields 、 to update json Fields, etc
Please move to the model chapter : Model
Four 、 close debug

hyperf The default creation time field and update time field are created_at and updated_at , Pay attention to this point , If cli When an error is reported, remember to create these two fields ; If you don't want hyperf Take over these two fields , You need to set public $timestamps = false;
as follows :

边栏推荐
- 8 tips - database performance optimization, yyds~
- Preparation of bovine erythrocyte superoxide dismutase sod/ folic acid coupled 2-ME albumin nanoparticles modified by bovine serum albumin
- 解决背景图设置100%铺满时,缩放浏览器出现水平滚动条时,滚动条超出的部分背景图没有铺满的问题
- Sorting out the encapsulation classes of control elements in appium
- 前缀异或和,异或差分数组
- Seretod2022 track1 code analysis - task-based dialogue system challenge for semi supervised and reinforcement learning
- In order to grasp the redis data structure, I drew 40 pictures (full version)
- The way of understanding JS: what is prototype chain
- Mwec: a new Chinese word discovery method based on multi semantic word vector
- [redis] ① introduction and installation of redis
猜你喜欢

C # from entry to mastery (III)

Redis killed twelve questions. How many questions can you carry?

HNOI2012矿场搭建

HCIP第十三天

Verilog语法基础HDL Bits训练 06

MPLS experiment

Research progress of data traceability based on the perspective of data element circulation

Semaphore

Redis夺命十二问,你能扛到第几问?

Verilog grammar basics HDL bits training 05
随机推荐
Leetcode notes 350. Intersection of two arrays II
前缀异或和,异或差分数组
Azure Synapse Analytics 性能优化指南(1)——使用有序聚集列存储索引优化性能
【NumPy中数组相关方法】
数据库工具对决:HeidiSQL 与 Navicat
2022/7/24 考试总结
Redis夺命十二问,你能扛到第几问?
数据流通交易场景下数据质量综合管理体系与技术框架研究
Quick start sequence table chain table
Find the single dog (Li Kou 260)
[untitled] how to realize pluggable configuration?
Mwec: a new Chinese word discovery method based on multi semantic word vector
Solve page refresh without attaching data
[oops framework] interface management
Nodejs starts mqtt service with an error schemaerror: expected 'schema' to be an object or Boolean problem solving
MPLS experiment
Preparation of bovine erythrocyte superoxide dismutase sod/ folic acid coupled 2-ME albumin nanoparticles modified by bovine serum albumin
LCA three postures (multiplication, tarjan+ joint search set, tree chain dissection)
How to use 120 lines of code to realize an interactive and complete drag and drop upload component?
ShardingSphere数据分片