简体   繁体   English

2个表中的外键(CakePHP)

[英]Foreign Key in 2 tables (CakePHP)

I have 3 tables: staffs, users and threads. 我有3个表:人员,用户和线程。

Both the staffs and users can create threads and a thread belongs to a user. 员工和用户都可以创建线程,并且线程属于用户。

In thread, there is a user_id (FK - BelongsTo). 在线程中,有一个user_id(FK-BelongsTo)。 The question is, How can I identify who created the thread. 问题是,我该如何识别谁创建了线程。 If I create a field called creator_id I still don't know If it's a staff or a user who created the thread. 如果我创建一个名为creator_id的字段,我仍然不知道是创建该线程的人员还是用户。

I also tried a different approach by creating a field called creator (enum('staff', 'user)). 我还尝试了一种不同的方法,即创建一个称为创建者的字段(enum('staff','user))。 The limitation with this method is, when I am using Thread->find(), I couldn't figure out a way to retrieve the information of the creator. 这种方法的局限性是,当我使用Thread-> find()时,我想不出一种方法来检索创建者的信息。

What options do I have to achieve what I want? 为了达到我想要的目标,我必须采取哪些选择?

Do the Model Association as the following in Model for Thread. 请按照“线程模型”中的以下步骤进行模型关联。 Create a field called creator_id and store the id of the user/staff who created the thread, 创建一个名为creator_id的字段,并存储创建线程的用户/工作人员的ID,

public $belongsTo = array(
    'Client' => array(
        'className' => 'Staff',
        'foreignKey' => 'creator_id',
        'conditions' => array('Thread.creator' => 'staff'),
        'fields' => '',
        'order' => ''
    ),
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'creator_id',
        'conditions' => array('Thread.creator' => 'user'),
        'fields' => '',
        'order' => ''
    )
);

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

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