简体   繁体   English

MySQL一对多关系

[英]MySQL one-to-many relationship

For a forum-like website that I'm building, I have PHP classes 'user' and 'post'. 对于我正在构建的类似论坛的网站,我有PHP类“ user”和“ post”。 A user has an array of posts. 用户有一系列帖子。 How to resemble that connection in a relational database? 如何类似于关系数据库中的连接? With a foreign key on 'post', pointing to a 'user'? 在“ post”上使用外键指向“用户”? This makes sense to me because of a one-to-one relationship, whereas 'user' to 'post' would be one-to-many, and so far as I can see. 由于一对一的关系,这对我来说很有意义,而据我所见,“用户”到“发布”将是一对多的。 Any thoughts are welcome - thanks! 欢迎任何想法-谢谢!

Your solution is good. 您的解决方案很好。 I can't see any better option. 我看不到任何更好的选择。

Just for clarify; 只是为了澄清;

Now, with OOP, the goal is to work with objects. 现在,使用OOP,目标是使用对象。

So first step is to create object from database. 因此,第一步是从数据库创建对象。 If you want to go further, you can use an ORM and create your DAO. 如果您想走得更远,可以使用ORM并创建DAO。 With this layer, you can manipulate object, modify them and finally save them into database with OOP approach. 通过这一层,您可以操作对象,对其进行修改,最后使用OOP方法将其保存到数据库中。 An exemple, Doctrine2 : Doctrine website 例如,Doctrine2: Doctrine网站

Now, for simple projet you do not absolutely need ORM. 现在,对于简单的projet,您并不是绝对需要ORM。 Just take care to work with object, and, for exemple, create little method in your object: save(...) who Insert in DB your object. 只需小心使用对象,例如,在对象中创建一些小方法:save(...)谁在数据库中插入了对象。

Complexify it by adding update, delete,... and finally go on CRUD system. 通过添加更新,删除等来使其复杂化,最后进入CRUD系统。 I think it's good idea before go directly on ORM, and see you to learn and concept good OO project. 我认为在直接进行ORM之前,这是一个好主意,并希望您学习并构思好的OO项目。

Hope it's will help you (and sorry for my very bad english :) ) 希望它能对您有所帮助(对不起,我的英语不好:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM