知识笔记(五十七)———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();
    }
相关推荐
滑动变滚动的蜗牛1 小时前
我发布了自己的个人小程序(诉述洞)
前端·ai编程·全栈
nice_lcj5201 小时前
Bean 的作用域:单例、原型及特殊作用域
java·spring·面试·原型·单例·bean作用域
ZYH_06012 小时前
Shell 第一阶段笔记:变量、引号与变量截取
前端·chrome·笔记
YIAN3 小时前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!
前端·websocket·node.js
这个昵称不容易3 小时前
SpringBoot 基于web的酒店客房管理系统设计与实现:176 个 Java 文件 233 个接口,会员预约到退房一条链
java·springboot·毕设
nice_lcj5203 小时前
Spring AOP 面向切面编程:切面解决什么问题
java·spring·面试·aop·切面·面向切面编程
星恒随风3 小时前
从0实现C++ AI大模型接入SDK(七):Ollama本地模型部署、NDJSON流式响应与LLMManager模型管理
linux·开发语言·c++·人工智能·笔记
考虑考虑4 小时前
JDK27的LazyConstant
java·后端·java ee
掘金安东尼4 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)
前端·程序员·agent
知识搬运临时工4 小时前
加一层连接池,TPS 从 26438 掉到 12923
java·后端