Thinkphp automatically completes the session assignment sample code in the model, and thinkphp sample code
I believe that users who have used thinkphp know that thinkphp models can complete many auxiliary functions, such as automatic verification and Automatic completion, today, during development, we encountered the function of automatically completing the task and obtaining the session value and then assigning values automatically. For details, refer to the code;
Class ArticlelModel extends Model {protected $ _ auto = array ('addtime', 'time', 1, 'function'), array ('username', 'getname ', 1, 'callback'); // This function gets the name value in the session protected function getName () {return $ _ SESSION ["name"];}
Note the difference between the function and callback of the last parameter;
Function: When a function is used, Common/common. php is automatically used to find the corresponding function;
Callback: Use the callback method defined in the current model
Thinkphp's session Shopping Cart. Thanks for the complete code examples.
<? Phpclass CartTool {private static $ ins = null; private $ items = array (); final protected function _ construct () {} final protected function _ clone () {}// obtain the instance protected static function getIns () {if (! (Self: $ ins instanceof self) {self: $ ins = new self ();} return self ::$ ins ;} // put the singleton object of the shopping cart in the session public static function getCart () {if (! Isset ($ _ SESSION ['cart']) |! ($ _ SESSION ['cart'] instanceof self) {$ _ SESSION ['cart'] = self: getIns ();} return $ _ SESSION ['cart'];} /* Add item param int $ id item primary key param string $ name item name param float $ item price param int $ num Number of purchased items */public function addItem ($ id, $ name, $ price, $ brand, $ thumb, $ num = 1) {if ($ this-> hasItem ($ id) {// if the item already exists, add the number $ 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 number of items in the shopping cart, param int $ id, item primary key, param int $ num, change the quantity of a product to $ num */public function modNum ($ id, $ num = 1) {if (! $ This-> hasItem ($ id) {r ...... remaining full text>
The following is an example of the Code in CURD in the thinkphp manual:
1. Complete the most functions with the least code
2. Excellent ideas and innovative models
3. Complete MVC and full built-in implementation
4. Ideal Performance and complete functions
5. Complete Chinese documents and online manuals
6. Perfect support for different databases
7. enterprise-level features
8. Fast BUG response and repair capabilities
9. After three years of accumulation and detailed improvements
10. Community-based teams and open-source forces
11. Many third-party derivative open-source projects
12. Better than other teams (^_^)
ThinkPHP will be your wise choice! ThinkPHP has many outstanding features, including:
Simple and Easy-to-use MVC Mode
Unique core compilation and project compilation mechanisms
Built-in XML template engine, supporting tag Library
Rich model support
Highly Automated Support for CURD and operations
Rich Query Language Support
Automatic directory structure Creation
Distributed Database Support
Multi-database connection and switchover support
ActiveRecords mode and ROR features
Flexible and simple project configuration
Automatic model verification and Processing
Static Page generation and diversified cache mechanisms
Rich database and PDO support
SEO and URL Routing Support
AJAX support
Scalable System Base Class Library
Automatic Transcoding
Support for components and plug-ins
Role-based permission control system detailed development guide and full Chinese comments
Hope to adopt it.