thinkphp利用模型可以关联两个或者多个关联的数据表,方法就是利用建立模型来取数据库字段,举个栗子:
数据表tableA 中有id、content、time、uid等字段,数据表tableB中有id、username等字段,tableA中的字段uid存取对应的就是tableB中的id字段,那么如何在前台模板取出tableA和tableB中的任意字段,方法如下:
建立TaskModel.class.php:
<?php
/** * 工作计划模型 */
Class TaskViewModel extends ViewModel {
Protected $viewFields = array(
'task' => array(
'id', 'task', 'finishtime','rate', //取得task表中的部分字段,将在前台模板显示提取的字段
'_type' => 'LEFT' //数据表向左映射,leftjoin,添加后面数据表的字段内容
),
'user' => array(
'username','shortiphone', //
'_on' => 'task.uid = user.id'
),
);
}
?>建立TaskViewModule.class.php
<?php
/** * 工作计划模型 */
Class TaskViewModel extends ViewModel {
Protected $viewFields = array(
'task' => array(
'id', 'task', 'finishtime','rate','content','settime',
'_type' => 'LEFT'
),
'user' => array(
'username','shortiphone',
'_on' => 'task.uid = user.id'
),
);
}
?> 


还没有评论,来说两句吧...