Thinkphp 多表查询

在model里进行关联,在 model里这么写,例如:ArticleViewModel.class.php

<?php
class ArticleViewModel extends Model{
	protected $viewModel = true;
	protected $masterModel = 'a';
	protected $viewFields = array(
		'a'=>array('id','subject','_type'=>'LEFT'),
		'b'=>array('title','_on'=>'a.gid=b.id'),
		);
        }
?>


那么就可以将表a.gid和表b的id关联起来了

<?php
 public function read(){      
        $Article=D("ArticleView");//调用上面的ArticleViewModel
        $list=$Article->find();
        echo $Article->getlastsql();
        }
?>

 

然后你会打印出类似:SELECT a.id AS id,a.subject AS subject b.title AS title FROM xx_a a LEFT JOIN xx_b b ON a.gid=b.id

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章