#### :-: **控制器**
~~~
<?php
namespace app\index\controller;
use app\index\model\ArticleModel;
use app\index\model\CommentModel;
use think\Controller;
use think\Request;
class Test extends Controller
{
//关联查询
//我们可以通过下面的方式获取关联数据
public function index()
{
$db = new ArticleModel();
$article = $db->get(2);
// 获取文章的所有评论
dump($article->comments);
// 也可以进行条件搜索
dump($article->comments()->where('status', 1)->select());
}
//根据关联条件查询
//可以根据关联条件来查询当前模型对象数据,例如:
public function index1()
{
$db = new ArticleModel();
// 查询评论超过3个的文章
//$list = $db->has('comments','>=',3)->select();
// 查询评论状态正常的文章
$list = $db->hasWhere('comments', ['status' => 1])->select();
dump($list);
}
//关联新增
public function index2()
{
$db = new ArticleModel();
$article = $db->find(2);
// 增加一个关联数据
//$article->comments()->save(['content'=>'test']);
// 批量增加关联数据
$article->comments()->saveAll([
['content' => 'thinkphp'],
['content' => 'onethink'],
]);
if ($article) {
return '添加成功';
}
return '添加失败';
}
//相对关联查询
public function index3()
{
$db = new CommentModel();
$comment = $db->get(2);
//dump($comment);die;
dump($comment->article);
}
//关联删除
public function index4()
{
$db = new ArticleModel();
$article = $db->get(1, 'comments');
//dump($article);die;
$article->together('comments')->delete();
}
}
~~~
#### :-: **模型**
~~~
<?php
namespace app\index\model;
use think\Model;
class ArticleModel extends Model
{
protected $table='tp_article';
public function comments()
{
return $this->hasMany('CommentModel','article_id','id');
}
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class CommentModel extends Model
{
protected $table='tp_comment';
public function article()
{
return $this->belongsTo('ArticleModel','article_id','id');
}
}
~~~
#### :-: **数据库**
~~~
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,
`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', '1');
INSERT INTO `tp_article` VALUES ('3', '文章3', '1');
-- ----------------------------
-- Table structure for `tp_comment`
-- ----------------------------
DROP TABLE IF EXISTS `tp_comment`;
CREATE TABLE `tp_comment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(11) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`status` int(11) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_comment
-- ----------------------------
INSERT INTO `tp_comment` VALUES ('2', '2', '文章2评论1', '1');
INSERT INTO `tp_comment` VALUES ('3', '3', '文章3评论1', '1');
INSERT INTO `tp_comment` VALUES ('6', '2', '文章2评论2', '1');
INSERT INTO `tp_comment` VALUES ('7', '3', '文章3评论2', '1');
INSERT INTO `tp_comment` VALUES ('8', '3', '文章3评论3', '1');
INSERT INTO `tp_comment` VALUES ('9', '2', '文章2评论3', '1');
INSERT INTO `tp_comment` VALUES ('19', '2', 'onethink', '1');
INSERT INTO `tp_comment` VALUES ('18', '2', 'thinkphp', '1');
~~~