Home > PHP Framework > ThinkPHP > body text

Analyze a ThinkPHP joint table query case

angryTom
Release: 2020-03-09 09:37:38
forward
2461 people have browsed it

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;,
                ),
        );
}
Copy after login

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!

Related labels:
source:www.100txy.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!