知识笔记(五十七)———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();
    }
相关推荐
alexander06810 小时前
CSS 类选择器组合
前端·css
guodingdingh14 小时前
软件开发工作问题总结0718
java·开发语言·数据库
寅时码15 小时前
React 之死·终章:一个 useRef,把闭包陷阱、依赖数组、漫天 rerender 全送走
前端·react.js·ai编程
不好听61315 小时前
HTML 事件监听机制:从 DOM Level 0 到 React 合成事件
前端·react.js·html
wordbaby15 小时前
为什么刷新页面就 404?聊聊 SPA 路由与 Nginx 的那点事
前端
咖啡八杯16 小时前
GoF设计模式——解释器模式
java·后端·spring·设计模式
不好听61316 小时前
React 核心概念:JSX、组件与数据驱动
前端·react.js
触底反弹16 小时前
🔥 React 零基础入门(中):500 行屎山代码到组件化的蜕变
前端·javascript·react.js
不好听61316 小时前
React vs Vue:两大前端框架技术选型深度对比
前端·vue.js·react.js
GuWenyue16 小时前
Cursor黑盒拆解!1套LangChain.js手写Mini编程Agent,自动生成React项目,效率提升60%
前端·数据库·人工智能