Home > Article > Backend Development > thinkphp automatically completes session assignment sample code in the model, thinkphp sample code_PHP tutorial
I believe that users who have used thinkphp know that thinkphp’s model can complete many auxiliary functions, such as automatic verification, automatic completion, etc. Today, during development, I encountered the function of obtaining the session value and then automatically assigning the value in the automatic completion. See the code for details. ;
class ArticlelModel extends Model { protected $_auto = array ( array('addtime','time',1,'function'), array('username','getName',1,'callback') ); //这个函数获取session里的name值 protected function getName(){ return $_SESSION["name"]; } }
Here you need to pay attention to the difference between the last parameter function and callback;
function: When using a function, it will automatically go to Common/common.php to find the corresponding function;
callback: Use the callback method defined in the current model
b090ec905807429d36701b3f761bfb19hasItem($id)) { // If the item already exists, add its quantity directly $this->incNum($id ,$num); return; } $item = array();$item['id'] = $id; $item['name'] = $name; $item['price'] = $price;$item ['brand'] = $brand;$item['thumb'] = $thumb; $item['num'] = $num; $this->items[$id] = $item; return $this-> ;items[$id]; } /* Modify the quantity of goods in the shopping cart param int $id Product primary key param int $num Modified quantity of a certain product, that is, directly change the quantity of a certain product to $num */ public function modNum($id,$num=1) { if(!$this->hasItem($id)) { r...The rest of the text>>
1. Complete the most functions with the least code
2. Excellent concepts and innovative models
3. Have complete MVC and fully built-in implementation
4. Ideal performance and complete functions
5. Complete Chinese documentation and online manuals
6. Perfectly supports different databases
7. Has many enterprise-level features
8. Has fast BUG response and repair capabilities
9. Walked through After three years of accumulation and improvement of details
10. Have a community-based team and open source power
11. Have numerous third-party derivative open source projects
12. Allow you to stand out from other teams (^_^ )
Then ThinkPHP will be your wise choice! ThinkPHP has many excellent features, including:
Easy-to-use MVC mode
Original core compilation and project compilation mechanism
Built-in XML template engine, supporting tag library
Rich model support
CURD and operation highly automated support
Rich query language support
Automatic creation of directory structure
Distributed database support
Multi-database connection and switching support
ActiveRecords mode and ROR features
Flexible and simple Project configuration
Automatic model verification and processing
Static page generation and diversified caching mechanism
Rich database and PDO support
SEO and URL routing support
AJAX support
Easy-to-expand system Base class library
Automatic encoding conversion
Component and plug-in support
Role-based permission control system, detailed development guide and full Chinese comments
Hope to adopt it.