2014-10-20 121 views
1

首先,我是新的Laravel,我已经阅读了一些教程和大量的文档,但现在我遇到了一个问题,我找不到解决方案。Laravel多重关系

我有三个表:post,post_adv_option,adv_option。

那么这些表之间的关系是:

  • 岗位post_adv_option:1对许多
  • post_adv_option到adv_option:壹壹到

这里是我的代码我正在使用:

型号

class Post extends Eloquent { 
    public function postAdvancedOptions() { 
     return $this->hasMany('PostAdvancedOptions','post_id'); 
    } 
} 

class PostAdvancedOption extends Eloquent { 
    public function AdvancedOption() { 
     return $this->hasOne('AdvancedOption','id','advanced_option_id'); 
    } 
} 

class AdvancedOption extends Eloquent { 

} 

Post Table 
------------------------------ 
| id | title | content | ... | 
------------------------------ 
| 1 | AAAAA | aaaaaaa | ... | 
------------------------------ 
| 2 | BBBBB | bbbbbbb | ... | 
------------------------------ 
| . | ..... | ....... | ... | 
------------------------------ 

PostAdvancedOption Table 
--------------------------------------------- 
| id | post_id | advanced_option_id | value | 
--------------------------------------------- 
| 1 | 1  | 2     | xxxxx | 
--------------------------------------------- 
| 2 | 2  | 1     | aaaaa | 
--------------------------------------------- 
| 3 | 2  | 4     | bbbbb | 
--------------------------------------------- 
| 4 | 2  | 5     | xxxxx | 
--------------------------------------------- 

AdvancedOption Table 
---------------------------------------- 
| id | name  | sort | description | 
---------------------------------------- 
| 1 | abc  | 0  | desc  | 
---------------------------------------- 
| 2 | def  | 2  | desc  | 
---------------------------------------- 
| 3 | ghi  | 1  | desc  | 
---------------------------------------- 
| . | ......... | .  | desc  | 
---------------------------------------- 
| 9 | mno  | 8  | desc  | 
---------------------------------------- 

控制器

$postArray = Post::with('postAdvancedOptions')->where('id', '=', $id)->get()->toArray(); 

结果 我的电流输出看起来像这样

array(3) [ 
array(15) [ 
    'id' => integer 64 
    'title' => string (19) "test" 
    'content' => string (6) "lorem ipsum"   
    'post_advanced_options' => array(3) [ 
     array(4) [ 
      'id' => integer 34 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "xxx" 
     ] 
     array(4) [ 
      'id' => integer 35 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "yyy" 
     ] 
     array(4) [ 
      'id' => integer 36 
      'post_id' => integer 64 
      'advanced_option_id' => integer 6 
      'option' => string (3) "vvv" 
     ] 
    ] 
] 

但我需要的是:由AdvancedOption.sort

array(3) [ 
array(15) [ 
    'id' => integer 64 
    'title' => string (19) "test" 
    'content' => string (6) "lorem ipsum"   
    'post_advanced_options' => array(3) [ 
     array(4) [ 
      'id' => integer 34 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "xxx" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 0 

     ] 
     array(4) [ 
      'id' => integer 35 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "yyy" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 1 

     ] 
     array(4) [ 
      'id' => integer 36 
      'post_id' => integer 64 
      'advanced_option_id' => integer 6 
      'option' => string (3) "vvv" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 1 
     ] 
    ] 
] 

我如何可以调用的关系从邮政PostAdvancedOptionsAdvancedOption

..并责令post_advanced_options

回答

3

目前你有你的桌子es建立了多对多关系,但您的模型没有正确设置以建立多对多关系。您需要像这样:

class Post extends Eloquent { 
    public function options() { 
     return $this->belongsToMany('AdvancedOption'); 
    } 
} 

class AdvancedOption extends Eloquent { 
    public function posts() { 
     return $this->belongsToMany('Post'); 
    } 
} 

你不需要为PostAdvancedOptions一个模型,因为那是你的数据透视表,而不是一个模型,但只是连接关系。每当你告诉Laravel一个型号belongsToMany它会自动查找某个数据透视表。该数据透视表将为advanced_option_post,并且将具有id,advanced_option_idpost_id的列。如果你需要,你也可以重写这个命名。

您可以阅读更多关于多对多关系here

+1

而且你必须重视那些互相使用附加方法,例如:$ post-> AdvancedOption() - > attach(); – Burimi 2014-10-20 18:57:12

+0

@罗斯埃德曼,谢谢你的帮助解释。我的解决方案现在是一个带有函数的数据透视表:'返回$ this-> belongsToMany('AdvancedOption') - > orderBy(“sort”,“desc”) - > withPivot(array('value'));' – Patrick 2014-10-21 09:31:17

0

我也相对新Laravel,但如果你只需要AdvancedOptions信息,你可以在帖子模型中使用了“Has Many Through”:

class Post extends Eloquent { 
    public function AdvancedOptions() { 
     return $this->hasManyThrough('PostAdvancedOption', 'AdvancedOption'); 
    } 
} 

或者,如果你要实现的数据透视表如Ross Edman所述,那么你应该将PostAdvancedOption表中的“值”移动到AdvancedOption

0

如果关系是一对多关系,那么可以将外键保留在子表中(在这种情况下,AdvanceOptions保存邮政主键)。

既然你定义这样的关系,我建议修复数据库架构,则AdvanceOption保存这样父邮政主键:

Post Table 
------------------------------ 
| id | title | content | ... | 
------------------------------ 
| 1 | AAAAA | aaaaaaa | ... | 
------------------------------ 



AdvancedOption Table 
------------------------------------------------------------------- 
| id | name  | post_id | value | another advanceoption column | 
------------------------------------------------------------------- 
| 1 | abc  | 0  |  |        | 
------------------------------------------------------------------- 

,然后将模型看起来是这样的:

class Post extends Eloquent { 
    public function advanceoption() { 
     return $this->hasMany('App\AdvanceOption','post_id'); 
    } 
} 

class AdvancedOption extends Eloquent { 
    public function post() { 
     return $this->belongsTo('App\Post','post_id'); 
    } 
} 

终于,找到你想要的结果:

$postArray = Post::find($id)->advanceoption()->get();