【问题标题】:How to create self referential relationship in laravel?如何在 laravel 中创建自引用关系?
【发布时间】:2014-01-22 07:27:30
【问题描述】:

我是 Laravel 的新手。我只想创建一个自引用模型。例如,我想创建一个产品类别,其中 parent_id 字段与产品类别 ID 相同。这怎么可能?

型号如下所示

class Product_category extends Eloquent 
{
    protected $guarded = array();

    public static $rules = array(
        'name' => 'required',
        'parent_id' => 'required'
    );

     function product_category()
    {
        return $this->belongsto('Product_category','parent_id');
    }
}

结果达到了“100”的最大函数嵌套级别,正在中止!错误

【问题讨论】:

  • 为什么?为什么要检索与关系完全相同的实例?
  • 嗨 Cryode,我的目的是创建一个模型类别,其中其父类别是同一模型中的前一个类别之一。即类别模型具有不同的子类别。某些类别元素具有父类别,这是类别模型中的先前条目之一。
  • 为了让 Laravel 正常工作,你的类名应该是 ProductCategory,而不是 Product_category
  • @Cryode 它被称为一棵树。
  • 我有我的答案here,也许你可以看看这个例子。

标签: laravel-4


【解决方案1】:

您可以向模型添加关系并为关系字段设置自定义键。

更新:

试试这个结构

class Post extends Eloquent {

    public function parent()
    {
        return $this->belongsTo('Post', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('Post', 'parent_id');
    }
}

旧答案:

class Post extends Eloquent {

    function posts(){
        return $this->hasMany('Post', 'parent_id');
    }
}

【讨论】:

  • Hai JackKPoint,它导致错误“达到'100'的最大函数嵌套级别,正在中止!” .
  • 如何访问这些元素。是否可以通过 $Post->parent->name 实现?
  • @Praveen 查看我的访问父母的答案!
  • 我认为您应该能够执行类似public function children() { return $this->hasMany('Post', 'id', 'parent_id'); } 的操作,id 是父 id,您可以将其映射到引用模型的其他列作为 parent_id
【解决方案2】:

根据您的 cmets 尝试访问父级,我在代码中添加了更多内容!

class Person extends \Eloquent {
    protected $fillable = [];
    var $mom, $kids;

    function __construct() { 
        if($this->dependency_id<>0) {
            $this->mother->with('mother');  
        }
    }

    public function children() {
        $children = $this->hasMany('Person','dependency_id');
        foreach($children as $child) {
            $child->mom = $this;
        }
        return  $children;
    }

    public function mother() {
        $mother = $this->belongsTo('Person','dependency_id');
        if(isset($mother->kids)) {
            $mother->kids->merge($mother);
        }
        return $mother;
    }
}

然后您可以通过急切加载从子级访问父级,在此处查看更多信息:http://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/

【讨论】:

    【解决方案3】:

    您的模型不会因产生“达到 '100' 的最大函数嵌套级别”错误而出错。这是XDebug的配置;增加你的xdebug.max_nesting_level

    以下内容来自 laracasts.com 上的@sitesense 的a 2015 post

    这不是 Laravel、Symfony 或其他任何东西中的错误。仅在安装 XDebug 时发生。

    这仅仅是因为递归调用了 100 个或更多函数。这并不是一个很高的数字,因此更高版本的 XDebug (>= 2.3.0) 已将此限制提高到 256。请参见此处:

    http://bugs.xdebug.org/bug_view_page.php?bug_id=00001100

    编辑:事实上,最新的 Homestead 配置脚本已经将限制设置为 250。请参见此处的第 122 行:

    https://github.com/laravel/settler/blob/master/scripts/provision.sh#L122

    所以将xdebug.max_nesting_level = 250 添加到php.ini 应该可以做到这一点。

    【讨论】:

    • 这个实际上应该有助于解决 OPs 问题。我不明白有人会否决这个+1 .. :)
    【解决方案4】:

    你可以引用自己,使用 $this

    class Post extends Eloquent {
    
        function posts(){
            return $this->hasMany($this, 'parent_id');
        }
    }
    

    【讨论】:

      【解决方案5】:

      看看我的回答here。 关键是Model.php下面这段代码

      public function children()
          {
              return $this->hasMany(Structure::class, 'parent_id')->with('children');
          }
      

      【讨论】:

        猜你喜欢
        • 2016-12-07
        • 2020-04-06
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 2013-06-01
        • 1970-01-01
        相关资源
        最近更新 更多