Home > Article > PHP Framework > 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)
Device table
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:
<?php namespace Admin\Model; use Think\Model\RelationModel; class DoorcontrollerModel extends RelationModel{ protected $_link = array( 'Weixin'=>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 to PHP 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!