The above join function requires three parameters, which are:
join
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 modelassociated 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
, 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: