zoukankan      html  css  js  c++  java
  • tp模型和数据库操作方法

    一、新建的模型名和表名一样,采用驼峰式,如表名user_type模型取名为UserType

    namespace appindexmodel;
    use thinkModel;
    class UserType extends Model
    {

    }
    --------------

    控制器调用方法如下
    use appindexmodelUser;
    //插入方法一
    /*
    $user=new User;
    $user->username='hehe';
    $user->password='123456';
    $user->status='1';
    $user->save();
    */
    //插入方法二
    /*
    $data['username']='test';
    $data['password']='pass';
    User::create( $data);
    */

     //批量插入方法一
    /*
    $user=new User;
    $list=[
    ['username'=>'aaa','password'=>'123'],
    ['username'=>'bbb','password'=>'456']
    ];//二维数组
    $user->saveAll($list);
    */
    //以ID的方式查询数据
    /*
    $user=User::get(3);//查询ID为3的单条数据
    echo $user->username;//以对象的方式显示对应的字段值
    echo $user['password'];//以数组的方式显示
    */
    //按指定的字段查询User是模型类名 getByUsername中的Username是字段名 dome是值 查询user表中username='dome' 显示ID
    /*
    $user=User::getByUsername('dome');
    echo $user->id;
    */
    //多条件查询
    /*
    echo $user=User::get(['username'=>'dome','password'=>'pass5']);//查询关系是and
    echo $user=User::where(['username'=>'dome','password'=>'pass5'])->find();//查询关系是and
    */
    //查询所有数据
    /*
    $list=User::all();
    $list=User::all(['id'=>5]);
    */
    //更新数据方法一
    /*
    $user=User::get(5);//更新ID等于5
    $user->username='u5';
    $user->password='p5';
    $user->save();
    */
    //更新数据方法二
    /*
    $arr['username']='aaa';
    $arr['password']='bbb';
    User::update($arr,['id'=>5]);
    */
    //删除数据两种
    // User::get(5)->delete();//删除ID等于5
    //User::destroy(6);//删除ID等于6
     
  • 相关阅读:
    用Python完成Excel的常用操作
    用Python实现excel 14个常用操作
    ubuntu and centos各种上网代理设置
    vim 熟练度练习
    Ansible-playbook 使用方式 看一篇就够了
    python 使用ldap3 查询跨域的用户信息
    python pyinstaller 的使用
    vs code新建python虚拟环境
    vs code 远程开发环境设置
    上下文管理器(Context Manager)
  • 原文地址:https://www.cnblogs.com/bk7788/p/7256837.html
Copyright © 2011-2022 走看看