Question

For a forum-like website that I'm building, I have PHP classes 'user' and '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'? 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!

Was it helpful?

Solution

Your solution is good. I can't see any better option.

OTHER TIPS

Just for clarify;

Now, with OOP, the goal is to work with objects.

So first step is to create object from database. If you want to go further, you can use an ORM and create your DAO. With this layer, you can manipulate object, modify them and finally save them into database with OOP approach. An exemple, Doctrine2 : Doctrine website

Now, for simple projet you do not absolutely need ORM. Just take care to work with object, and, for exemple, create little method in your object: save(...) who Insert in DB your object.

Complexify it by adding update, delete,... and finally go on CRUD system. I think it's good idea before go directly on ORM, and see you to learn and concept good OO project.

Hope it's will help you (and sorry for my very bad english :) )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top