Question

Background

I'm writing an app where I need to pull all the employees (table 1) and add them to a dropdown. This then saves the employee_id in the supervisors table (Table 2).

What I have

I have the two tables:

,------------,-----------,------------,--------------,
| Table_1_id | Name      | Surname    | Table_2_id   |
'------------'-----------'------------'--------------'

,------------,------------,
| Table_2_id | Table_1_id |
'------------'------------'

What have I tried

I've tried the following:

$hrEmployees = $this->HrEmployee->find('list',
    array(
        'fields' => array("id","fullname"),
        'order' => array('HrEmployee.name ASC','HrEmployee.surname ASC')
    )
);
$this->set(compact('hrEmployees'));

This is in my Table 1 Controller under the edit/add function.

Problem

I have no idea how to do this. I thought that using a virtual field and just passing that to the view will then fill the dropdown with the names, but clearly I'm wrong. If I have to redesign the DB structure I will, but I just need this to work! Can someone help?

Was it helpful?

Solution

You don't need to have table2_Id in your employee table. You can fetch employee array from employee array and pass it to template. When ever user selects an employee from the dropdown, you need to run another query to insert the selected employee Id into supervisor table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top