简体   繁体   English

在 CakePHP 的一个字段中具有多个 id 的表关联

[英]Table Associations with multiple id's in one field in CakePHP

I am using CakePHP and I have 2 tables, Documents and Download, on the Download table I have a field: Document_id, making the association with the model works fine, but sometimes i have in Document_id a field like this "2,10,12", How can I associat the tables?我正在使用 CakePHP,我有 2 个表,文档和下载,在下载表上我有一个字段:Document_id,使与 model 的关联工作正常,但有时我在 Document_id 中有一个这样的字段“2,10,12 ", 如何关联表?

You shouldn't store multiple ids in one field (unless you're intentionally denormalizing your database, but then you wouldn't be asking this question).您不应该在一个字段中存储多个 ID(除非您有意对数据库进行非规范化,但那样您就不会问这个问题)。

The "proper" way to do this is use a 3rd table called a linking table.执行此操作的“正确”方法是使用称为链接表的第三个表。 eg "documents_downloads", to store the download_ids with matching document_ids.例如“documents_downloads”,用于存储 download_ids 与匹配的 document_ids。

Download hasMany DocumentDownload下载 hasMany DocumentDownload
DocumentDownload belongsTo Download & Document DocumentDownload属于To Download & Document
Document hasMany DocumentDownload文档有很多文档下载

The simplest way to implement this is using Cake's "hasMany through (The Join Model)"最简单的实现方式是使用 Cake 的“hasMany through (The Join Model)”

See section of the Cakebook: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model请参阅 Cakebook 部分: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

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

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