繁体   English   中英

Laravel笔直的morphTo关系

[英]Laravel straight morphTo relationship

一个非常简单的问题: Laravel雄辩的文档指定morphTo函数定义了多态,一对一或多个逆关系。 无论如何(是否有Laravel叉子?)是否可以创建多态的,非反向的一对一或许多关系,而无需自己动手并深入Laravel的Eloquent关系代码中?

要明确:我想做类似的事情:

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                  |
// ------------------------------------
// 

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

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

实际上,我通过深入研究Laravel Eloquent类并自己编写了一个解决方案。

执行此操作的两种方法是创建新关系,或者只是“修补” MorphTo关系以允许将变形的实例具有多个父对象。

无论哪种方式,解决方案都是通过替换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']));
        }
    }
}

你可以在这里找到差异

暂无
暂无

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

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