Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to make thinkphp3.2 version of view model errors all over again.
Namespace Home \ Model;
Use Think \ Model \ ViewModel;
Class ViewFriendsModel extends ViewModel {
Public $ viewFields = array (
'Friend' => array ('id', 'uid', 'fid', 'truename', 'addtime', 'typeid', 'status '),
'User' => array ('nickname', '_ on' => 'friend. fid = user. id '),
);
}
1. namespace Home \ Model;
This must be written in the first line. If an error is reported, it means that it is not the first line. Sometimes the name is the same, but the error is still reported. In this case, there are still a few, this is because the file has a BOM header. This is usually because you have edited it in notepad. There are many solutions. If you use dreamweaver, you can find the file, modify the page properties, and encode the title to remove the checklist before the Unicode signature (BOM) (s.
2. Reference The namespace use Think \ Model \ ViewModel;
This is ignored by many colleagues. Because many students are greedy for the convenience of drawing, copy the previous model file and change the file name to write the view. Note that the namespace referenced in your previous Model is like this: use Think \ Model.
3. inherit class FriendsModel extends ViewModel
This class is ViewModel, not Model. View classes are different.
4. Table Name: friend, user
This is the name of the table in your database, and TP uses this name as the alias of this table name. If you set the prefix in the configuration, remember not to write the prefix to reduce the code word. The name in the database is n_friend and n_user.
The final expression is as follows: select * from n_friend friend join n_user user on friend. uid = user. id
5. Join table _ on
This is the same as above 3. Do not write a prefix for the table name.
6. File Name
Note that the file name must be capitalized and the class name in the Code must be the same as the file name. For example, the file name is FriendsModel. class. php, the class term should be FriendsModel, and the first letter should be capitalized.
Note that the file name must not have an underscore (_). Some people like to name User_ViewModel.class.php like this. The TP view mechanism should be unrecognized by the system. Write UserViewModel. class. php in this way.
7. Controller call
The instantiation model must use D (). If M () is used, it is wrong. This is what I do in the controller.
D ('viewds DS ')-> select ();
8. debug the view Model
This method can be used to debug the view model of thinkphp3.2.
Echo D ('viewfriends ')-> getLastSql ();
Use the getLastSql () method provided by TP to print the statement. Then run the statement in the mysql database to see what error is reported. Based on this error, debug our SQL statement.
9. View where ()
The where method of the view should be used in this way ('friend. fid = % d', $ fid) to indicate the table
Join the QQ Group to discuss more thinkphp problems: 323899029
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB