ThinkPHP的连贯操作方法中field方法有很多的使用技巧,field方法主要目的是标识要返回或者操作的字段,下面详细道来。
1、用于查询
在查询操作中field方法是使用最频繁的。
- $Model->field('id,title,content')->select();
这里使用field方法指定了查询的结果集中包含id,title,content三个字段的值。执行的SQL相当于:
- SELECT id,title,content FROM table
当然,除了select方法之外,所有的查询方法,包括find等都可以使用field方法,这里只是以select为例说明。
上面的例子也可以使用数组代替:
- $Model->field(array('id','title','content'))->select();
最终执行的SQL和上面等效。
^_^似乎看起来数组的用法过于复杂,不过先别下这个结论,后面就会明白数组用法的好处了。
数组方式的定义可以为某些字段定义别名,例如:
- $Model->field(array('id','title'=>'name','content'))->select();
执行的SQL相当于:
- SELECT id,title as name,content FROM table
如果你希望直接使用:
- $Model->field('id,title as name,content')->select();
可能会得到错误的结果。
对于一些更复杂的字段要求,数组的优势则更加明显,例如:
- $Model->field(array('id','concat(name,'-',id)'=>'truename','LEFT(title,7)'=>'sub_title'))->select();
执行的SQL相当于:
- SELECT id,concat(name,'-',id) as truename,LEFT(title,7) as sub_title FROM table
想必大家都明白了,对于需要在field中使用SQL函数的情况,数组方式可以很好的解决。
是不是field方法就这么点作用了呢?如果你这么认为,那就太低估ThinkPHP的field方法了,ThinkPHP考虑的细节远比你想象的要周到^_^。
先看下面的情况,如果有一个表有非常多的字段,而且有两个需求,首先要求需要获取所有的字段,这个也许很简单,因为不调用field方法或者直接使用空的field方法都能做到,事实上,的确如此:
- $Model->select();
- $Model->field()->select();
- $Model->field('*')->select();
上面三个用法是等效的,都相当于执行SQL:
- SELECT * FROM table
但是这并不是我说的获取所有字段,我希望显式的调用所有字段(对于对性能要求比较高的系统,这个要求并不过分,起码是一个比较好的习惯),那么OK,仍然很简单,下面的用法可以完成预期的作用:
- $Model->field(true)->select();
fied(true)的用法会显式的获取数据表的所有字段列表,哪怕你的数据表有100个字段。
第二个需求是我希望获取排除content字段(文本字段的值非常耗内存)之外的所有字段值,我们就可以使用field方法的排除功能,例如下面的方式就可以实现所说的功能:
- $Model->field('content',true)->select();
要排除更多的字段也可以:
- $Model->field('user_id,content',true)->select();
- //或者用
- $Model->field(array('user_id','content'),true)->select();
2、用于写入
除了查询操作之外,field方法还有一个非常重要的安全功能--字段合法性检测(注意:该功能3.1版本开始才能支持)。field方法结合create方法使用就可以完成表单提交的字段合法性检测,如果我们在表单提交的处理方法中使用了:
- $Model->field('title,email,content')->create();
即表示表单中的合法字段只有title,email和content字段,无论用户通过什么手段更改或者添加了浏览器的提交字段,都会直接屏蔽。因为,其他是所有字段我们都不希望由用户提交来决定,你可以通过自动完成功能定义额外的字段写入。
总结
从field方法的用法我们应该可以了解什么叫做小方法大用法,当然,我们只是希望ThinkPHP给你的开发带来更多的方便和惊奇,欢迎大家一起来分享你的用法和心得!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software