zoukankan      html  css  js  c++  java
  • PHP ThinkPHP 非常好用的增删改查方法

    获取列表数据(多条)

       /**
    * 获取页面列表
    * @param $params //查询条件 例:['id'=>['in','1,2']] ['status'=>1]
    * @param array $orderby //排序 例:['id'=>'desc']
    * @param array $limit //查询页数 例:[0,10]
    * @param bool $is_total 是否需要总数
    * @param string $fields 是否需要总数 例:'name'
    * @return array
    */
    public function resultsAction($params = [],$orderby = [],$limit = [],$is_total = true,$fields = ''){
    if(empty($orderby)){
    $orderby = array('id'=>'desc');
    }

    //列表模型
    $results = $this
    ->where($params)
    ->order($orderby)
    ->field($fields);

    //查询数量
    if(!empty($limit)){
    list($offset, $limit) = [$limit[0],$limit[1]];

    $results = $results->limit($offset,$limit);
    }

    //查询
    $results = $results->select();

    //把数据转为数组
    $results = collection($results)->toArray();

    //总数
    if (!$is_total){
    return $results;
    }

    //查询总数
    $total = $this
    ->where($params)
    ->count();


    return $result = array("total" => $total, "rows" => $results);
    }

    获取单条数据

       /**
    * 获取单数据
    * @param array $params
    * @param string $fields
    */
    public function resultAction($params = [],$fields = ''){
    $result = $this
    ->field($fields)
    ->where($params)
    ->find();

    if(!empty($result)){
    $result=$result->toArray();
    }
    return $result;
    }

    新增或修改数据

       /**
    * 新增或修改数据
    * @param array $params 更新内容
    * @param array $args 更新条件 存在值为更新
    */
    public function saveAction($params = [],$args = []){
    if(empty($args)){
    $save = $this
    ->allowField(true)
    ->save($params);
    }
    else{
    $save = $this
    ->allowField(true)
    ->update($params,$args);
    }

    return $save;
    }

    删除数据

       /**
      *删除
    * @param array $params //查询条件 例:['id'=>['in','1,2']] ['status'=>1]
    */
    public function delAction($params = []){
    $del = $this
    ->where($params)
    ->delete();
    return $del;
    }

    插入多条数据

       /**
    * 新增
    * @param array $params 更新内容
    */
    public function saveAllAction($params = []){
    $save = $this
    ->allowField(true)
    ->saveAll($params);

    return $save;
    }
  • 相关阅读:
    centos 6.8 配置 Redis3.2.5
    php将字符串转为二进制数据串
    php密码对称encrypt加密
    mysql 新建用户并赋予远程访问权限
    centos6.8 搭建postfix/dovecot邮件服务器
    centos 6.8 设置svn钩子同步至web目录
    百分百解决Job for network.service failed. See 'system的问题
    java锁机制详解
    解决网页打不开简书的问题
    SpringBoot读取不到application.yml
  • 原文地址:https://www.cnblogs.com/youantianqin/p/11944451.html
Copyright © 2011-2022 走看看