开发者

Simple database considerations and CakePHP

I'm thinking of creating a simple game in order to help get my head around database design and CakePHP. The reasoning is that the game should ultimately be extendable, something that should be provided by MVC.

I'm struggling to get my head around a correct database design ho开发者_如何学编程wever, for example:

I have a table for users, just containing the usual details etc.

Each user in the game will have an armoury (storing weapons) and so my understanding is that a User and Armoury tables share a 'has' relationship.

My question is where should the foreign key be placed? If at all? Should I have a Armoury foreign key in the User table or a User foreign key in the Armoury table. Or should I have an entirely different table for the relationship between the two?

Also, regardless of approach, how do I then relate the structure of the database within CakePHP?


First of all, you should have in mind the cakePHP cookbook, it will help you, above all if you're starting, so you'll get in contact with the conventions, MVC...

I start from the end, in CakePHP the relations between tables are related with models(yes, ModelViewController,the first one).

If the armory has logic enough to be a model itself, then the relation between User and Armory would be User hasOne Armory. But if, for example, an User could have more than one, it would be User hasMany Armories. I don't know your case, just guessing.

About foreign keys, there are very important, because once you have the database you can "bake" (cake generates code for you) your models, views and controllers. And you do it with the information of the database.

So, I would say that if an User hasOne Armory, you should place a user_id field in your Armory table, to see to whom belongs this armory.

Hope it helps, I'm not experienced in cakePHP but this are the basics. I highly recommend you to read at least the most important parts of the cookbook.

Alf.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜