在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