关于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','content'=>'root_content'), 'Topic' =>array('id'=>'topic_id','create_time','from'=>'topic_from','content','status','_on'=>'topic.id=topic.rootid') ); }
上面的语句执行的sql如下:
SELECT topic.id AS root_id,Topic.id AS topic_id FROM fl_topic topic JOIN fl_topic Topic ON topic.id=Topic.rootid
这句sql语句的错误很明显:因为sql是不区分大小写的,所以会直接报??Not unique table/alias: 'Topic'的错误。
?
继续来看这个函数:
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; }
上面的函数对于前面的‘topic’索引只是做了工厂方式的模型生成。而其中的class_exists是不区分大小写的,而数组的索引是区分大小写的,我们可以利用这个特性,把同一个表的引用用仅大小写不同的索引表示
再在后面跟上_as属性即可:
?
下面代码:
public $viewFields = array( 'topic'=>array('id'=>'root_id','content'=>'root_content','_as'=>'root_topic'), 'Topic' => array('id'=>'topic_id','create_time','from'=>'topic_from','content','status','_on'=>'root_topic.id=topic.rootid') );
?这样便可得到正确的sql语句了
SELECT root_topic.id AS root_id,Topic.id AS topic_id FROM fl_topic root_topic JOIN fl_topic Topic ON root_topic.id=topic.rootid
?
tp的确是国内一款少有的成熟的框架~~但有些地方还是要用些另类的技巧来使用,自连接表的视图模型使用总觉得还是比较尴尬的,期待有更好的方式可以达到自连接的目的
?