在不指定id的情况下将hasOne模型附加到另一个Laravel

在不指定id的情况下将hasOne模型附加到另一个Laravel

本文介绍了在不指定id的情况下将hasOne模型附加到另一个Laravel/Eloquent模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我们有以下两个表,其中type_id引用了 questionType 中的一行:

Given we have the following two tables where type_id references a row in questionType:

问题

id | type_id | description
---+---------+------------
1  | 1       | A nice question
.. | ..      | ..

questionType

id | name
---+----------------
1  | Multiple-choice
.. | ..

具有以下口才模型:

class Question extends Model {
    public function type() {
        return $this->hasOne( 'QuestionType', 'id', 'type_id' );
    }
}

class QuestionType extends Model {
}

问题1

如何添加一个引用现有问题类型而没有的新问题,以手动方式对ID进行任何操作?例如,以下工作有效,但imo丑陋,因为我必须手动分配相应的问题类型id:

Question 1

How can I add a new question that references an existing question type without manually doing anything with ids? For example the following works but is ugly imo since I have to manually assign the corresponding question type id:

$q = new Question;
$q->type_id = 1; // Multiple-choice
$q->description = 'This is a multiple-choice question';
$q->save();

有人会认为有一种方法可以让ORM处理id分配(不是要在ORM中避免类似的事情吗?),类似(这在Eloquent中是行不通的) ORM ):

One would think there was a way to let the ORM handle the id-assignment (isn't the point to avoid stuff like this with ORMs?), something along the lines of (this does not work in Eloquent ORM):

$q = new Question;
$q->type = QuestionType.where('name', '=', 'Multiple-choice');
$q->description = 'This is a multiple-choice question';
$q->save();

问题2

关于问题1,我将如何添加一个引用 new 问题类型的新问题,而无需手动对id做任何事情?类似地,我想像的东西如下:

Question 2

In relation to question 1, how would I go about adding a new question that references a new question type without manually doing anything with ids? Similarly I imagine something along the lines of:

$t = new QuestionType;
$t->name = 'Another type';

$q = new Question;
$q->type = $t;
$q->description = 'This is a multiple-choice question';
$q->save();

在这里,我希望$q->save()保存新的问题类型和问题(或类似内容).

Here I'd like $q->save() to save both the new question type and question (or something similar).

以下方法有效,但我还是自己分配一个ID,我认为ORM应该处理该ID:

The following works, but again I'm assigning the id myself which I believe the ORM should handle:

$t = new QuestionType;
$t->name = 'Another type';
$t->save();

$q = new Question;
$q->type = $t->id;
$q->description = 'This is a multiple-choice question';
$q->save();

我尝试过使用save()update()方法的不同组合而没有运气.我还寻找了在hasMany关系上存在的attach(),但似乎在hasOne中丢失了.

I've tried playing with different combinations of save(), update() methods without luck. I also looked for attach() which exists on the hasMany relationships but seem to be missing in hasOne.

推荐答案

首先,您误解了所引用的关系.

First off, you misunderstood the relation you refer to.

这是您需要的:

// Question model
public function questionType()
{
  return $this->belongsTo('QuestionType', 'type_id');
}

// QuestionType model
public function questions()
{
  return $this->hasMany('Question', 'type_id');
}

然后您可以将它们链接在一起,如下所示:

then you can link them together like this:

$questionType = QuestionType::where(..)->first();

$question = new Question;
... // do something with it

// associate
$question->questionType()->associate($questionType);

// or the other way around - save new question and link to the type:
$questionType->questions()->save($question);

您也可以显式传递一个ID进行关联:

You can explicitly pass an id to associate as well:

$question->type_id = $someTypeId;
$question->save();


您不能这样做:


You can't do this:

$question->questionType = $someQuestionType;

通过这种方式,雄辩的人会处理模型属性,而不是关系.

for this way Eloquent handles model attributes, not relations.

问题2 :

$questionType = new QuestionType(['name' => 'multiple']);
$questionType->save();

$question = new Question([ ... some values ... ]);

// then either this way:
$questionType->questions()->save($question);

// or, again, the other way around:
$question->questionType()->associate($questionType);
$question->save();

这篇关于在不指定id的情况下将hasOne模型附加到另一个Laravel/Eloquent模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:11