【问题标题】:Laravel straight morphTo relationshipLaravel 直接 morphTo 关系
【发布时间】:2014-12-01 07:07:20
【问题描述】:

非常简单的问题:Laravel eloquent docs 指定 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


    【解决方案1】:

    实际上,我通过深入研究 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']));
            }
        }
    }
    

    你可以找到差异here

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2017-08-05
      • 2021-07-06
      • 1970-01-01
      相关资源
      最近更新 更多