houmin0036

先分别创建三张表: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 }

 

完成! 

分类:

技术点:

相关文章:

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