View models and pagination

Source: Internet
Author: User

A View Model
The correlation model can solve a pair of one or one-to-many and many-to-many associated operations. They are generated by generating multiple SQL statements
Then the array is assembled to get the final desired result. For one-on-one such has_one and belongs_to multiple tables
The view model can be used to correlate queries. It uses a federated query (join), which is ideal for such query requirements.

In Weibo/home/controller/usercontroller. the code in class. PHP is:

1 class extends Controller {2      Public function index () {3         $user = D (' Userview '); 4         Var_dump ($user->group (' id '),Select ()); 5     }6  }

Note that tables with no userview in the data table

New Weibo/home/model/userviewmodel. Class. PHP , the code is:

1<?PHP2 namespace Home\model;3  UseThink\model\viewmodel;4 5 classUserviewmodelextendsViewModel {6     protected $viewFields=Array(7' User ' =Array(' id ', ' user ', ' email ',),8' Card ' =Array(' Code ', ' _on ' = ' user.id=card.uid '),9         );Ten}

At this point, you can connect the user table and the card table in the data table:

Where the user table is:

The card table is:

The data after the connection is:

PS: Use the associated query to display the User table and contain the data associated with the Card table, which is not associated is ignored. If
If you want to make an unrelated query, you can use left-hand query.

1 // Left JOIN 2 Array (' id ', ' user ', ' email ', ' _type ' = ' left '),
1 // using group grouping to remove duplicate data 2 Var_dump ($user->group (' id ')->select ());
1 // use Count to get the number of bars associated 2 Array (' id ', ' user ', ' email ', ' _type ' = ' left ', ' COUNT (content.id) ' = ' abc '),

Second, paging function

In Weibo/home/controller/usercontroller. insert code in class. PHP :

1<?PHP2 namespace Home\controller;3  UseThink\controller;4  UseThink\page;5 6 classUsercontrollerextendsController {7      Public functionindex () {8         $user= D (' User ');9         $count=$user-Count();Ten         $page=NewPage ($count, 1); One         $page->setconfig (' first ', ' home '); A         $page->setconfig (' prev ', ' prev '); -         $page->setconfig (' Next ', ' next page ')); -         $page->setconfig (' Last ', ' End '); the         $page->setconfig (' theme ', ' Total%total_row% data Total%total_page% page%first%%up_page%%link_page%%down_page%%END% '); -         $show=$page-Show (); -         $list=$user->limit ($page->firstrow,$page->listrows)Select (); -         $this->assign (' list ',$list); +         $this->assign (' page ',$show); -         $this-display (); +     } A}

The code in weibo/home/view/user/index.html is as follows:

1 <TableBorder= "1">2      <TR><th>Id</th><th>User</th><th>Email</th></TR>3      <foreachname= "List"Item= "obj">4          <TR><TD>{$obj. ID}</TD><TD>{$obj. User}</TD><TD>{$obj. Email}</TD></TR>5      </foreach>6 </Table>7{$page}

The displayed effects are as follows:

Note: There is no "home" and "last", next time remember to look at the manual where the problem

1 // Set page name of the variable 2 ' var_page ' = ' PAGE ',

If not set, the URL is http://localhost/demo39/index.php/user/index/p/2.html

Set to: http://localhost/demo39/index.php/user/index/page/2.html

Another way of paging is:
In Weibo/home/controller/usercontroller. the code in class. PHP is:

1<?PHP2 namespace Home\controller;3  UseThink\controller;4  UseThink\page;5 6 classUsercontrollerextendsController {7      Public functionindex () {8         $user= D (' User ');9         $list=$user->page ($_get[' Page ']. ', 1 ')Select ();Ten         $count=$user-Count(); One         $page=NewPage ($count, 1); A         $show=$page-Show (); -         $this->assign (' list ',$list); -         $this->assign (' page ',$show); the         $this-display (); -     } -}

Notice how many bars are displayed on each page, $page = New page ($count, 1) , and the following 1 must

and $list = $user->page ($_get[' page ']. ', 1 ')->select (); 1 in the same.

The code in weibo/home/view/user/index.html remains the same

View models and pagination

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.