简体   繁体   English

链接一个模型不止一次

[英]Link one model more than once

I have a function where a user can request a project. 我有一个功能,用户可以请求一个项目。 The request has 2 fields for other employees to be added. 该请求有2个字段供其他员工添加。

It's got a field for a person who is responsible for the project ( person_responsible ) and the employee who is supposed to attend the opening meeting ( person_attending ). 它有一个领域的人谁负责的项目( person_responsible )以及谁应该参加开幕会议(员工person_attending )。

What I want to know is, since both these fields ( person_responsible and person_attending ) will be pulling it's data from hr_employees table, how would I set this up in my Project -Model. 我想知道的是,由于这两个字段( person_responsibleperson_attending )都将从hr_employees表中提取数据,因此我将如何在Project hr_employees中进行设置。

At the moment I have the one field set up like this: 目前,我已经像这样设置了一个字段:

public $belongsTo = array(
    'HrEmployee' => array(
        'className' => 'HrEmployee',
        'foreignKey' => 'responsible_person',
        'fields' => 'HrEmployee.employeename',
    )
);

How would I set up the other field? 我将如何设置其他领域?

What I do in this cases is to make two associations. 在这种情况下,我要做的是建立两个关联。 Since cake allow to customize relations, you can have two relations to the same model with different names. 由于cake允许自定义关系,因此您可以对具有不同名称的同一模型具有两个关系。

public $belongsTo = array(
    'ResponsibleEmployee' => array(
        'className' => 'HrEmployee',
        'foreignKey' => 'responsible_person',
        'fields' => 'HrEmployee.employeename',
    ),
    'AttendingEmployee' => array(
        'className' => 'HrEmployee',
        'foreignKey' => 'person_attending',
        'fields' => 'HrEmployee.employeename',
    )
);

Change the names to adjust your needs. 更改名称以调整您的需求。 Now, if your model is set as containable and you retrieve the Project model with those models in it, you'll get something like 现在,如果您的模型被设置为可包含的,并且检索其中包含这些模型的Project模型,您将得到类似

array('Project' => array(/*data project*/),
      'ResponsibleEmployee' => array(/*name*/),
      'AttendingEmployee' => array(/*name*/)
)

(or another variation of that array depending on how the query was made). (或该数组的另一种形式,具体取决于查询的方式)。

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

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