知识笔记(五十七)———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();
    }
相关推荐
sun_weitao3 小时前
Chrome 扩展插件开发指南
前端·javascript·chrome
认真的小羽❅3 小时前
ThreadLocal 在虚拟线程间要小心
java·开发语言·jvm
美味蛋炒饭.3 小时前
Java八股面试(4)
java·开发语言·面试
SL_staff3 小时前
OKR落地难?技术团队如何用五层联动链打通执行断层
java·开源·全栈
省钱兄--zs3 小时前
无人自助拼豆系统架构设计与功能实现落地部署全流程实战指南
java·数据仓库·spring boot·系统架构·需求分析
Raas1003 小时前
AI网关能做调用审计吗?MAI Gateway(魔芋企业级AI网关)实战能力深度解读
java·服务器·网络·数据库·gateway·prompt·企业级产品
海绵宝宝转agent4 小时前
2026-10-14算法+面经总结
java·笔记·算法·缓存
小马不起床4 小时前
零基础玩转 FastAPI_12:前端详情增强与角色权限分配
前端·状态模式·fastapi
Java爱好狂.4 小时前
挑战一周速通Spring+SpringBoot+SpringCloud!
java·后端·spring·springboot·架构师·java面试·java程序员
skywalk81634 小时前
Monty / 千脑理论(Thousand Brains Project)学习笔记
笔记·学习·monty·千脑理论