namespace Common\Model;
use Think\Model\ViewModel;
//视图模型
class CategoryViewModel extends ViewModel {
protected $viewFields = array(
'category' => array(
'*',
'_type' => 'LEFT'
),
'model' => array(
'name' => 'modelname',//显示字段name as model
'tablename' => 'tablename',//显示字段name as model
'_on' => 'category.modelid = model.id',//_on 对应上面LEFT关联条件
),
);
}
多表进行查询
protected $viewFields = array(
"Answer" => array(
"anid", "content", "time", "accept", "uid", "asid" => "answerid",
"_type" => "LEFT"
),
"Ask" => array(
"answer","asid" => "askid", "cid",
"_on" => "ask.asid = answer.asid",
"_type" => 'RIGHT'
),
"Category" => array(
"cid" => "cateid", "title",
"_on" => "ask.cid = category.cid"
),
);/**
* 关联模型
* 一对多~~~ 建议一的主表 多的位附表
*/
namespace Home\Model;
use Think\Model\RelationModel;
class AskModel extends RelationModel {
//Protected $tableName='userinfo';
protected $_link = array(
"answer" => array(
"mapping_type" => self::HAS_MANY,
"class_name" => "answer",
"mapping_name" => "lower",
"foreign_key" => "asid"
),
);
}