Home  >  Article  >  Backend Development  >  ThinkPHP3.2中关联查询解决办法

ThinkPHP3.2中关联查询解决办法

WBOY
WBOYOriginal
2016-06-13 12:24:491199browse

ThinkPHP3.2中关联查询

CREATE TABLE `test_avatar` (<br />  `uid` int(11) unsigned NOT NULL DEFAULT '0',<br />  `avatar` varchar(255) NOT NULL DEFAULT '',<br />  PRIMARY KEY (`uid`)<br />) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br />INSERT INTO `test_avatar` VALUES (1,'./Uploads/admin.jpg');<br /><br />CREATE TABLE `test_pro` (<br />  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,<br />  `uid` int(11) unsigned NOT NULL DEFAULT '0',<br />  `name` varchar(255) NOT NULL DEFAULT '',<br />  PRIMARY KEY (`id`)<br />) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br />INSERT INTO `test_pro` VALUES (1,1,'产品1'),(2,1,'产品2');<br /><br />CREATE TABLE `test_user` (<br />  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,<br />  `username` varchar(50) NOT NULL DEFAULT '',<br />  `tel` int(11) unsigned NOT NULL DEFAULT '0',<br />  PRIMARY KEY (`id`)<br />) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br />INSERT INTO `test_user` VALUES (1,'admin',110);

如上三个表:用户头像表、产品表、用户表
怎样在模型中建立关联?
class ProModel extends RelationModel{<br /><br />	protected $_link=array(<br />		'Avatar'=>array(<br />			'mapping_type'=>self::HAS_ONE,<br />			'class_name'=>'Avatar',<br />			'foreign_key'=>'uid',<br />			'mapping_fields'=>'avatar',<br />			'as_fields'=>'avatar',<br />		),<br />        )<br />}

试过用HAS_ONE、BELONGS_TO、HAS_MANY、MANY_TO_MANY都不行,求大神指点
------解决思路----------------------
Model下建立ProModelModel.class.php
<br />class ProModelModel extends ViewModel {  <br />		<br />	public $viewFields = array(     <br />		'test_avatar'=>array(<br />				'id',...<br />			),     <br />		'test_pror'=>array(<br />			'id',...<br />			'_on'=>''<br />			),      <br />               'test_pror'=>array(<br />			'id',...<br />			'_on'=>''<br />			), <br />	   ); <br />}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn