tags:

views:

132

answers:

2

I have an table with fields like:
empid,empname,empcode,department_id ...


I need an array like
[Model]
-------[department_id 1]
--------------[0]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

--------------[1]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

--------------[2]
--------------------[empid]
--------------------[empname]
--------------------[empcode]


-------[department_id 2]
--------------[0]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

--------------[1]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

--------------[2]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

-------[department_id 3]
--------------[0]
--------------------[empid]
--------------------[empname]
--------------------[empcode]

--------------[1]
--------------------[empid]
--------------------[empname]
--------------------[empcode]


Regards,

A: 

It seems that your 'Department' and 'Employee' entities have a one-to-many relationship. So better break up the table into two. have one table for Department and another for Employee. The Department ID would then be a foreign key on Employee table.

For example,

"Department" Table:- id, department_name, ..

"Employee" Table:- id, emp_name, emp_code,..., department_id (foreign key)

Read more about model relationships at: http://book.cakephp.org/view/79/Relationship-Types

Vicer
My question is not related to relationships actually, basically groupby isnt yeilding results to form this array to be used in views, am wondering if there is another way to do that, probably an self join or virtual table
openprojdevel
that's because group by returns unique keys isn't it? meaning.. if you group by department_id, it will return a certain department_id only once. Since you don't want to break this table for some reason.. which will easily retrieve the results the way you want, why don't you simply retrieve all records and use a loop to go through the array and categorize them the way you want? just an idea.
Vicer
+1  A: 

You should look for Set::combine function:

http://book.cakephp.org/view/662/combine

The exact solution for you will be (based on your example):

$employees = $this->Employee->find('all');
$result = Set::combine(
    $employees, 
    '/Employee/empid', 
    '/Employee', 
    '/Employee/department_id');

Hope this helps

Nik