简体   繁体   English

Laravel笔直的morphTo关系

[英]Laravel straight morphTo relationship

Very simple question: Laravel eloquent docs specify that the morphTo function defines a polymorphic, inverse one-to-one or many relationship. 一个非常简单的问题: Laravel雄辩的文档指定morphTo函数定义了多态,一对一或多个逆关系。 Is there anyway (laravel forks maybe ?) to create a polymorphic, non-inverse one-to-one or many relationship, Without doing it myself and diving in Laravel's Eloquent relationships code ? 无论如何(是否有Laravel叉子?)是否可以创建多态的,非反向的一对一或许多关系,而无需自己动手并深入Laravel的Eloquent关系代码中?

To be clear: I want to do something like: 要明确:我想做类似的事情:

class Answer extends Eloquent {
    protected $fillable = array('answer_id', 'answer_type');

    public function answers() {
        return $this->straightMorphTo('answer_id', 'answer_type', 'id');
    }
}

class AnswerFirst extends Eloquent {
    protected $fillable = array('id', 'text');

    public function answers() {
        return $this->belongsTo('Answer');
    }
}

class AnswerSecond extends Eloquent {
    protected $fillable = array('id', 'number');

    public function answers() {
        return $this->belongsTo('Answer');
    }
}

//
// Answer
// ------------------------------------
// answer_id     | answer_type        |
// ------------------------------------
// 1             | AnswerFirst        |
// 2             | AnswerSecond       |
// ------------------------------------
// 
// AnswerFirst
// ------------------------------------
// id            | text               |
// ------------------------------------
// 1             | 1rst part          |
// 1             | 2nd part           |
// ------------------------------------
// 
// AnswerSecond
// ------------------------------------
// id            | number             |
// ------------------------------------
// 2             | 1                  |
// 2             | 2                  |
// ------------------------------------
// 

And then, Answer::with('answers')->find(2) should return 然后,Answer :: with('answers')-> find(2)应该返回

{ 
    id:'2', 
    answer_type:'AnswerSecond',
    answers: [
        {id:'2',number:'1'},
        {id:'2',number:'2'},
    ] 
}

Actually I found a solution by digging into the Laravel Eloquent classes and coding it myself. 实际上,我通过深入研究Laravel Eloquent类并自己编写了一个解决方案。

Two ways of doing this, either create a new relationship or just 'patch' the MorphTo relationship to allow the instance that will be morphed to have multiple parents. 执行此操作的两种方法是创建新关系,或者只是“修补” MorphTo关系以允许将变形的实例具有多个父对象。

Either way, the solution comes just by replacing the MorphTo (or your relation copied from MorphTo) matchToMorphParents function by 无论哪种方式,解决方案都是通过替换MorphTo(或从MorphTo复制的关系)matchToMorphParents函数来实现的

protected function matchToMorphParents($type, Collection $results)
{
    $resultsMap = array();
    foreach ($results as $result)
    {
        if (isset($this->dictionary[$type][$result->getKey()]))
        {
            foreach ($this->dictionary[$type][$result->getKey()] as $model)
            {
                $resultsMap[$model->{$this->foreignKey}]['model'] = $model;
                if (!array_key_exists('results', $resultsMap[$model->{$this->foreignKey}])) {
                    $resultsMap[$model->{$this->foreignKey}]['results'] = array();
                }
                array_push($resultsMap[$model->{$this->foreignKey}]['results'], $result);
            }
        }
    }
    foreach ($resultsMap as $map)
    {
        if (sizeof($map['results']) === 1) {
            $map['model']->setRelation($this->relation, $map['results'][0]);
        } else {
            $map['model']->setRelation($this->relation, new Collection($map['results']));
        }
    }
}

You can find the diff here 你可以在这里找到差异

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

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