日期:2014-05-17  浏览次数:20640 次

关于Thinkphp框架视图模型调用的一些问题总结

对于tp框架中视图模型的调用在项目中比较常用,这次的毕业设计中对于碰到了一些问题写下总结:

?

一、自定义DAO的ORM的Model定义在视图调用时出现异常

?

当把Model定义成形如

class UserModel extends Model {

	private $ormObj;
	
	/**
	 * 
	 * 构造函数
	 */
	function __construct(){
		
		$this->ormObj=M('User');
		
	}
}

然后是视图模型的定义:

class MentionviewModel extends ViewModel {
	
    public $viewFields = array(
       'Mention'=>array('id','tid','uid'),
       'Topic'  =>  array('create_time','from'=>'topic_from','content','status','_on'=>'Mention.tid=Topic.id'),
       'User'  =>  array('nickname','homepage','avatar','_on'=>'Topic.uid=User.id')
    );
    
}

而输出的SQL语句确是:

SELECT Mention.id AS id,Mention.uid AS uid,Mention.tid AS tid,Topic.create_time AS create_time,Topic.from AS topic_from,Topic.content AS content,User.avatar AS avatar,User.nickname AS nickname,User.homepage AS homepage FROM fl_mention Mention JOIN Topic ON Mention.tid=Topic.id JOIN User ON Topic.uid=User.id WHERE Topic.status=1 and Mention.uid=1 ORDER BY Mention.id desc LIMIT 0,20

?对于要取别名的表名丢失了,这种DAO式的ORM调用时在视图模型中会出现找不到表名的情况。

?

在父类ViewModel中有这样一个函数,是提取表名的:

public function getTableName()
    {
        if(empty($this->trueTableName)) {
            $tableName = '';
            foreach ($this->viewFields as $key=>$view){
                // 获取数据表名称
                $class  =   $key.'Model';
                $Model  =  class_exists($class)?new $class():M($key);
                $tableName .= $Model->getTableName();
                // 表别名定义
                $tableName .= !empty($view['_as'])?' '.$view['_as']:' '.$key;
                // 支持ON 条件定义
                $tableName .= !empty($view['_on'])?' ON '.$view['_on']:'';
                // 指定JOIN类型 例如 RIGHT INNER LEFT 下一个表有效
                $type = !empty($view['_type'])?$view['_type']:'';
                $tableName   .= ' '.strtoupper($type).' JOIN ';
                $len  =  strlen($type.'_JOIN ');
            }
            $tableName = substr($tableName,0,-$len);
            $this->trueTableName    =   $tableName;
        }
        return $this->trueTableName;
    }

这里函数看到会去调用该类的超类Model的 $Model->getTableName() 这个函数:

?

public function getTableName()
    {
        if(empty($this->trueTableName)) {
            $tableName  = !empty($this->tablePrefix) ? $this->tablePrefix : '';
            if(!empty($this->tableName)) {
                $tableName .= $this->tableName;
            }else{
                $tableName .= parse_name($this->name);
            }
            $tableName .= !empty($this->tableSuffix) ? $this->tableSuffix : '';
            if(!empty($this->dbName))
                $tableName    =  $this->dbName.'.'.$tableName;
            $this->trueTableName    =   strtolower($tableName);
        }
        return $this->trueTableName;
    }

这个函数中这个语句empty($this->trueTableName)便是提取表名的

下面给出解决方案一:

class UserModel extends Model {
	
	protected $trueTableName = 'fl_user';  

	private $ormObj;
	
	/**
	 * 
	 * 构造函数
	 */
	function __construct(){
		
		$this->ormObj=M('User');
		
	}
}

?在该类中加入protected $trueTableName = 'fl_user'; 这个属性使触发getTablename函数时可以找到对应的表名;

解决方案二:

function __construct(){
		
	parent::__construct();
	$this->ormObj=M('User');
		
}

?在子类中将覆盖掉的父类构造函数重新引入

?

?

?

?

二、自连接表的视图模型调用:

?

形如:

class TopicviewModel extends ViewModel {
	
    public $viewFields = array(
       'topic'=>array('id'=>'root_id','