Home>Article>PHP Framework> Analyze a ThinkPHP joint table query case

Analyze a ThinkPHP joint table query case

angryTom
angryTom forward
2020-03-09 09:37:38 2399browse

Analyze a ThinkPHP joint table query case

Analysis of a ThinkPHP joint table query case

What I will introduce to you today is the thinkPHP joint table query case. Two tables are involved here, one is the device table doorcontroller and the administrator table weixin. The weixin_id in the device table is set as a foreign key in the management table, where the foreign key field weixin_id in the master table corresponds to the primary key field id in the slave table.

(Recommended tutorial:thinkphp tutorial)

Analyze a ThinkPHP joint table query case

Device table

Analyze a ThinkPHP joint table query case

Admin Table

In addition, we also need to reference the RelationModel.class.php file during the development process. This file is located in ThinkPHP\Library\Think\Model\RelationModel.class.php and is officially developed. As follows:

array( 'mapping_type' => self::HAS_ONE, 'foreign_key'=>"id",//主表的外键对应的从表主键字段 'mapping_key'=>'weixin_id',//主表设置的外键 'as_fields' => 'nickname', ), ); }

In this way, the result of die(var_dump($arr)) will add a nickname field on the basis of the original data, which realizes the master-slave table related query, by querying only the doorcontroller table At the same time, the nickname of its slave table weixin administrator is also queried.

For more tutorials related to PHP framework, please pay attention toPHP Chinese website!

The above is the detailed content of Analyze a ThinkPHP joint table query case. For more information, please follow other related articles on the PHP Chinese website!

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