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

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