例:用户表  tp_user    字段:id   name         卡片表:tp_card    字段:id  uid  name

建立模型

<?php
namespace Index\Model;
use Think\Model\RelationModel;
class UserModel extends RelationModel{
    protected $tableName = 'User';
    protected $_link = array(
        'card' =>array(
            'mapping_type'=>self::HAS_ONE,
            'foreign_key'=>'uid',
        )
    );
 }

 控制器查询

    public function index(){
        // 判断用户是否登录
        is_login();
        $user_info = D('user')->where(array('id'=>session('userid')))->relation(true)->select();
        var_dump($user_info);  //打印
    }

Relation(true)会关联保存User模型定义的所有关联数据 

相关文章:

  • 2021-09-01
  • 2021-11-19
  • 2019-12-06
  • 2021-04-04
  • 2021-09-05
  • 2017-11-30
  • 2021-04-03
  • 2021-09-05
猜你喜欢
  • 2021-09-08
  • 2021-09-08
  • 2021-11-19
  • 2018-11-10
  • 2021-06-09
  • 2021-11-28
  • 2020-01-26
  • 2021-09-05
相关资源
相似解决方案