事务操作实例
/**
* 事务的使用
* @param Request $request
* @return array
*/
public function actionApi(Request $request)
{
$param = $request->post();
$param = [
'username' => $param['username'],
'password' => $param['password'],
'head_url' => $param['head_url'],
'time' => time(),
];
$result = $this->medoo->action(function() use ($param){
$pdosmt = $this->medoo->insert('user', $param);
if ($pdosmt->rowCount() > 0) {
return ['data' => true];
} else {
return ['msg' => '插入失败'];
}
});
if ($result) {
return ['data' => true];
} else {
return ['msg' => '系统错误'];
}
}
基本用法
控制器Contorller\Test\IndexController.php
<?php
namespace App\Http\Controllers\Test;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class IndexController extends Controller
{
/**
* @var \Medoo\Medoo
*/
protected $medoo;
public function __construct()
{
$this->medoo = app('medoo');
}
/**
* 查询多条数据
* @return array
*/
public function selectApi()
{
$data = $this->medoo->select('user', ['id', 'username', 'head_url', 'time'], ['is_delete' => 0]);
return ['data' => $data];
}
/**
* 查询单条数据
* @return array
*/
public function getApi()
{
$data = $this->medoo->get('user', ['id', 'username', 'head_url', 'time'], ['is_delete' => 0]);
return ['data' => $data];
}
/**
* 插入数据数据
* @return array
*/
public function insertApi(Request $request)
{
$param = $request->post();
if (empty($param['username'])) {
return ['msg' => '请输入您的姓名'];
}
if (empty($param['password'])) {
return ['msg' => '请输入您的密码'];
}
if (empty($param['head_url'])) {
return ['msg' => '请输入头像地址'];
}
$pdosmt = $this->medoo->insert('user', [
'username' => $param['username'],
'password' => $param['password'],
'head_url' => $param['head_url'],
'time' => time(),
]);
if ($pdosmt->rowCount() > 0) {
return ['data' => true];
} else {
return ['msg' => '插入失败'];
}
}
/**
* 更新数据数据
* @return array
*/
public function updateApi(Request $request)
{
$param = $request->post();
if (empty($param['id'])) {
return ['msg' => '参数id错误'];
}
if (empty($param['username'])) {
return ['msg' => '请输入您的姓名'];
}
if (empty($param['password'])) {
return ['msg' => '请输入您的密码'];
}
if (empty($param['head_url'])) {
return ['msg' => '请输入头像地址'];
}
$id = $this->medoo->get('user', 'id', ['id' => $param['id']]);
if (!$id) {
return ['msg' => '数据错误,找不到该数据'];
}
$pdosmt = $this->medoo->update('user', [
'username' => $param['username'],
'password' => $param['password'],
'head_url' => $param['head_url'],
'time' => time(),
], ['id' => $id]);
if ($pdosmt->rowCount() > 0) {
return ['data' => true];
} else {
return ['msg' => '更新失败'];
}
}
/**
* 删除数据
* @param Request $request
* @return array
*/
public function deleteApi(Request $request)
{
$id = $request->post('id');
if (!$id) {
return ['msg' => '数据错误,找不到该数据'];
}
$pdosmt = $this->medoo->delete('user', ['id'=>$id]);
if ($pdosmt->rowCount() > 0) {
return ['data' => true];
} else {
return ['msg' => '删除失败'];
}
}
}
路由组routes\api.php
Route::group(['namespace' => 'Test', 'prefix' => 'test'], function () {
Route::any('select', 'IndexController@selectApi');
Route::any('get', 'IndexController@getApi');
Route::any('insert', 'IndexController@insertApi');
Route::any('update', 'IndexController@updateApi');
Route::any('delete', 'IndexController@deleteApi');
});