例:用户表  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模型定义的所有关联数据 

相关文章:

  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-06-18
  • 2021-06-16
猜你喜欢
  • 2022-12-23
  • 2021-05-01
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
相关资源
相似解决方案