关联模型 has_one, belongs_to的外键是不同表的吗?

浏览:2385 发布日期:2014/05/27 分类:求助交流 关键字: foreign_key 关联模型
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class UserModel extends RelationModel{
    protected $_link = array(
        'Profile' => array(
            'mapping_type' => self::HAS_ONE,
            'class_name' => 'Profile',
            'mapping_name' => 'Profile',
            // 'condition' => 'id=3',
            'foreign_key' => 'user_aa'
        ),
        'class' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'class',
            'mapping_name' => 'class',
            'foreign_key' => 'class_id',
            // 'parent_key' => 'uid'
            // //'foreign_key'
        )

    );


}
?>
think_user表的字段如下:id,class_id,account,passwordthink_profile表字段如下:id,user_aa,emailthink_classs表字段如下:id    ,uid,classname按照上面的表字段,和上面的关联模型的关系,是可以获取数据的,
Action的代码如下public function testrelationmodel(){

  
        $UserProfile = D('User');
        $UserProfiles = $UserProfile->relation('Profile')->select();
        dump($UserProfiles);

        $User = D("User");
        //$UserInfo = $User->relation(true)->find(1);
        $UserInfo = $User->relation('class')->select();
        dump($UserInfo);
        //dump($User->getLastSql());
    }
我不明白的是:foreign_key到底是什么什么意思,这个 外键好像在 has_one的时候是指被关联的那个表里的某个对应字段,而在belongs_to的时候 这个 外键又属于 自身表里的某个字段,写完了这些文字 好像有点理解。

还是想请 大牛给讲讲 这个外键的概念,我自己摸索的 模模糊糊的。。
最佳答案
评论( 相关
后面还有条评论,点击查看>>