A summary of some skills in the development process using thinkphp. I will continue to add more when I find them in the future. More friends are welcome to leave comments below.
(1) Do not use {$_GET.id} or {$Think.get.id} directly in the template, because {$_GET.id} {$Think.get.id} does not have any Filtering, prone to XSS. It is recommended to use the I method, that is: {:I('get.id')}
(2) What should I do if I need to get the fields of a table in the database in thinkphp? An example is as follows:
$user=M('user'); $fields=$user->getDbFields();
The result will be returned as a one-dimensional array consisting of table fields.
(3) During the data modification process, if we only need to modify the value of a certain field, we can use the setField method instead of calling the save method every time, for example:
$user=M('user'); $user->where('id=2')->setField('username','www.phpernote.com');
(4) When comparison is involved, it is not necessary to use the if condition form. You can also write it in the following form:
<eq name="web" value="phpernote">value</eq> // name 变量的值等于 phpernote 就输出 <neq name="web" value="phpernote">value</neq> // name 变量的值不等于 phpernote 就输出 <gt name="age" value="5">value</gt> // name 变量的值大于 5 就输出 <egt name="age" value="5">value</egt> // name 变量的值大于等于 5 就输出 <lt name="age" value="5">value</lt> // name 变量的值小于 5 就输出 <elt name="age" value="5">value</elt> // name 变量的值小于等于 5 就输出
(5) In the delete operation of thinkphp, you can use delete directly to perform deletion without using where, for example:
$User->delete('2,5');//删除主键为2和5的数据
(6) Instructions on the usage of several quick operation functions of thinkphp.
C operation, operation (dynamic) configuration: mainly used in the Action method
Get:C('configuration parameters')
Settings: C('configuration parameters', new value)
A operation, quickly create Action objects:
$action=A('User'); Equivalent to $action=new UserAction();
D operation to quickly create model data objects:
$model=D('User'); Equivalent to $model=new UserModel();
S operation, quick operation cache method
Get: S('name')
Settings: S('name','value');
Delete: S('name',NULL);
F operation, a quick file data saving method, the usage method is the same as S operation.
(7) When naming the Model, it should be consistent with the table name in the database by default. For example, the name of the database corresponding to PhpernoteUserModel.class.php should be 'prefix_phpernote_user', and the call in the program code should be: D('PhpernoteUser');
(8) If caching is not required during the development process, the following definition can be made in the entry file:
define('NO_CACHE_RUNTIME',true);
(9) If you need to know some additional information during the development process, you can define it in the configuration file. Here are some common information definition methods:
'SHOW_RUN_TIME'=>true,//运行时间显示 'SHOW_ADV_TIME'=>true,//显示详细的运行时间 'SHOW_DB_TIMES'=>true,//显示数据库的操作次数 'SHOW_CACHE_TIMES'=>true,//显示缓存操作次数 'SHOW_USE_MEM'=>true,//显示内存开销
(10) Customize the Model class and use it when instantiating the model (the model is the database operation class), for example:
a. Create an IndexModel.class.php in the Model directory under the Lib directory in the project directory, and then write a method play in it. The content of this method is to output 1 to 10 in a loop.
b. Then in the method in Action, you can also write:
$index=M('index','IndexModel');//这里可以加上自定义的模型类一起实例化 $index->play();
Then 1 to 10 will be output.
(11) If you need to change the default template suffix when using thinkphp, you can define it as follows in the configuration file:
'TMPL_TEMPLATE_SUFFIX' => '.dwt'//这里就将模板后缀名改为了dwt