Home  >  Article  >  PHP Framework  >  About ThinkPHP's join query not using the default table prefix

About ThinkPHP's join query not using the default table prefix

藏色散人
藏色散人forward
2021-04-30 09:26:212641browse

The following tutorial column will introduce to you how ThinkPHP's join query does not use the default table prefix. I hope it will be helpful to friends in need! ThinkPHP’s join related query does not use the default table prefix

Regarding ThinkPHP’s related query, the official document describes it like this:

The above join function requires three parameters, which are:
About ThinkPHPs join query not using the default table prefixjoin

The (complete) table name and alias to be associated, supports three writing methods:

写法1:[ '完整表名或者子查询'=>'别名' ]
写法2:'完整表名 别名'
写法3:'不带数据表前缀的表名'

condition

关联条件,可以为字符串或数组, 为数组时每一个元素都是一个关联条件。

type

关联类型,可以为: INNER、LEFT、RIGHT、FULL,不区分大小写,默认为INNER。

Different prefixes

Generally, they are queried in the same database. In this case, the

same table is used by default Prefix, such as (shop_)

, so when using

database model

associated query is often written like this:

Order::alias('o')
     ->join('user u', 'o.user_id = u.id')
     ->select();
In the above code, because it is using model query, so By default, the table prefix will be added. The complete table names of the two tables are shop_order and

shop_user

, and the association type defaults to INNER association. But if you associate a table with a different prefix (for example: pay_record) , the above query statement will obviously not work. In this case, you need to slightly modify the association statement. , the modified code is as follows:

Order::alias('o')
     ->join(['pay_record' => 'r'], 'o.pay_id = r.id')
     ->select();

In this way, the model can be used to associate tables with different prefixes for query. Summary

The above method is actually

modifying the join parameter in the join function from a string to an array

; ThinkPHP is an excellent development framework. The above The association method is only one of them. For more methods, please refer to the official manual: ThinkPHP official manual.

Related recommendations:
The latest 10 thinkphp video tutorials

The above is the detailed content of About ThinkPHP's join query not using the default table prefix. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete