【问题标题】:How to give 3 relations to same table in Cakephp如何在 Cakephp 中为同一张表提供 3 个关系
【发布时间】:2011-04-16 04:13:20
【问题描述】:
您好,我是 cake php 新手,无法解决问题。问题是我有一张像这样的桌子;
id varchar(16)
parent_id varchar(16)
文字文字
user_id bigint(20)
is_deleted_by_user bit(1)
is_deleted_by_us 位(1)
who_deleted bigint(20)
who_answered bigint(20)
modified_at 日期时间
created_at 日期时间
在这个表中,我想给出 users 表和 user_id、who_deleted、who_answered 之间的关系。我的意思是 user_id、who_deleted 和 who_answered 是一个用户 ID。我怎样才能给出用户表和这个表之间的关系?
【问题讨论】:
标签:
database
cakephp
foreign-keys
models
database-relations
【解决方案1】:
为同一个模型创建多个关系相对容易。有一个section of the documentation 专用于此。以下是我为具有多个与 Binary 模型关联的字段的 Resource 模型所做的:
class Resource extends AppModel {
public $belongsTo = array (
'PDF' => array (
'className' => 'Binary',
'foreignKey' => 'pdf_file_id'
),
'MSWord' => array (
'className' => 'Binary',
'foreignKey' => 'msword_file_id'
)
);
... other class code ...
}
resources 表包含 pdf_file_id 和 msword_file_id 字段,每个字段都引用了 Binary 记录。
希望对您有所帮助。