知识笔记(五十七)———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();
    }
相关推荐
我要神龙摆尾3 分钟前
约定俗成的力量--java中泛型的意义和用法
java·开发语言
毅炼12 分钟前
hot100打卡——day14
java·数据结构·算法·leetcode·ai·深度优先·哈希算法
毕设源码-朱学姐15 分钟前
【开题答辩全过程】以 基于HTML5的购物网站的设计与实现为例,包含答辩的问题和答案
前端·html·html5
C雨后彩虹23 分钟前
优雅子数组
java·数据结构·算法·华为·面试
梦65025 分钟前
CSS 元素垂直水平居中的 8 种方法
前端·css
一嘴一个橘子26 分钟前
springmvc 全局异常处理 and 拦截器
java
wangmengxxw26 分钟前
SpringAI-mysql
java·数据库·人工智能·mysql·springai
Coder_Boy_29 分钟前
基于SpringAI的在线考试系统-数据库设计核心业务方案
java·数据库·spring boot·ddd·tdd
power 雀儿31 分钟前
大模型基础:概念理解与 C++Token 化实现笔记
笔记
一嘴一个橘子31 分钟前
springmvc 参数校验
java