知识笔记(五十七)———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();
    }
相关推荐
苦 涩11 小时前
考研408笔记之计算机网络(六)——应用层
笔记·计算机网络·考研408
Lyyaoo.12 小时前
【JAVA基础面经】JVM的内存模型
java·开发语言·jvm
杨凯凡12 小时前
【017】泛型与通配符:API 设计里怎么用省心
java·开发语言
IT利刃出鞘12 小时前
Spring工具类--ObjectUtils的使用
java·后端·spring
还有你Y16 小时前
Shell 脚本语法
前端·语法·sh
踩着两条虫18 小时前
如何评价VTJ.PRO?
前端·架构·ai编程
MY_TEUCK19 小时前
Sealos 平台部署实战指南:结合 Cursor 与版本发布流程
java·人工智能·学习·aigc
Mh19 小时前
鼠标跟随倾斜动效
前端·css·vue.js
我爱cope19 小时前
【从0开始学设计模式-10| 装饰模式】
java·开发语言·设计模式
朝新_20 小时前
【Spring AI 】图像与语音模型实战
java·人工智能·spring