#### :-: **控制器**
~~~
<?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');
~~~