类的属性不存在:app\index\model\Topic->id
问题一:
有点懵了。这个是哪里错了?
问题二::关于tags标签的编辑以后的保存,请老师写一个具体的代码。
最后附上我的源代码:
一:Topic.php:
<?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;
class Topic extends \think\Controller
{
public function newTopic()
{
if(request()->isPost())
{
$postData = input('post.');
$topic = new TopicModel();
$user = session('user');
$topic->title = $postData['title'];
$topic->content = $postData['body'];
$topic->category_id = $postData['category_id'];
$topic->created_at = intval(microtime(true));
$topic->user_id = $user->id;
$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)
{
$topic_tag = new TopicTagModel();
$topic_tag->topic_id = $topicId;
$topic_tag->tag_id = $tagId;
$topic_tag->save();
}
private function createTag($tagname)
{
$tag = new TagModel();
$tag->name = $tagname;
$tag->save();
return $tag;
}
public function detail()
{
$topicId = input('get.id');
$topic = TopicModel::getTopic($topicId);
$user = session('user');
$categoryNames = getcategoryNames($topic->category_id);
$tags = TopicTagModel::getTagsFromTagsByTopicId($topicId);
$this->assign([
'user' => $user,
'topic' => $topic,
'category' => config('category'),
'categoryNames' => $categoryNames,
'tags' => $tags,
]);
echo $this->fetch('detail');
}
public function edit_Topic()
{
if(request()->isPost())
{
$postData = input('post.');
$topic = new TopicModel();
$topicId = $postData['topicId'];
var_dump($topicId);
$user = session('user');
$list=[
'title' => $postData['title'],
'content' => $postData['body'],
'category_id' => $postData['category_id'],
'created_at' => intval(microtime(true)),
'user_id' => $user->id,
];
$Topic = new TopicModel();
$Topic->save($list,['id' => $topicId]);
// 处理标签
$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('恭喜,帖子编辑成功。');
}else{
// 显示原来的默认信息
$topicId = input('get.topicId');
$user = session('user');
$topic = TopicModel::getTopic($topicId);
$categoryNames = getcategoryNames($topic->category_id);
$tagsValue = TopicTagModel::getTagsFromTagsByTopicId($topicId);
$tags = TagModel::all();
$this->assign([
'topicId' => $topicId,
'user' => $user,
'topic' => $topic,
'category' => config('category'),
'categoryNames' => $categoryNames,
'tagsValue' => $tagsValue,
'tags' => $tags,
]);
echo $this->fetch('edit_Topic');
}
}
}
二:edit_topic.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>面向大海的论坛</title>
<link rel="stylesheet" href="__CSS__/all-df86af5803.css">
<script src="__JS__/all-17476e6cc3.js"></script>
<script src="__JS__/app.js"></script>
</head>
<body class="forum" data-page="forum">
<?php require(APP_PATH.'index/view/common/header.html'); ?>
<div id="main" class="main-container container">
<div class="row max-width">
<div class="col-xs-12">
<ul class="breadcrumb">
<li>
<a href="index.html">首页</a>
</li>
<li>
<strong>编辑帖子</strong>
</li>
</ul>
</div>
</div>
<div class="thread_create">
<div class="col-md-9 main-col">
<div class="panel panel-default corner-radius">
<div class="panel-heading">编辑帖子</div>
<div class="panel-body">
<div class="reply-box form box-block">
<form method="POST" action="<?=url('topic/edit_topic');?>" accept-charset="UTF-8" id="thread_create_form" class="create_form">
<input name="_token" type="hidden" value="BAnCy9yzQxp30BiIkeU58OMjR5uWeDM9dCSMNwKH">
<div class="form-group">
<input class="form-control" id="thread_title" value="<?=$topic->title;?>" placeholder="标题" name="title" type="text">
</div>
<div class="form-group">
<select class="form-control selectpicker" id='category_id' name="category_id" >
<option value="<?php echo $categoryNames[2];?>" selected> - <?php echo $categoryNames[1];?></option>
<?php $category = isset($category) ? $category : []; ?>
<?php foreach($category as $c => $subCategory ): ?>
<optgroup label="<?=$c ; ?>">
<?php foreach($subCategory as $subc => $name ): ?>
<option value=<?=$subc;?>> - <?=$name ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
</div>
<!-- editor start -->
<div class="editor-toolbar">
<div class="opts pull-right">
<span class="dropdown dropdown-small" id="editor-toolbar-insert-code">
<a href="#editor-toolbar-insert-code" data-toggle="dropdown" title="插入代码"><i class="fa fa-code"></i></a>
<ul class="dropdown-menu insert-codes" role="menu">
<li><a data-lang="php" href="#">PHP</a></li>
<li><a data-lang="html" href="#">HTML</a></li>
<li><a data-lang="scss" href="#">CSS / SCSS</a></li>
<li><a data-lang="js" href="#">JavaScript</a></li>
<li><a data-lang="yml" href="#">YAML <i>(.yml)</i></a></li>
<li><a data-lang="coffee" href="#">CoffeeScript</a></li>
<li><a data-lang="conf" href="#">Nginx / Redis <i>(.conf)</i></a></li>
<li><a data-lang="python" href="#">Python</a></li>
<li><a data-lang="java" href="#">Java</a></li>
<li><a data-lang="ruby" href="#">Ruby</a></li>
<li><a data-lang="erlang" href="#">Erlang</a></li>
<li><a data-lang="shell" href="#">Shell / Bash</a></li>
</ul>
</span>
<a class="btn-upload" href="javascript:void(0);" data-toggle="tooltip" data-placement="bottom" title="上传图片"><i class="fa fa-image"></i> </a>
<input type="file" name="file" class="input-file" style="display: none;" />
</div>
<ul class="nav nav-pills" style="clear:none;">
<li class="edit active"><a href="#">编辑</a></li>
<li class="preview"><a href="#">预览</a></li>
</ul>
</div> <!-- end -->
<div class="form-group">
<textarea class="post-editor form-control" rows="15" style="overflow:hidden" id="body_field" placeholder="请使用Markdown撰写内容" name="body" cols="50"><?=$topic->content;?></textarea>
</div>
<div class="form-group">
<!--显示默认的标签和选项-->
<select class="form-control js-tag-tokenizer" id='tags' multiple="multiple" name="tags[]">
<!--原来所有的供选择的标签-->
<?php $tags = isset($tags) ? $tags : []; ?>
<?php foreach($tags as $tags):?>
<option value="<?=$tags->id;?>"><?=$tags->name;?></option>
<?php endforeach;?>
<!--原来所有的供选择的标签结束-->
<!--显示这个帖子的默认标签-->
<?php foreach($tagsValue as $tagsValue): ?>
<option value="<?=$tagsValue->id;?>" selected><?=$tagsValue->tag->name;?></option>
<?php endforeach; ?>
<!--显示这个帖子的默认标签结束-->
</select>
<input type="hidden" name="topicId" value="<?=$topicId;?>">
<!--显示默认的标签和选项结束-->
</div>
<div class="form-group status-post-submit">
<input class="btn btn-primary col-xs-2" id="thread-create-submit" type="submit" value="编辑完成,提交">
<div class="pull-right">
<small>支持在编辑框拖拽、复制粘贴或<a class="btn-upload" href="javascript:void(0);">浏览本地文件</a>进行图片上传.</small>
<a href="/markdown" target="_blank"><i class="fa fa-lightbulb-o"></i> 排版说明</a>
</small>
</div>
</div>
<div class="box preview markdown-body" id="preview-box" style="display:none;"></div>
</form>
</div>
</div>
</div>
</div>
<?php require(APP_PATH.'index/view/common/new_topic_sidebar.html'); ?>
</div>
</div>
<?php require(APP_PATH.'index/view/common/footer.html'); ?>
</body>
</html>
正在回答 回答被采纳积分+1
你好,1、首先添加编辑按钮,并通过href连接传递编辑帖子的id,
<a href="<?=url('topic/editPost');?>?topicId=<?=$topic->id;?>">编辑</a>
2、 根据传递的id获取帖子的详细信息显示在编辑页面
public function editPost() { $topicId = input('get.topicId'); $topicTags = TopicTagModel::where(['topic_id' => $topicId])->select(); $selectTags = []; foreach($topicTags as $topicTag) { $selectTags[] = $topicTag->tag_id; } $this->assign([ 'user' => session('user'), 'topic' => TopicModel::get($topicId), 'category' => config('category'), 'tags' => TagModel::all(), 'selectTags' => $selectTags, ]); echo $this->fetch('edit_topic'); }
3、然后根据帖子id以及编辑后帖子的详细信息跟新数据库信息
action="<?=url('topic/newTopic');?>?topicId=<?=$topic->id;?>"
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');
}
如果解决您的问题请采纳,祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星