封装thinkphp的增删改查

原文地址:封装thinkphp的增删改查 作者:1976252184

//添加数据的方法

function insert() {
  $name=$this->getActionName();//获取当前的控制名
  $model = D ($name);//
  if (false === $model->create ()) {
   $this->error ( $model->getError () );
  }
  //保存当前数据对象
  $list=$model->add ();
  if ($list!==false) { //保存成功
   $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );//跳转页面
   $this->success ('新增成功!');
                       
  } else {
   //失败提示
   $this->error ('新增失败!');
  }
 }

//展示添加界面

public function add() {
  $this->display ();
 }

//修改数据的方法

function update() {

  $name=$this->getActionName();
  $model = D ( $name );
  if (false === $model->create ()) {
   $this->error ( $model->getError () );
  }
  // 更新数据
  $list=$model->save ();
  if (false !== $list) {
   //成功提示
   $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
   $this->success ('编辑成功!');
  } else {
   //错误提示
   $this->error ('编辑失败!');
  }
 }

//显示编辑界面

function read() {
  $this->edit ();
 }

 function edit() {
  $name=$this->getActionName();
  $model = M ( $name );
  $map[$model->getPk ()] = $_REQUEST [$model->getPk ()];
  $vo = $model->where($map)->find();
  $this->assign ( 'vo', $vo );
  $this->display ();
 }

//删除方法

public function delete() {
  //删除指定记录
  $name=$this->getActionName();
  $model = M ($name);
  if (! empty ( $model )) {
   $pk = $model->getPk ();//获取当前的模块的主键
   $id = $_REQUEST [$pk];
   if (isset ( $id )) {
    $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
    $list=$model->where ( $condition )->setField ( 'status', - 1 );
    if ($list!==false) {
                                       
     $this->success ('删除成功!' );
    } else {
     $this->error ('删除失败!');
    }
   } else {
    $this->error ( '非法操作' );
   }
  }
 }

public function foreverdelete() {
  //删除指定记录
  $name=$this->getActionName();
  $model = D ($name);
  if (! empty ( $model )) {
   $pk = $model->getPk ();
   $id = $_REQUEST [$pk];
   if (isset ( $id )) {
    $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
    if (false !== $model->where ( $condition )->delete ()) {
     //echo $model->getlastsql();
                                       
     $this->success ('删除成功!');
    } else {
     $this->error ('删除失败!');
    }
   } else {
    $this->error ( '非法操作' );
   }
  }
  $this->forward ();
 }

public function clear() {
  //删除指定记录
  $name=$this->getActionName();
  $model = D ($name);
  if (! empty ( $model )) {
   if (false !== $model->where ( 'status=-1' )->delete ()) { // status=1 to status=-1
    $this->assign ( "jumpUrl", $this->getReturnUrl () );
    $this->success ( L ( '_DELETE_SUCCESS_' ) );
   } else {
    $this->error ( L ( '_DELETE_FAIL_' ) );
   }
  }
  $this->forward ();
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值