ThinkPHP Database View model

Source: Internet
Author: User
Meaning: The View in this article refers to the database View model, rather than the View class in ThinkPHP to implement the ThinkPHP database View model.

Note: The View in this article refers to the database View model, rather than the View in ThinkPHP.
View class implementation.

A database View is a virtual table that extracts the required data columns from one or more basic tables as needed. In this way, you do not have to query Table B and Table c based on table.
Tables.

Views are not supported in some databases. ThinkPHP
Simulate and implement the database view, which can be used for multi-table joint query.

To use the View model in ThinkPHP, you only need to inherit the ViewModel and then set
ViewFields attribute. use the D method to instantiate the model.
View model instance

The existing user table and article table are as follows (the table prefix is
Test _):
User table (omitted ):
Article table (omitted ):
Create a View model file




  1. Class ArticleViewModel extends ViewModel {
  2. Public $ viewFields = array (
  3. 'Article' => array ('aid ', 'title', 'content', 'uid '),
  4. 'User' => array ('username', '_ on' => 'article. uid = user. uid '),
  5. );
  6. }
  7. ?>

In this view model file, the model class inherits the ViewModel
View model and define $ viewFields attributes. each element includes a data table and the fields to be queried. In each table element
Element to define the association query conditions.

Save the file as Lib/Model/ArticleViewModel. class. php.
In the Action
Use View model in operation

In module operations, use the D method to instantiate the View model:




  1. Class ArticleAction extends Action {
  2. Public function index (){
  3. Header ("Content-Type: text/html; charset = utf-8 ");

  4. $ Dao = D ('articleview'); // instantiate the View
  5. $ Article_list = $ Dao-> select ();
  6. Print_r ($ article_list );
  7. Echo'

    ';
  8. // Print the executed SQL statement
  9. Echo: '. $ Dao-> getLastSql ();
  10. }
  11. }
  12. ?>

Access this method and print the result as follows:




  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [Aid] => 1
  6. [Title] => Article 1
  7. [Content] => Article 1 details ......
  8. [Username] => admin
  9. )

  10. [1] => Array
  11. (
  12. [Aid] => 2
  13. [Title] => Article 2
  14. [Content] => Article 2 details ......
  15. [Username] => Jack
  16. )

  17. )

The executed SQL statement is:




  1. SELECT article. aid AS aid, article. title AS title, article. content AS content,
  2. Article. uid AS uid, user. username AS username FROM test_article article JOIN test_user user ON
  3. Article. uid = user. uid

From the above results, we can see that the view model simulates the implementation of the database view by using JOIN queries to aggregate data from multiple tables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.