一对多关联模型,BELONGS_TO

先分别创建三张表:test_user   test_message  

test_user 表里有id、name字段

test_message 表里有id、content、uid字段

 

然后建立一个Model

 1 <?php
 2     class MessageModel extends RelationModel{
 3         protected $_link = array(
 4             'User' => array(
 5                 'mapping_type' => BELONGS_TO, 
 6                 'foreign_key'=>'uid',
 7                 'mapping_name'=>'test_user', //表名全称(包括表前缀)
 8                 'mapping_fields' =>'name',
 9                 'as_fields' => 'name' 
10             )
11         );
12     }
13 
14 
15 ?>

 

 

 

接着建立一个Action

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

 

完成! 

posted @ 2015-10-05 16:29  哟风  Views(334)  Comments(0Edit  收藏  举报