在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