知识笔记(五十七)———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();
    }
相关推荐
hoLzwEge7 分钟前
node-linker VS shamefully-hoist
前端·前端框架
袋鱼不重13 分钟前
解决 Web 端图片预览与下载颜色不一致的一种工程方案
前端·后端
风止何安啊19 分钟前
教你用 JS + AI 实现简单的爬虫,零门槛爬取网页信息
前端
cidy_9820 分钟前
codebase-memory-mcp 新手完全教程:让 AI 真正「理解」你的代码库
前端
牛奶27 分钟前
HTTPS你不知道的事
前端·https·浏览器
小小小小宇29 分钟前
前端 Vue 如何避免不必要的子组件渲染全解析
前端
cidy_981 小时前
codebase-memory-mcp 安装教程
前端
mt_z1 小时前
Webpack 与 Vite 完全指南
前端
灏仟亿前端技术团队1 小时前
B 端多弹窗越来越难维护?试试把弹窗交互 Promise 化
前端
奇奇怪怪的1 小时前
向量数据库选型与生产级实战
前端