


About the principle and analysis of automatic filling in thinkPHP framework
This article mainly introduces the automatic filling of the thinkPHP framework. It analyzes the principles, usage methods and related operation precautions of the thinkPHP framework automatic filling in detail in the form of examples. Friends in need can refer to this article
Examples analyze the principles and usage of automatic filling in the thinkPHP framework. Share it with everyone for your reference, the details are as follows:
thinkphp has a method to automatically fill fields
The filling rules are as follows
array( array(完成字段1,完成规则,[完成条件,附加规则]), array(完成字段2,完成规则,[完成条件,附加规则]), ...... );
Note: After studying the source code, I found that there is actually a fourth parameter, which is used to pass parameters to functions or callback methods. Parameter 1 defaults to this field value, such as:
array('mobile','trim',3,'function',参数2,参数3'),
Verify payment dynamic and static
Static verification
The automatic verification rules of the model are pre-defined in the model class, and will be automatically verified after using the create
method.
The following is the official example
1. First define the verification rules in the model
namespace Home\Model; use Think\Model; class UserModel extends Model{ protected $_auto = array ( array('status','1'), // 新增的时候把status字段设置为1 array('password','md5',3,'function') , // 对password字段在新增和编辑的时候使md5函数处理 array('name','getName',3,'callback'), // 对name字段在新增和编辑的时候回调getName方法 array('update_time','time',2,'function'), // 对update_time字段在更新的时候写入当前时间戳 ); }
2. When calling, use the create
method to automatically fill in the
$User = D("User"); // 实例化User对象 if (!$User->create()){ // 创建数据对象 // 如果创建失败 表示验证没有通过 输出错误提示信息 exit($User->getError()); }else{ // 验证通过 写入新增数据 $User->add(); }
dynamic verification
following It is an official example
$rules = array ( array('status','1'), // 新增的时候把status字段设置为1 array('password','md5',3,'function') , // 对password字段在新增和编辑的时候使md5函数处理 array('update_time','time',2,'function'), // 对update_time字段在更新的时候写入当前时间戳 ); $User = M('User'); $User->auto($rules)->create(); $User->add();
The following is the core code analysis:
The create method will be called after calling autoOperation
Method, the method is as follows
/** * 自动表单处理 * @access public * @param array $data 创建数据 * @param string $type 创建类型 * @return mixed */ private function autoOperation(&$data,$type) { if(!empty($this->options['auto'])) { $_auto = $this->options['auto']; unset($this->options['auto']); }elseif(!empty($this->_auto)){ $_auto = $this->_auto; } // 自动填充 if(isset($_auto)) { foreach ($_auto as $auto){ // 填充因子定义格式 // array('field','填充内容','填充条件','附加规则',[额外参数]) if(empty($auto[2])) $auto[2] = self::MODEL_INSERT; // 默认为新增的时候自动填充 //这里的判断是关键,$type为当前的操作状态,值为1表示是插入,值为2表示是更新 //如果当前的$type状态值等于设置的值$auto[2]或者$auto[2]的值为3,代表需要填充 if( $type == $auto[2] || $auto[2] == self::MODEL_BOTH) { if(empty($auto[3])) $auto[3] = 'string'; switch(trim($auto[3])) { case 'function': // 使用函数进行填充 字段的值作为参数 case 'callback': // 使用回调方法 $args = isset($auto[4])?(array)$auto[4]:array(); if(isset($data[$auto[0]])) { array_unshift($args,$data[$auto[0]]); } if('function'==$auto[3]) { $data[$auto[0]] = call_user_func_array($auto[1], $args); }else{ $data[$auto[0]] = call_user_func_array(array(&$this,$auto[1]), $args); } break; case 'field': // 用其它字段的值进行填充 $data[$auto[0]] = $data[$auto[1]]; break; case 'ignore': // 为空忽略 if($auto[1]===$data[$auto[0]]) unset($data[$auto[0]]); break; case 'string': default: // 默认作为字符串填充 $data[$auto[0]] = $auto[1]; } if(isset($data[$auto[0]]) && false === $data[$auto[0]] ) unset($data[$auto[0]]); } } } return $data; }
About the usage of smarty loop nesting
About common errors when compiling configure in PHP
About the use analysis of static variables and static static variables in PHP
##
The above is the detailed content of About the principle and analysis of automatic filling in thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to Handle Autofill and Autocomplete in PHP Forms As the Internet develops, people increasingly rely on autofill and autocomplete features to simplify their operations on the website. Implementing these functions in PHP forms is not complicated. This article will briefly introduce how to use PHP to handle auto-fill and auto-complete of forms. Before we begin, we need to clarify what autofill and autocomplete are. Autofill refers to automatically filling in the fields in a form for users based on their previous input or history. For example, when the user enters an email

Using Java to implement automatic filling of form data and input suggestions In recent years, with the development of the Internet, filling in form data has become a part of our daily lives. However, filling in a large amount of form data often causes some trouble for users, especially when entering repeated data. In order to improve the user's filling efficiency and experience, we can use Java language to implement automatic filling of form data and input suggestions. This article will introduce how to use Java to implement this function and provide code examples for reference. First, we need

As web development continues to evolve, developers need to use some practical tools and frameworks to save time and effort while improving the quality of their applications. ThinkPHP is a popular PHP framework that greatly simplifies development and increases efficiency. In this article, we will learn how to use the latest version of ThinkPHP6 framework. Environmental requirements First, you need to confirm that your system meets the following requirements: PHP version 7.1 and above MySQL version 5.5 and above Composer is a

ThinkPHP is a well-known PHP open source framework. It is characterized by efficiency, simplicity, and ease of use, and can quickly build large-scale Web applications. This article will introduce you to the usage and precautions of the ThinkPHP framework. 1. Installation of ThinkPHP framework 1. Download the ThinkPHP framework. You can download the ThinkPHP compressed package on the official website (http://www.thinkphp.cn/) and unzip it. You can also install it through Composer

With the continuous development of the Internet, forms have become one of the functions we use on our daily websites. Getting users to fill out a form is undoubtedly a tedious task, so it’s necessary to use some tricks to simplify the process. This article will introduce techniques for implementing form autofill in PHP. 1. Use default value When setting the default value of the form, you can use the "value" attribute in the form tag to specify it. Here is an example: <inputtype="text"name=&q

In today's Internet era, WeChat has become an indispensable part of people's daily lives. Whether it is social networking, payment, shopping or messaging, WeChat plays an important role. Therefore, using the ThinkPHP framework for WeChat development has become the choice of many developers. ThinkPHP framework is a domestic PHP development framework. It has the characteristics of high development efficiency, strong scalability, and complete documentation. It is suitable for the development of WeChat public accounts, small programs, etc. This article will start with access to the WeChat public platform, message processing, and material management.

"Development Suggestions for Using the ThinkPHP Framework for RBAC Permission Management" With the development of the Internet, more and more Web applications need to implement permission management functions to ensure the security and controllability of the system. RBAC (Role-BasedAccessControl, role-based access control), as a mature permission management model, has been widely used in actual development. ThinkPHP is a popular PHP framework that provides rich functions and flexible extensions.

How to use PHP crawler to automatically fill forms and submit data? With the development of the Internet, we increasingly need to obtain data from web pages, or automatically fill in forms and submit data. As a powerful server-side language, PHP provides numerous tools and class libraries to implement these functions. In this article, we will explain how to use crawlers in PHP to automatically fill forms and submit data. First, we need to use the curl library in PHP to obtain and submit web page data. The curl library is a powerful
