# **多态一对多**
#### :-: **数据表**
~~~
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `tp_article`
-- ----------------------------
DROP TABLE IF EXISTS `tp_article`;
CREATE TABLE `tp_article` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`content` text,
`status` int(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_article
-- ----------------------------
INSERT INTO `tp_article` VALUES ('2', '文章2', null, '1');
INSERT INTO `tp_article` VALUES ('3', '文章3', null, '1');
-- ----------------------------
-- Table structure for `tp_book`
-- ----------------------------
DROP TABLE IF EXISTS `tp_book`;
CREATE TABLE `tp_book` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`status` int(11) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_book
-- ----------------------------
INSERT INTO `tp_book` VALUES ('1', '乾坤大挪移', '1', '1');
INSERT INTO `tp_book` VALUES ('2', '六脉神剑', '2', '1');
INSERT INTO `tp_book` VALUES ('3', '如来神掌', '3', '1');
INSERT INTO `tp_book` VALUES ('4', '九阴真经', '4', '1');
INSERT INTO `tp_book` VALUES ('5', '降龙十八掌', '5', '1');
INSERT INTO `tp_book` VALUES ('6', '葵花宝典', '6', '1');
-- ----------------------------
-- Table structure for `tp_comment`
-- ----------------------------
DROP TABLE IF EXISTS `tp_comment`;
CREATE TABLE `tp_comment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`commentable_id` int(255) DEFAULT NULL,
`commentable_type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_comment
-- ----------------------------
INSERT INTO `tp_comment` VALUES ('2', '2', 'ArticleModel');
INSERT INTO `tp_comment` VALUES ('3', '2', 'ArticleModel');
INSERT INTO `tp_comment` VALUES ('6', '1', 'BookModel');
INSERT INTO `tp_comment` VALUES ('7', '1', 'BookModel');
~~~
#### :-: **模型**
~~~
<?php
namespace app\index\model;
use think\Model;
class ArticleModel extends Model
{
protected $table='tp_article';
/**
* 获取所有针对文章的评论。
*/
public function comments()
{
return $this->morphMany('CommentModel', 'commentable','ArticleModel');
}
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class BookModel extends Model
{
protected $table = 'tp_book';
protected $autoWriteTimestamp = true;
/**
* 获取所有针对书籍的评论。
*/
public function comments()
{
return $this->morphMany('CommentModel', 'commentable','BookModel');
}
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class CommentModel extends Model
{
protected $table='tp_comment';
/**
* 获取评论对应的多态模型。
*/
public function commentable()
{
return $this->morphTo('commentable');
}
}
~~~
#### :-: **控制器**
~~~
<?php
namespace app\index\controller;
use app\index\model\ArticleModel;
use app\index\model\AvatarModel;
use app\index\model\BookModel;
use app\index\model\CommentModel;
use app\index\model\MemberModel;
use app\index\model\TeamModel;
use think\Controller;
use think\Request;
class Test2 extends Controller
{
//多态一对多关联
//获取多态一对多关联
public function index()
{
$db = new ArticleModel();
$article = $db->get(2);
foreach ($article->comments as $comment) {
dump($comment);
}
}
//获取多态一对多关联
public function index1()
{
$db = new BookModel();
$book = $db->get(1);
foreach ($book->comments as $comment) {
dump($comment);
}
}
//从关联表获取主表信息
public function index2()
{
$db = new CommentModel();
$comment = $db->get(2);
//dump($article->comments);die;
$commentable = $comment->commentable;
dump($commentable);
}
~~~
# **多态一对一**
#### :-: **数据表**
~~~
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `tp_avatar`
-- ----------------------------
DROP TABLE IF EXISTS `tp_avatar`;
CREATE TABLE `tp_avatar` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`avatar` varchar(255) DEFAULT NULL,
`imageable_id` int(11) DEFAULT NULL,
`imageable_type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_avatar
-- ----------------------------
INSERT INTO `tp_avatar` VALUES ('1', '张三头像', '1', 'MemberModel');
INSERT INTO `tp_avatar` VALUES ('2', '李四头像', '2', 'MemberModel');
INSERT INTO `tp_avatar` VALUES ('3', '王五头像', '3', 'MemberModel');
INSERT INTO `tp_avatar` VALUES ('4', '黄队头像', '3', 'TeamModel');
INSERT INTO `tp_avatar` VALUES ('5', '蓝队头像', '2', 'TeamModel');
INSERT INTO `tp_avatar` VALUES ('6', '红队头像', '1', 'TeamModel');
-- ----------------------------
-- Table structure for `tp_member`
-- ----------------------------
DROP TABLE IF EXISTS `tp_member`;
CREATE TABLE `tp_member` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_member
-- ----------------------------
INSERT INTO `tp_member` VALUES ('1', '张三');
INSERT INTO `tp_member` VALUES ('2', '李四');
INSERT INTO `tp_member` VALUES ('3', '王五');
-- ----------------------------
-- Table structure for `tp_team`
-- ----------------------------
DROP TABLE IF EXISTS `tp_team`;
CREATE TABLE `tp_team` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_team
-- ----------------------------
INSERT INTO `tp_team` VALUES ('1', '红队');
INSERT INTO `tp_team` VALUES ('2', '蓝队');
INSERT INTO `tp_team` VALUES ('3', '黄队');
~~~
#### :-: **模型**
~~~
<?php
namespace app\index\model;
use think\Model;
class TeamModel extends Model
{
protected $table='tp_team';
/**
* 获取用户的头像
*/
public function avatar()
{
return $this->morphOne('AvatarModel', 'imageable','TeamModel');
}
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class MemberModel extends Model
{
protected $table='tp_member';
/**
* 获取用户的头像
*/
public function avatar()
{
return $this->morphOne('AvatarModel', 'imageable','MemberModel');
}
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class AvatarModel extends Model
{
protected $table='tp_avatar';
/**
* 获取头像对应的多态模型。
*/
public function imageable()
{
return $this->morphTo('imageable');
}
}
~~~
#### :-: **控制器**
~~~
<?php
namespace app\index\controller;
use app\index\model\ArticleModel;
use app\index\model\AvatarModel;
use app\index\model\BookModel;
use app\index\model\CommentModel;
use app\index\model\MemberModel;
use app\index\model\TeamModel;
use think\Controller;
use think\Request;
class Test2 extends Controller
{
//多态一对一关联
//获取多态一对一关联
public function index3()
{
$db= new MemberModel();
$member = $db->get(2);
dump($member->avatar);
}
//获取多态一对一关联
public function index4()
{
$db = new TeamModel();
$team = $db->get(2);
dump($team->avatar);
}
//从关联表获取主表信息
public function index5()
{
$db =new AvatarModel();
$avatar = $db->get(1);
dump($avatar->imageable);
}
}
~~~