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

Analyze a ThinkPHP joint table query case

angryTom
angryTomforward
2020-03-09 09:37:382455browse

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:

<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class DoorcontrollerModel extends RelationModel{
    protected $_link = array(
        &#39;Weixin&#39;=>array(
            &#39;mapping_type&#39;      => self::HAS_ONE,
                &#39;foreign_key&#39;=>"id",//主表的外键对应的从表主键字段
                &#39;mapping_key&#39;=>&#39;weixin_id&#39;,//主表设置的外键
                &#39;as_fields&#39; => &#39;nickname&#39;,
                ),
        );
}

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!

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

Related articles

See more