# **多態一對多**
#### :-: **數據表**
~~~
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);
}
}
~~~