As a scripting language widely used on the web server, PHP has unique advantages in agile web development, with many successful cases, active communities, rich resources, and many frameworks. ThinkPHP, as an earlier and relatively mature domestic PHP framework, has a considerable number of users in the country. Through the "Geek Academy In-depth ThinkPHP Framework Video Tutorial", we will introduce the basic knowledge of ThinkPHP to prepare for subsequent actual projects.
Course playback address: //m.sbmmt.com/course/322.html
The teacher’s teaching style:
The lectures are friendly and natural, unpretentious, not pretentious, nor deliberately exaggerated, but talk eloquently and carefully, and the relationship between teachers and students is In an atmosphere of equality, collaboration, and harmony, silent emotional exchanges are carried out, and the desire and exploration of knowledge are integrated into simple and real teaching situations. Students gain knowledge through quiet thinking and silent approval
The more difficult point in this video is that the ThinkPHP model realizes automatic filling of data:
ThinkPHP automatic filling
ThinkPHP has a built-in automatic filling function of data objects, which can be used to handle automatic processing of default values, data filtering, and other system-written fields.
To use the autofill function, you only need to define the $_auto attribute (an array composed of multiple verification factors) in the corresponding Model class. The $_auto attribute is an array composed of multiple fill factors. The syntax format is as follows:
protected $_auto = array( array(填充字段,填充内容[,填充条件][,附加规则]) };
Autofill example
Examples of autofill that may be used when users register or modify information:
class UserModel extends Model{ protected $_auto = array ( // 新增的时候把status字段设置为1 array('status','1'), // 对password字段在所有情况下使用md5函数处理 array('password','md5',3,'function'), // 对username字段在新增时回调getName方法 array('username','getName',1,'callback'), // 对regdate字段在新增时写入当前时间戳 array('regdate','time',1,'function'), // 对regip字段在新增时写入用户注册IP地址 array('regip','get_client_ip',1,'function'), ); }
Like automatic verification, the automatic completion mechanism requires Use the create method to take effect:
$Article = D("User"); if(!$User->create()){ // 如果创建数据对象失败(可能是验证未通过等),输出错误提示信息 exit($Article->getError()); }else{ // 继续下一步流程如将数据写入数据表 }
Tips
Different from automatic verification, when automatic filling is invalid (such as calling a non-existent function or the automatically filled field does not exist, etc.) As a result, the creation of the data object (create()) fails. Whether the automatic filling is correct and effective can only be detected through debugging or actual data storage.
Dynamic change of automatic completion rules
Same as automatic verification, you can use the setProperty method in the operation method to dynamically change the automatic completion rules:
$Dao = D("User"); $auto = array ( // 仅对password字段进行处理 array('password','md5',1,'function') ); $User->setProperty("_auto",$auto); if(!$User->create()){ …… }
The above is the detailed content of Geek Academy's resource recommendations for in-depth ThinkPHP framework video tutorials. For more information, please follow other related articles on the PHP Chinese website!