zoukankan      html  css  js  c++  java
  • thinkphp创建对象及数据操作

    ThinkPHP有三种创建控制器对象的方式:

    1. 通过命名空间的路径找到类然后new出来例如:$dx = new HomeControllerIndexController();
    2. 通过A快捷函数创建对象A("模块/控制器")例如: $dx = A("HomeIndex");
    3. 通过R快捷函数创建对象并调用方法;R("Index/ShuChu")

    ThinkPHP操作数据库:

    首先需要在配置文件中配置数据库信息

    在创建模型对象执行sql语句

    创建模型对象有三种方式:

    1. 原始方式需要模型中建模型类,类名必须是数据库表名,例如:$m = new HomeModelInfoModel();
    2. 使用快捷函数D:$m = D("Info");需要注意的是如果没有模型类可以不加参数,对象就是model父类的对象

      3、使用快捷函数M:$m = M("Nation");

    操作数据库:

    首先介绍一下连贯函数,就是返回值是:$this,也就是说返回自身对象,可以继续调用函数

    数据库操作中基本的函数:select("主键值【,主键值】")、find("主键值"),聚合函数不是连贯函数。

    具体用法如下如下:(可以参考手册)

    操作数据库
    $attr = $m->select(); //查询所有数据
    $attr = $m->select("p001,p002,p003");
    $attr = $m->find("p001"); //找特定的数据根据主键值找

    where可以加查询条件
    $attr = $m->where("code='p001' or sex=true")->select();

    table可以切换要操作的表
    $attr = $m->table("Nation")->select();

    alias可以设置表的别名
    $attr = $m->alias("人员")->select();

    field可以指定查询的字段
    $attr = $m->field("code,name")->select();

    order可以加排序条件
    $attr = $m->order("Nation desc")->select();

    group可以分组
    $attr = $m->field("Nation")->group("Nation")->select();

    having可以加分组后的条件
    $attr = $m->field("Nation")->group("Nation")->having("count(*)>5")->select();

    join可以连接多个表,在field里面要给字段加别名
    $attr = $m->field("Info.Code as 代号,Info.Name as 姓名,Sex as 性别,Nation.Name as 民族名称")->join("Nation on Info.Nation = Nation.Code")->select();

    union联合查询
    $attr = $m->field("name")->union("select name from nation")->select();

    distinct去重
    $attr = $m->field("Nation")->distinct(true)->select();

    limit可以分页,参数第一个代表跳过多少条,第二个代表取多少条
    $attr = $m->limit(10,5)->select();

    page可以分页。第一个参数代表是当前页,第二个参数代表每页多少条
    $attr = $m->page(3,5)->select();

    取数据总条数
    $attr = $m->count("*");
    取某一列的和
    $attr = $m->table("Car")->sum("Price");
    取平均值
    $attr = $m->table("Car")->avg("Price");
    取最大值
    $attr = $m->table("Car")->max("Price");
    取最小值
    $attr = $m->table("Car")->min("Price");


    $sql = "select * from Info where Nation='n001'";
    $attr = $m->query($sql);

    $sql = "insert into Nation values('n104','按实际')";
    $attr = $m->execute($sql);

    $attr = $m->field("Info.Code as code,Info.Name as name,sex,Nation.Name as nationname,birthday")->join("Nation on Info.Nation = Nation.Code")->select();

    $this->assign("info",$attr);

    $this->assign("test",10);

    $this->display();

        

    ThinkPHP内置标签

    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    </tr>
    <foreach name="info" item="v" > //name是传过来的名字,item是数组元素
    <tr>
    <td><{$v.code}></td>
    <td><{$v.name}></td>
    <td><{$v["sex"]?"男":"女"}></td>  //三目运算符不支持点操作
    <td><{$v.nationname}></td>
    <td><{$v.birthday}></td>
    </tr>
    </foreach>


    </table>

    <if condition="$test gt 10"> //condition是条件,因为是标签为了防止歧义用备用词
    hello 5
    <else />
    hello 10
    </if>

  • 相关阅读:
    [Angular 9] Built-in template syntax $any
    [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
    [Angular 9 Unit testing] Stronger typing for dependency injection in tests
    [Angular] Preserve the current route’s query parameters when navigating with the Angular Router
    [Angular] Do relative routing inside component
    [Typescript] Make your optional fields required in TypeScript
    [Typescript] Exclude Properties from a Type in TypeScript
    [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
    [Debug] Set and remove DOM breakpoints
    【职业素养】4种让你显得没教养的做法
  • 原文地址:https://www.cnblogs.com/yongjiapei/p/5723718.html
Copyright © 2011-2022 走看看