Laravel-多对多表,其中多对多表是(部分)多态 [英] Laravel - many-to-many where the many-to-many table is (part-) polymorph

查看:326
本文介绍了Laravel-多对多表,其中多对多表是(部分)多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张叫奖金的桌子.用户可以为某些操作获得奖励(就像奖励一样).好了,可以将奖金分配给许多用户,并且许多用户可以获得相同的奖金.因此,用户与奖金之间存在多对多的关系.

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.

这里的问题是我需要将多态类型保存在奖励表中,并将ID保存在多对多表中.

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.

我认为这应该是最好的方法,但是如何使用laravel来实现呢?我认为这不是正常的用例.但是,我仍然想将其用作laravel中的其他关系,以便我可以通过正确的多态关系来获取用户并获得他的红利.

I think this should be the best way but how would I realize it with 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.

你有什么主意吗?

推荐答案

您可能必须开发自己的关系类. 例如:

You are probably going to have to develop your own relationship classes. Ex:

模型

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

关系

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;
    }

在这种情况下,我们使用Lime Survey为每个调查创建一个唯一表(请注意$ instance-> setSid()更改表名),并为每个答案->问题值创建一个唯一列. (注意$ parent-> sid.'X'.$ parent-> gid.'X'.$ parent-> qid.'AS value')

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. ( note $parent->sid.'X'.$parent->gid.'X'.$parent->qid. 'AS value')

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

Where sid = survey_id, gid = group_id(I think) and qid = question_id

这很烦人.

请注意我如何引用父级的值来进一步开发查询. 您应该能够遵循类似的方法来实现自己的内心需求,并且仍然保持使用Eloquent的可行性.

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.

这篇关于Laravel-多对多表,其中多对多表是(部分)多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆