THINKPHP 关联模型 一对一

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

posted @ 2017-07-14 15:13  智昕  阅读(448)  评论(0编辑  收藏  举报