简体   繁体   English

在Laravel 4中使用数据透视表时如何分配关系?

[英]How to assign relation when working with pivot table in Laravel 4?

Details 细节

I have 3 tables : 我有3张桌子:

  • catalog_downloads catalog_downloads
  • export_frequencies 出口频率
  • export_frequencies_catalog_downloads (Pivot Table) export_frequencies_catalog_downloads(数据透视表)

Diagram 图表

在此处输入图片说明

I am not sure if I set the relation between them correctly. 我不确定是否正确设置它们之间的关系。 Please correct me if I am wrong. 如果我错了,请纠正我。

Here is what I did 这是我所做的

In CatalogDownload.php CatalogDownload.php

public function export_frequencies(){ 
        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }

In ExportFrequency.php ExportFrequency.php

public function catalog_downloads(){ 
        return $this->belongsToMany('CatalogDownload','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }

Questions 问题

  • According to my diagram - Did I assign the relationship correctly ? 根据我的图表-我是否正确分配了关系? I hope I didn't mix up between hasMany and belongsTo 我希望我不要在hasManybelongsTo之间belongsTo

  • Will I need a class or a model for a Pivot Table ? 我是否需要数据透视表的类或模型?

Thanks 谢谢

Since export_frequencies is in the CatalogDownload model you have to invert the ID's because the parameters of belongsToMany are as follows: 由于export_frequenciesCatalogDownload模型中,因此您必须将ID反转,因为belongsToMany的参数如下:

1. Name of the referenced (target) Model (ExportFrequency)
2. Name of the Pivot table
3. Name of the id colum of the referencing (local) Model (CatalogDownload in this case)
4. Name of the id colum of the referenced (target) Model (ExportFrequency in this case)

what leads to this function: 导致此功能的原因:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }

The other function was correct. 另一个功能是正确的。

If you had some data in your pivot table, for instance a colum with the name someCounter then you will have to tell the relation to load that column when creating the pivot object like this: 如果您的数据透视表中有一些数据,例如名称为someCounter列,那么在创建数据透视对象时,您将必须告诉关系以加载该列,如下所示:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}

That will load the column and make it avalible like this: 这将加载该列并使其可用,如下所示:

$catalogDownload->export_frequencies()->first()->pivot->someCounter;

You will need a separate Pivot Model if you need to do some special handling for the fields or if that pivot itself has a relation of its own but then you might consider using a full blown model instead of a pure Pivot Model. 如果需要对字段进行一些特殊处理,或者该数据透视点本身具有自己的关系,则需要单独的数据透视模型,但是您可能会考虑使用完整模型而不是纯数据透视模型。

As an added note to the accepted answer, you are able to set up your many to many relationships without referencing the pivot table and the relevant id's as long as you follow a specific convention. 作为已接受答案的补充说明,只要遵循特定约定,就可以建立多对多关系而无需引用数据透视表和相关ID。

You can name your pivot table using singular references to the related tables, like ' catalog_download_export_frequency '. 您可以使用对相关表的单引用来命名数据透视表,例如“ catalog_download_export_frequency ”。 Notice the alphabetic order of the singular references . 注意单数引用的字母顺序

Then you can simply do: 然后,您可以简单地执行以下操作:

// CatalogDownload Model
public function exportFrequencies()
{
    return $this->belongsToMany('ExportFrequency');
}

// ExportFrequency Model
public function catalogDownloads()
{
    return $this->belongsToMany('CatalogDownload');
}

This will then allow you to run queries using the query builder or Eloquent like: 然后,这将允许您使用查询生成器或Eloquent来运行查询,例如:

$catalogDownload->exportFrequencies()->get(); // Get all export frequencies for a specific CatalogDownload.

Or 要么

$this->catalogDownload->with('exportFrequencies')->find($id); // Using eager loading and dependency injection, when CatalogDownload is assigned to $this->catalogDownload

Hope this helps! 希望这可以帮助!

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

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