laravel 雄辩的关系 - 附加(但不保存)到有很多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23951222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent relations - attach (but don't save) to Has Many
提问by Joe
I have the following relations set up:
我建立了以下关系:
class Page {
public function comments() {
return $this->hasMany('Comment');
}
}
class Comment {
public function page() {
return $this->belongsTo('Page');
}
}
Pretty bog standard. One page can have many comments, and one comment belongs to a single page.
漂亮的沼泽标准。一个页面可以有多个评论,一个评论属于一个页面。
I'd like to be able to create a new page:
我希望能够创建一个新页面:
$page = new Page;
and a comment
和评论
$comment = new Comment;
and attach the comment to the page, without saving any of it
并将评论附加到页面,不保存任何内容
$page->comments->associate($comment);
I've tried the following:
我尝试了以下方法:
// These are for one-to-many from the MANY side (eg. $comment->page->associate...)
$page->comments->associate($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::associate()
$page->comments()->associate($comment); // Call to undefined method Illuminate\Database\Query\Builder::associate()
// These 2 are for many-to-many relations, so don't work
$page->comments->attach($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::attach()
$page->comments()->attach($comment); // Call to undefined method Illuminate\Database\Query\Builder::attach()
// These 2 will (if successful) save to the DB, which I don't want
$page->comments->save($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::save()
$page->comments()->save($comment); // Integrity constraint violation: 1048 Column 'page_id' cannot be null
The really odd thing is that doing the opposite (attaching the page to the comment) works correctly:
真正奇怪的是,做相反的事情(将页面附加到评论)可以正常工作:
$comment->page()->associate($page);
The relevant docs are herebut they don't make any mention of attaching to the ONE side of a one-to-many. Is it even possible? (I feel like it should be)
相关文档在这里,但他们没有提到附加到一对多的一侧。甚至有可能吗?(我觉得应该是)
回答by Benubird
It sounds like you just want to add the new comment object to the page's comments collection - you can do that easily, using the basic colection add method:
听起来您只想将新的评论对象添加到页面的评论集合中 - 您可以使用基本的集合添加方法轻松做到这一点:
$page = new Page;
$comment = new Comment;
$page->comments->add($comment);
回答by Jarek Tkaczyk
You can't do since there are no ids to link.
您不能这样做,因为没有要链接的 ID。
So first you need to save the parent ($page
) then save the child model:
所以首先你需要保存父$page
模型( ) 然后保存子模型:
// $page is existing model, $comment don't need to be
$page->comments()->save($comment); // saves the comment
or the other way around, this time without saving:
或者反过来,这次没有保存:
// again $page exists, $comment don't need to
$comment->page()->associate($page); // doesn't save the comment yet
$comment->save();
回答by Julius Blatt
according to Benubird I just wanted to add something as I stumbled over this today:
根据 Benubird 的说法,我只是想添加一些东西,因为我今天偶然发现了这个:
You can call the add method on a collection like Benubird stated. To consider the concerns of edpaaz (additional fired query) I did this:
您可以在像 Benubird 所述的集合上调用 add 方法。为了考虑 edpaaz(附加触发查询)的担忧,我这样做了:
$collection = $page->comments()->getEager(); // Will return the eager collection
$collection->add($comment) // Add comment to collection
As far as I can see this will prevent the additional query as we only use the relation-object.
据我所知,这将阻止额外的查询,因为我们只使用关系对象。
In my case, one of the entities were persistent while the first (in your case page) was not (and to be created). As I had to process a few things and wanted to handle this in a object manner, I wanted to add a persistent entity object to a non persistent. Should work with both non persistent, too though.
在我的情况下,其中一个实体是持久的,而第一个(在您的案例页面中)不是(并且将被创建)。由于我必须处理一些事情并希望以对象方式处理它,因此我想将一个持久化实体对象添加到一个非持久化对象中。也应该与非持久性一起使用。
Thank you Benubird for pointing me to the right direction. Hope my addition helps someone as it did for me.
感谢 Benubird 为我指明了正确的方向。希望我的添加对某人有帮助,就像对我一样。
Please have in mind that this is my first stackoverflow post, so please leave your feedback with a bit concern.
请记住,这是我的第一篇 stackoverflow 帖子,所以请留下您的反馈。