ThinkPHP view.
Many new TP users do not know much about the view model in the model. Although there are detailed manuals on the official website, it is hard for beginners to understand!
First, let's briefly talk about the functions that the view model can implement. Basically, it is about the association of fields between the master table and the secondary table to implement multi-Table association query. Compared with the use of native SQL statements, it is much simpler.
First, when initializing a data table, instead of using the traditional M ('user'), the view model uses D ('userview'), for example:
The primary requirement is that the first letter of a few words is capitalized.
In the Lib/Model directory, create XXXViewModel. class. php (XXX is the name of any controller/the first letter in uppercase). The content format is as follows:
We recommend that you add '_ type' => 'left' to all arrays. Because when the data of the associated table is empty, the system reports an error.
First, the main table is the fields to be queried in the customer array. The p_id and B _id in the table are the aliases set for the associated tables, in fact, it is similar to the LeftJoin statement in native SQL.
As shown in, '_ table' writes the name of the table to be associated. It is worth noting that the table name needs to write the full name, including its prefix.
'_ As' is the alias you want to give it, and 'name' is the field you used later in the CURD operation,
'_ On' performs the Join Operation: master table. associated ID = sub-table (alias used ). primary Key ID. When they are equal, a field in the primary table is associated with a data entry in the secondary table. The Select operation is to display the associated content.
Of course, this is generally used to deal with complex table data associations. For example, if a primary table is associated with more than a dozen sub-tables, you will feel at ease when using the view model.
This is just a simple example of basic functions. I will explain it here, hoping to help some new TP users.
What is the difference between the visual model and the associated Model in thinkphp?
Look at your hobbies and habits.
How does the view model in THINKPHP implement COUNT query?
'Cate' => array ('id', 'name '),
'Category '=> array ('count (*)' => 'nums', '_ on' => 'blog. cid = cate. id '),