知识笔记(五十七)———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();
    }
相关推荐
寻星探路16 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
崔庆才丨静觅17 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
曹牧18 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby606118 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了18 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅18 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
wdfk_prog18 小时前
[Linux]学习笔记系列 -- [drivers][input]input
linux·笔记·学习
崔庆才丨静觅18 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
ouliten19 小时前
cuda编程笔记(36)-- 应用Tensor Core加速矩阵乘法
笔记·cuda