知识笔记(五十七)———fastadmin编辑方法

通过一些方法来进行编辑

首先,在控制器中要引入 think\Db;的方法

复制代码
use think\Db;

然后通过引入的 Db来进行编辑方法的实现

复制代码
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        //Id为表中的Id
		$row = $this->model->get( [ 'Id' => $ids ] );
		// 如果id不存在
		if ( !$row ) {
			$this->error( __( 'No Results were found' ) );
		}
		//如果点击确认编辑执行
		if ( $this->request->isPost() ) {
			// 获取编辑所有字段和数据
			$params = $this->request->post( 'row/a' );
			//如果获取到parmas则执行
			if ( $params ) {
				Db::startTrans();
				//事务
				//成功
				try {
					//编辑
					$result = $row->save( $params );
					// 如果编辑失败则会提示
					if ( $result === false )exception( $row->getError() );
					Db::commit();
					//提交
				} catch ( \Exception $e ) {
					//失败
					Db::rollback();
					//回滚
					$this->error( $e->getMessage() );
				}
				$this->success();
				//编辑成功
			}
		}
		// 点击编辑是回显数据
		$this->view->assign( 'row', $row );
		return $this->view->fetch();

        // if ($this->request->isPost()) {
        //     $this->token();
        // }
        // $row = $this->model->get($ids);
        // $this->modelValidate = true;
        // if (!$row) {
        //     $this->error(__('No Results were found'));
        // }
        // $this->view->assign('row',$row);
        // return $this->view->fetch();
    }
相关推荐
haluhalu.3 分钟前
从 Linux 线程控制到 pthread 库
java·linux·服务器
indexsunny3 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务架构的三轮提问
java·spring boot·微服务·eureka·kafka·mybatis·spring security
三伏5227 分钟前
stm32f103系列手册IIC笔记2
笔记·stm32·嵌入式硬件
花间相见8 分钟前
【JAVA开发】—— HTTP常见请求方法
java·开发语言·http
鄭郑9 分钟前
【Playwright学习笔记 06】用户视觉定位的方法
笔记·学习
APIshop9 分钟前
实战代码解析:item_get——获取某鱼商品详情接口
java·linux·数据库
小二·11 分钟前
Python Web 开发进阶实战:AI 伦理审计平台 —— 在 Flask + Vue 中构建算法偏见检测与公平性评估系统
前端·人工智能·python
zhangchangz15 分钟前
Idea护眼插件分享之:Catppuccin Theme
java·ide·intellij-idea
走粥19 分钟前
选项式API与组合式API的区别
开发语言·前端·javascript·vue.js·前端框架
浮生醉清风i20 分钟前
Spring Ai
java·人工智能·spring