Pages

Friday, December 7, 2012

Interview Question on relations in cakephp

Recently I was asked on different types of relations in between models in a cakephp framework

Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to many ingredients.

Then I was given 4 tables as follows

Employee:
Eid,fname,lname

Department:
Deptid,deptname

Salary: 
Sid,salary

EmpSalDeptrelation:
eid,sid,deptid

To these I was told to determine how relations will be represented in EmpSalDeptrelation table so that if I fetch 1 employee I get his salary department along

Answer is to the EmpSalDeptrelation model add the relationships as

eid belongsTo Eid of employee,sid belongs to Sid of salary,deptid belongs to Deptid of Department

and then in the respective controller which uses($uses) this model
add
$this->empsaldeptrelation->find();
set the conditions where eid equaks to concerned employee id; 

No comments:

Post a Comment