简体   繁体   English

Laravel-多对多,其中多对多表是(部分)多态

[英]Laravel - many-to-many where the many-to-many table is (part-) polymorph

I have a table called bonus. 我有一张桌子叫做红利。 A user can get a bonus (it's like an reward) for certain actions. 用户可以为某些操作获得奖励(就像奖励)。 Well, the bonus can be assigned to many users and many users can get the same bonus. 好了,可以将奖金分配给许多用户,并且许多用户可以获得相同的奖金。 So it's a many to many relation between user and bonus. 因此,用户与奖金之间存在多对多的关系。

This is no problem so far. 到目前为止,这没有问题。 But users can get the same bonus for different actions. 但是用户可以针对不同的操作获得相同的奖励。 So let's say there is a bonus for voting on a picture. 因此,假设在图片上投票有好处。 Well, one user could vote on one picture and another one could vote on another picture which I'd like to save in the many-to-many table. 好吧,一个用户可以对一张图片进行投票,而另一个用户可以对另一张图片进行投票,我想将其保存在多对多表格中。

Furthermore there could be a bonus for writing a comment which is clearly another table than picture votes. 此外,写评论显然可能是图片投票之外的另一张桌子。

The problem here is that I would need to save the polymorphic type in the bonus table and the ID in the many-to-many table. 这里的问题是我需要将多态类型保存在奖励表中,将ID保存在多对多表中。

I think this should be the best way but how would I realize it with laravel? 我认为这应该是最好的方法,但是如何使用laravel来实现呢? I think this is not a normal use case. 我认为这不是正常的用例。 But still I'd like to use it as other relations in laravel so that I could fetch a user and get his bonuses with the correct polymorphic relation. 但是,我仍然想将其用作laravel中的其他关系,以便我可以通过正确的多态关系来获取用户并获得其奖励。

Do you have any ideas? 你有什么想法?

You are probably going to have to develop your own relationship classes. 您可能必须开发自己的关系类。 Ex: 例如:

MODEL 模型

public function answers()
{
    $instance = new Response();
    $instance->setSid($this->sid);
    return new QuestionAnswerRelation($instance->newQuery(),$this);
}

RELATIONSHIP 关系

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Pivotal\Survey\Models\Answer;
use Pivotal\Survey\Models\Collections\AnswerCollection;
use Pivotal\Survey\Models\QuestionInterface;
use Pivotal\Survey\Models\SurveyInterface;

class QuestionAnswerRelation extends Relation
{

    /**
     * Create a new relation instance.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  \Illuminate\Database\Eloquent\Model  $parent
     * @return void
     */
    public function __construct(Builder $query, QuestionInterface $parent)
    {
        $table = $query->getModel()->getTable();
        $this->query = $query
            ->select(array(
                \DB::raw($parent->sid.'X'.$parent->gid.'X'.$parent->qid . ' AS value'),
                'id'
            ));

        $this->query = $query;
        $this->parent = $parent;
        $this->related = $query->getModel();
        $this->addConstraints();
    }


    public function addEagerConstraints(array $models)
    {
        parent::addEagerConstraints($models);
    }

    public function initRelation(array $models, $relation)
    {

    }

    public function addConstraints()
    {

    }

    public function match(array $models, Collection $results, $relation)
    {

    }

    public function getResults()
    {
        $results = $this->query->get();
        $answerCollection = new AnswerCollection();

        foreach($results as $result)
        {
            $answer = new Answer($result->toArray());
            $answer->question = $this->parent;
            $answerCollection->add($answer);
        }

        return $answerCollection;
    }

In this case we are using Lime Survey which creates a unique table (note the $instance->setSid() changes the table name) for each of its surveys and a unique column for each of its answer -> question values. 在这种情况下,我们使用Lime Survey为每个调查创建一个唯一表(请注意$ instance-> setSid()会更改表名),并为每个答案->问题值创建一个唯一列。 ( note $parent->sid.'X'.$parent->gid.'X'.$parent->qid. 'AS value') (注意$ parent-> sid.'X'。$ parent-> gid.'X'。$ parent-> qid.'AS value')

Where sid = survey_id, gid = group_id(I think) and qid = question_id 其中sid = survey_id,gid = group_id(我认为)和qid = question_id

Its was quite irritating. 这很烦人。

Note how I reference values from the parent to further develop the query. 请注意我如何从父级引用值以进一步开发查询。 You should be able to follow a similar route to achieve whatever your heart desires and still maintain the feasibility to use Eloquent. 您应该能够遵循类似的方法来实现自己的内心需求,并且仍然保持使用Eloquent的可行性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM