Example Explanation of Multi table Association Query in thinkphp

  • 2021-08-10 06:59:46
  • OfStack

Example Explanation of Multi-table Association Query in thinkphp

When programming the back-end management system, 1 will use the framework to build the page quickly. Recently, I use thinkphp framework more. In fact, the application of thinkphp framework is to separate the front-end and back-end. The front-end user login query system is managed in home folder in thinkphp, and the back-end management system is managed in admin folder in thinkphp. By the way, mvc architecture is used when using thinkphp framework. mvc architecture is the structure of model (data model), view (view) and controller (controller). Here, the interface is controlled by view, and the role of controller is to manage view and controller. Detailed structure can be learned by querying thinkphp documents.

What we want to talk about today is the application of associated query of database tables encountered in the back-end management system.

The first thing to say is the application of query statements in thinkphp. Of course, it is not a simple query for a data table, but an association query between multiple tables. There are two methods for data association, join and table.

1. First, the table method is introduced to query the association between multiple tables

The front M is the M model in thinkphp, whose function is to select the data tables in the database, which tables to be associated with in table, which are conditionally associated with where, and which function of field is actually a filtering function, which can output the information you want or useful, so that the result after associating the tables is the data structure you want.


public function orderList(){
   $User=M("t_order");
   /* The associated query of the two tables gets the grade value */
   $userinfo = $User->table('t_order,t_commodity,t_user')->where('t_order.cname = t_commodity.cname and t_order.uname = t_user.uname ')->field('t_order_id,t_order.orderid,t_order.cname')->select();
   $this->assign("userInfo",$userinfo);
   $this->display("order-list");
}

2. The join method is used to query the association between multiple tables

The difference of table association is that join uses on to associate tables, and the rest is actually one sample, where is the association condition, and field is the filtered information (useful information for the next)


public function getBanner(){
 $bannerid=1;
 $banner=M("banner_item");
 $result=$banner->join('image ON banner_item.img_id = image.id')->where("banner_item.banner_id=".$bannerid)->field("key_word,type,banner_id,url,from")->select();
 echo json_encode($result);
}

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: