Php 在Laravel 4中使用透视表时如何分配关系? 细节

Php 在Laravel 4中使用透视表时如何分配关系? 细节,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我有三张桌子: 目录下载 输出频率 导出频率目录下载(透视表) 图解 我不确定我是否正确设置了它们之间的关系。 如果我错了,请纠正我 这就是我所做的 在CatalogDownload.php中 public function export_frequencies(){ return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequen

我有三张桌子:

  • 目录下载
  • 输出频率
  • 导出频率目录下载(透视表)
图解

我不确定我是否正确设置了它们之间的关系。 如果我错了,请纠正我

这就是我所做的 在
CatalogDownload.php中

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

public function export_frequencies(){ 
        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
public function catalog_downloads(){ 
        return $this->belongsToMany('CatalogDownload','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
问题
  • 根据我的图表-我是否正确分配了关系? 我希望我没有混淆
    有很多
    以下的

  • 我需要一个类还是一个数据透视表的模型


谢谢

因为
导出频率
目录下载
模型中,您必须反转ID,因为belongToMany的参数如下:

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)
是什么导致了这一功能:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
另一个功能是正确的

如果数据透视表中有一些数据,例如名称为
someCounter
的列,则在创建数据透视对象时,必须告知要加载该列的关系,如下所示:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}
$catalogDownload->export_frequencies()->first()->pivot->someCounter;
这将加载列并使其可用,如下所示:

public function export_frequencies(){ 

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

你需要一个单独的PIVO模型,如果你需要为字段做一些特殊的处理,或者如果这个枢轴本身有一个关系,那么你可以考虑使用一个完整的模型而不是纯的枢轴模型。

< P>因为<代码> ExtPosifStudio在<代码>目录下载> /代码>模型中,你必须反转ID。因为belongtomany的参数如下:

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)
是什么导致了这一功能:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
另一个功能是正确的

如果数据透视表中有一些数据,例如名称为
someCounter
的列,则在创建数据透视对象时,必须告知要加载该列的关系,如下所示:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}
$catalogDownload->export_frequencies()->first()->pivot->someCounter;
这将加载列并使其可用,如下所示:

public function export_frequencies(){ 

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

你需要一个单独的PIVO模型,如果你需要为字段做一些特殊的处理,或者如果这个枢轴本身有一个关系,那么你可以考虑使用一个完整的模型而不是纯的枢轴模型。

< P>因为<代码> ExtPosifStudio在<代码>目录下载> /代码>模型中,你必须反转ID。因为belongtomany的参数如下:

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)
是什么导致了这一功能:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
另一个功能是正确的

如果数据透视表中有一些数据,例如名称为
someCounter
的列,则在创建数据透视对象时,必须告知要加载该列的关系,如下所示:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}
$catalogDownload->export_frequencies()->first()->pivot->someCounter;
这将加载列并使其可用,如下所示:

public function export_frequencies(){ 

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

你需要一个单独的PIVO模型,如果你需要为字段做一些特殊的处理,或者如果这个枢轴本身有一个关系,那么你可以考虑使用一个完整的模型而不是纯的枢轴模型。

< P>因为<代码> ExtPosifStudio在<代码>目录下载> /代码>模型中,你必须反转ID。因为belongtomany的参数如下:

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)
是什么导致了这一功能:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
    }
另一个功能是正确的

如果数据透视表中有一些数据,例如名称为
someCounter
的列,则在创建数据透视对象时,必须告知要加载该列的关系,如下所示:

public function export_frequencies(){ 

        return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}
$catalogDownload->export_frequencies()->first()->pivot->someCounter;
这将加载列并使其可用,如下所示:

public function export_frequencies(){ 

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

你需要一个单独的枢轴模型,如果你需要对这些字段做一些特殊的处理,或者如果这个枢轴本身有一个自己的关系,那么你可以考虑使用一个完整的模型来代替一个纯的枢轴模型。只要遵循特定约定,就可以设置多对多关系,而无需引用透视表和相关id

您可以使用对相关表的单数引用来命名数据透视表,如“目录\下载\导出\频率”注意单数引用的字母顺序

然后你可以简单地做:

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

// ExportFrequency Model
public function catalogDownloads()
{
    return $this->belongsToMany('CatalogDownload');
}
然后,这将允许您使用查询生成器或类似以下工具运行查询:

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


希望这有帮助

作为对已接受答案的补充说明,您可以设置多对多关系,而无需参考透视表和相关id,只要遵循特定约定即可

您可以使用对相关表的单数引用来命名数据透视表,如“目录\下载\导出\频率”注意单数引用的字母顺序

然后你可以简单地做:

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

// ExportFrequency Model
public function catalogDownloads()
{
    return $this->belongsToMany('CatalogDownload');
}
然后,这将允许您使用查询生成器或类似以下工具运行查询:

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


希望这有帮助

作为对已接受答案的补充说明,您可以设置多对多关系,而无需参考透视表和相关id,只要遵循特定约定即可

您可以使用对相关表的单数引用来命名数据透视表,如“目录\下载\导出\频率”注意单数引用的字母顺序

然后你可以简单地做:

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

// ExportFrequency Model
public function catalogDownloads()
{
    return $this->belongsToMany('CatalogDownload');
}
然后,这将允许您使用查询生成器或类似以下工具运行查询:

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


希望这有帮助

作为对已接受答案的补充说明,您可以设置多对多关系,而无需参考透视表和相关id,只要遵循特定约定即可

您可以使用对相关表的单数引用来命名数据透视表,如“目录\下载\导出\频率”注意单数引用的字母顺序

然后你可以简单地做:

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

// ExportFrequency Model
public function catalogDownloads()
{
    return $this->belongsToMany('CatalogDownload');
}
这将允许您使用查询生成器或E运行查询