我在Laravel的一个奇怪的问题上征求你的意见。我有一个如下所示的数据库模式:
我通过项目上的hasManyThrough关系从数据库中获取一组注释。这种关系看起来像这样:
public function comments()
{
return $this->hasManyThrough('App\Models\Comment', 'App\Models\Task');
}
这很好用。但是如果我复制一个评论并像这样保存它,那就失败了:
// fetch comments on project through a hasManyThrough relation
$comments = $project->comments()->get();
foreach($comments as $comment) {
// this works:
$comment->save();
// this does not work:
$replicatedComment = $comment->replicate();
$replicatedComment->save();
}
保存复制的评论失败,并显示以下错误:
未找到列:1054未知列' project_id'在'字段列表'
不知何故雄辩地向所有模型添加了一个额外的属性project_id
,这些属性在调用replicate()时没有得到正确的处理。
我可以通过在保存复制的注释之前取消设置project_id来轻松解决这个问题,但这不应该是正确的方法。任何人都可以重现这种行为,这是一个想要的行为还是我做错了什么?
感谢您的建议 克莱门
答案 0 :(得分:0)
试试这个,它会解决你的问题 -
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var appRoutes = require('./routes/app');
var listRoutes = require('./routes/lists');
var app = express();
mongoose.connect('mongodb://localhost:27017/moviemeter');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
next();
});
app.use('/lists', listRoutes);
app.use('/', appRoutes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.render('index');
});
module.exports = app;