ThinkPHP学习笔记1-----CURD
ThinkPHP为我们提供非常快捷的方法执行curd操作,怎么用还是看手册,这里主要写一点我自己经验
例如
(一) 访问:http://localhost:1243/think/index.php/Form/show 显示数据库列表函数
代码如下
public function show(){ //进行输出表格 $user=M("user"); $userArr=$user->select(); //dump($userArr); $this->assign("userArr",$userArr); $this->display(); }
1.在M()函数中,user代表的是表名,也就是数据库中的user表.如果无法实例化对象,检查:
(1).是否在conf/config.php中设置了 ‘DB_DSN’,‘DB_PREFIX’(如果表名存在前缀);
(2)是否存在 表前缀_user表
2.display(0是存在于Action类中的函数,就像Smarty,ThinkPHP 的Action相当于smarty中的实例化的$smarty类.如果未指定display页面,则会默认寻找tpl/Form/show.html
(二). 访问:http://localhost:1243/think/index.php/Form/add 新增数据到数据库中
代码如下:
//添加数据 public function add(){ // $model=D("Form"); $user=M("user"); //验证规则可以写在action中 $validate = array( array('name','require','用户名不能为空'), // 为空验证 array('name','','用户名已存在','1','unique','1'), array('mail','email','邮箱格式错误'), ); //将规则写入model $user->setProperty("_validate",$validate);
if(!isset($_POST['sub'])){ $this->display(); exit; } //表单提交 //unset($_POST['sub']); //create函数其实是对数据库进行一次验证,在数组里面有的列才会存在在create所返回的数组中,所以可以省去unset操作 //例如在数据库表中,不存在列 mail,但是表单提交上来的有mail,create进行验证后返回的数组中不会包含mail $res=$user->create(); if(!$res){ //错误提示 $error=$user->getError(); $this->assign("error",$error); $this->display(); exit; } $result=$user->add(); if($result>0){ $this->success("添加成功","__URL__/show"); }else{ $this->error("添加失败,请重试"); } }
(1). D()函数在3.1以后无法进行单独的表设置,如果在Model文件夹下定义了一个FormModel.class.php,再用D("Form")实例化该Model,会提示数据表 Form不存在
暂未找到方法,请大家赐教
(2).进行自动验证的时候,可以将验证的规则写在action中,如上代码
倘若非要写在一个自定义的Model中,则如下
protected $_validate=array( //验证规则可以写在action中 array('name','require','用户名不能为空'), // 为空验证 array('name','','用户名已存在','1','unique','1'), array('mail','email','邮箱格式错误'), );
(3).关于create函数和add函数,见如上程序代码中注释
欢迎提出问题,共同解决。

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
