«
»

archived blog posts

my workaround: Cakephp model associations not in primary keys

10.23.08 | 6 Comments

I was just asked by another filipino cakephp developer about how to make model associations but the associations are not in primary keys. I already had been into this situation before, but I didn’t pursue a work around because, I felt it was not following good convention with regards to cakephp.

Well anyway, his db structure is like this. He has employees table , and belongs to Departments Table. But he said that an employee record has a department code. Meaning, to relate employees to departments, you must use a code. not an id, or a primary key.

I was a bit challenged. but it was easy to make a solution.

In short, I did this:
{code type=php}
var $belongsTo = array(
“Departments”=>array(
“className”=>”Departments”,
“foreignKey”=>”",
“conditions”=>”Employees.Deparment_code=Department.code”
)
);
{/code}

it works, wow!

Tags:

6 Comments


«
»