这个问题能给出正确的解答吗,问题区好几个人问,都没给出解决的办法
<?php
namespace app\index\controller;
use app\index\model\Tag as TagModel;
use app\index\model\Topic as TopicModel;
use app\index\model\TopicTag as TopicTagModel;
use think\Controller;
class Topic extends Controller {
public function newTopic() {
if (request()->isPost()) {
$postData = input('post.');
$topic = new TopicModel();
$user = session('user');
$topic->title = $postData['title'];
$topic->category_id = $postData['category_id'];
$topic->content = $postData['content'];
$topic->user_id = $user->id;
$topic->created_at = intval(microtime(true));
$topic->save();
//处理标签
$tags = $postData['tags'];
foreach ($tags as $tag) {
if (is_numeric($tag)) {
$this->createTopicTag($tag, $topic->id);
continue;
}
$newTag = $this->createTag($tag);
$this->createTopicTag($newTag->id, $topic->id);
}
$this->success('恭喜!帖子创建成功!');
}
$this->assign([
'user' => session('user'),
'category' => config('category'),
'tags' => TagModel::all()
]);
echo $this->fetch('new_topic');
}
private function createTopicTag($tagId, $topicId) {
$topicTag = new TopicTagModel();
$topicTag->tag_id = $tagId;
$topicTag->topic_id = $topicId;
$topicTag->save();
}
private function createTag($name) {
$tag = new TagModel();
$tag->name = $name;
$tag->save();
return $tag;
}
public function detail() {
$topicId = input('get.id');
$topic = TopicModel::getTopic($topicId);
$user = session('user');
$this->assign([
'user' => $user,
'topic' => $topic,
'topicTags' => TopicTagModel::getTopicTagsByTopicId($topic->id),
'categoryNames' => getCategoryNames($topic->category_id),
]);
echo $this->fetch('detail');
}
}
common.php文件
<?php
// 应用公共文件
function getCategoryNames($id) {
$category =[
'imooc' => [
1 => '站务与公告',
2 => '反馈',
3 => '使用指南',
],
'Mobile' => [
8 => 'Android',
],
'Lifestyle' => [
10 => '意欲蔓延',
],
'Technology' => [
5 => '程序猿',
6 => '分享与创造',
],
];
//$category = config('category');
foreach($category as $key => $cate) {
foreach($cate as $category_id => $categoryName) {
if ($category_id == $id) {
return [$key, $categoryName];
}
}
}
return [];
}
model文件夹下的Topic.php
<?php
namespace app\index\model;
use think\Model;
class Topic extends Model{
public static function getTopic($id) {
return self::find(['id' => $id]);
}
public function user() {
return $this->belongsTo('User', 'user_id');
}
/*public static function getTopic($id) {
return self::withCount(['replies', 'praises'])-find($id);
}*/
}
正在回答 回答被采纳积分+1
你好,建议参考一下newTopic()和 model下getTopic方法尝试:
public function newTopic(){
if (request()->isPost()) {
$postData = input('post.');
$user = session('user');
$topicId = input('get.topicId');
if (!$topicId) {
$topic = new TopicModel();
} else {
$topic = TopicModel::find($topicId);
}
$topic->title = $postData['title'];
$topic->category_id = $postData['category_id'];
$topic->content = $postData['content'];
$topic->user_id = $user->id;
$topic->created_at = intval(microtime(true));
$topic->save();
// 标签处理
$tags = $postData['tags'];
TopicTagModel::where(['topic_id' => $topic->id])->delete();
foreach($tags as $tag) {
if (is_numeric($tag)) {
$this->createTopicTag($tag, $topic->id);
continue;
}
$newTag = $this->createTag($tag);
$this->createTopicTag($newTag->id, $topic->id);
}
$message = $topicId ? '编辑帖子成功!' : '发表帖子成功!';
return $this->success($message, 'topic/index');
}
$tags = TagModel::all();
$this->assign([
'user' => session('user'),
'category' => config('category'),
'tags' => $tags
]);
echo $this->fetch('new_topic');
}public static function getTopic($id) {
return self::withCount(['replies', 'praises'])->find($id);
}如果解决您的问题请采纳,祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星