Re: Join a table to Itself with a join table

2008-09-23 Thread Werner Petry Moraes

 True but if I have employee Bob who is a supervisor.  Bob supervises
 Tina, and Pete but Bob also has a supervisor, Sam.

 In this scenario wouldn't Bob have to exist in both the Employee and
 Supervisor tables?  That would be duplicate data which is bad.

Right, a supervisor may have a supervisor himself.
You can use the same model for both and put the following relationship:

var $hasAndBelongsToMany = array(
'Supervisor' = array('className' = 'Employee',
   'joinTable' =
'employees_supervisors',
   'foreignKey' = 'employee_id',
   'associationForeignKey'
= 'supervisor_id',
   'unique' = true
   'conditions' =
array('Supervisor.is_supervisor' = true), // don't know if you
declare this condition using the relationship name or the model name.
if this doesn't work, try changing Supervisor for Employee
)
);


Werner Petry Moraes
http://werner.inf.br/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Join a table to Itself with a join table

2008-09-22 Thread BravoFoxtrot

Hi All,

I've been reading other posts on this but I still can seem to make it
work.  This post seems to be what I want to do but I'm still having
trouble.
http://groups.google.com/group/cake-php/browse_thread/thread/6fea193fdd352916/5997925ec651fe1a?lnk=gstq=join+to+itself+join+table#5997925ec651fe1a

I have a employees table:
CREATE TABLE employees (
id integer NOT NULL,
user_id integer,
start_date date,
termination_date date,
vacation_rate numeric(15,2),
is_supervisor boolean,
full_time boolean,
cont boolean,
part_time boolean,
part_time_amount numeric(15,2),
on_leave boolean,
birthday date
);

And join table employees_supervisors:
CREATE TABLE employees_supervisors (
id integer NOT NULL,
employee_id integer,
supervisor_id integer
);

The relationship is that an employee can have multiple supervisors and
a supervisor can supervise many employees.  I read that the HABTM is
maybe not the best option, so I tried to make hasMany and belongsTo
releationships:

In Employee Model:
var $hasMany = array(
'Supervisors'=array('className'='Employee',
'foreignKey'='employee_id',
'conditions' = '',
'fields' = '',
'order' = ''
)
);

var $belongsTo = array(
'User' = array('className' = 'User',
'foreignKey' = 'user_id',
'conditions' = '',
'fields' = '',
'order' = ''
),

'Supervisor'=array('className'='EmployeesSupervisor',
'foreignKey' = 'id',
'conditions' =
array('Employee.is_supervisor'=true),
'fields' = '',
'order' = ''
)
);

In EmployeesSupervisor model:
var $hasMany = array(
'Employees' = array('className' = 'EmployeesSupervisor',
'foreignKey' = 'employee_id',
'conditions' = '',
'fields' = '',
'order' = ''
)
);

var $belongsTo = array(
'Supervisor' = array('className' = 'Employee',
'foreignKey' = 'id',
'conditions' = '',
'fields' = '',
'order' = ''
)
);


My problem right now is that when the joins occur it joins
Employee.id = Supervisor.id when what I want is
Employee.id = Supervisor.employee_id and/or Employee.id =
Supervisor.supervisor_id.

I tried the associationForeignKey but I think that only works with
HABTM relationships.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread Werner Petry Moraes

Hi,

This is a HABTM relationship.

Take all that off and use only this.

var $hasAndBelongsToMany = array(
'Supervisor' = array('className' = 'Employee',
'joinTable' = 
'employees_supervisors',
'foreignKey' = 'employee_id',
'associationForeignKey' = 
'supervisor_id',
'unique' = true
)
);

 My problem right now is that when the joins occur it joins
 Employee.id = Supervisor.id when what I want is
 Employee.id = Supervisor.employee_id and/or Employee.id =
 Supervisor.supervisor_id.

 I tried the associationForeignKey but I think that only works with
 HABTM relationships.


Werner Petry Moraes
http://werner.inf.br/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread Werner Petry Moraes

Oh, i didn't read that condition:

 array('Employee.is_supervisor'=true)

You might want to add another model for the supervisors too, and
change that relationship to call it.
Then put another HABTM in the supervisors model pointing to the employees.


Werner Petry Moraes
http://werner.inf.br/



On Mon, Sep 22, 2008 at 3:40 PM, Werner Petry Moraes [EMAIL PROTECTED] wrote:
 Hi,

 This is a HABTM relationship.

 Take all that off and use only this.

var $hasAndBelongsToMany = array(
'Supervisor' = array('className' = 'Employee',
'joinTable' = 
 'employees_supervisors',
'foreignKey' = 'employee_id',
'associationForeignKey' = 
 'supervisor_id',
'unique' = true
)
);

 My problem right now is that when the joins occur it joins
 Employee.id = Supervisor.id when what I want is
 Employee.id = Supervisor.employee_id and/or Employee.id =
 Supervisor.supervisor_id.

 I tried the associationForeignKey but I think that only works with
 HABTM relationships.


 Werner Petry Moraes
 http://werner.inf.br/


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread BravoFoxtrot

Hi,

Thanks for the response.  I want to avoid adding a Supervisor model as
a supervisor is just another employee.  The
EmployeesSupervisor.supervisor_id is a foreign key to Employees.id.

I did initial try to set this up as HABTM but after reading other post
that suggested otherwise I switched to the current method.




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread Werner Petry Moraes

Hey,

 Thanks for the response.  I want to avoid adding a Supervisor model as
 a supervisor is just another employee.  The
 EmployeesSupervisor.supervisor_id is a foreign key to Employees.id.

Well, the supervisor has different behavior (Employee.is_supervisor =
true), so, I'd see it as another entity and model.
But, you can put this condition in the relationship declaration and
use the same model.

 I did initial try to set this up as HABTM but after reading other post
 that suggested otherwise I switched to the current method.

Your situation surely looks like a HABTM. I don't know if that
previous solution would work for you.

Werner Petry Moraes
http://werner.inf.br/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread BravoFoxtrot


 Well, the supervisor has different behavior (Employee.is_supervisor =
 true), so, I'd see it as another entity and model.
 But, you can put this condition in the relationship declaration and
 use the same model.

True but if I have employee Bob who is a supervisor.  Bob supervises
Tina, and Pete but Bob also has a supervisor, Sam.

In this scenario wouldn't Bob have to exist in both the Employee and
Supervisor tables?  That would be duplicate data which is bad.


Sam (The boss)
  |
Bob  (middle manager)
  /   \
Tina Pete (employees)

The following link describes a similar scenario, so this what I'm
trying to model:
http://groups.google.com/group/cake-php/browse_thread/thread/6fea193fdd352916/5997925ec651fe1a?lnk=gstq=join+to+itself+join+table#5997925ec651fe1a


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread BravoFoxtrot


 Well, the supervisor has different behavior (Employee.is_supervisor =
 true), so, I'd see it as another entity and model.
 But, you can put this condition in the relationship declaration and
 use the same model.

True but if I have employee Bob who is a supervisor.  Bob supervises
Tina, and Pete but Bob also has a supervisor, Sam.

In this scenario wouldn't Bob have to exist in both the Employee and
Supervisor tables?  That would be duplicate data which is bad.


Sam (The boss)
  |
Bob  (middle manager)
  /   \
Tina Pete (employees)

The following link describes a similar scenario, so this what I'm
trying to model:
http://groups.google.com/group/cake-php/browse_thread/thread/6fea193fdd352916/5997925ec651fe1a?lnk=gstq=join+to+itself+join+table#5997925ec651fe1a


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Join a table to Itself with a join table

2008-09-22 Thread BravoFoxtrot


 Well, the supervisor has different behavior (Employee.is_supervisor =
 true), so, I'd see it as another entity and model.
 But, you can put this condition in the relationship declaration and
 use the same model.

True but if I have employee Bob who is a supervisor.  Bob supervises
Tina, and Pete but Bob also has a supervisor, Sam.

In this scenario wouldn't Bob have to exist in both the Employee and
Supervisor tables?  That would be duplicate data which is bad.


Sam (The boss)
  |
Bob  (middle manager)
  /   \
Tina Pete (employees)

The following link describes a similar scenario, so this what I'm
trying to model:
http://groups.google.com/group/cake-php/browse_thread/thread/6fea193fdd352916/5997925ec651fe1a?lnk=gstq=join+to+itself+join+table#5997925ec651fe1a


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---