Junction Table with mutiple primary keys

2019-09-09 16:17发布

问题:

I started to learn Atk just a week ago and I decided to reimplement the business intranet which is becoming unmaintainable.

The model abstraction is very cool to use but I wonder how to specify multiple primary keys for my junction tables.

For example, I have sites and I want to assign machines on them for a period. Junction table

This is very important to me to not touch the database.

回答1:

I guess you can do that using joins in your Models if you're not allowed to add simple auto_incremented id field in that junction table.

But adding id field will be much more easier for you and also will work better in future.

For example, you can have some issues with deleting record without unique id field. I know (id_machine,id_site) is most likely unique in your case, but it's still hard to work with. One simple id field is easier, faster and better :)



回答2:

Dark has a good suggestion. You can also create a calculated ID field, I have never tried it myself but do try it

$model->getElement('id')->destroy();
$model->addExpression('id')->set('contact(id_machine,"-",id_site)');


标签: atk4