문제

I have a function where a user can request a project. The request has 2 fields for other employees to be added.

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).

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.

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.

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

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top