一对多关联模型,BELONGS_TO

先分别创建三张表:test_user   test_message  

test_user 表里有id、name字段

test_message 表里有id、content、uid字段

 

 

然后建立一个Model

<?php
    class MessageModel extends RelationModel{
        protected $_link = array(
            'User' => array(
                'mapping_type' => BELONGS_TO, 
                'foreign_key'=>'uid',
                'mapping_name'=>'test_user', //表名全称(包括表前缀)
                'mapping_fields' =>'name',
                'as_fields' => 'name' 
            )
        );
    }


?>

 

 

接着建立一个Action

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
        $db=D("Message");
        $list=$db->relation(true)->select();
        echo "<pre>";
        print_r($list);
    }
}

 

posted @ 2016-04-03 20:52  哟风  Views(137)  Comments(0Edit  收藏  举报