I think I've understood the basic idea behind object mapping but there is one gap in my knowledge base that I hope to fill now. First let me tell you what I understand out of the whole thing.
I have my database tables alright, and for every table I have a separate class. Each instance of each class represents a row of the table, for example table users - id, name, password would be represented like
class User {
protected $id;
protected $name;
protected $password;
}
Which would have methods like setName, setPassword, create, save, getById
and so on. This all makes a great lot of sense to me and brings a very good look to the code as well as maintainability, it's just awesome, I've fallen in love with this model of data handling ever since I discovered it.
However, now wo the thing that is not very clear to me. If I have a table that is a connecting table, sorry don't really know what the term for that is - a table which shows connection between other tables, how do I manipulate that table? Let me give an example again because I'm not very good with explanations. Say I add a table where users can add their contacts, just plain and simple, nothing fancy for the example. In the table contacts users can add other users like bookmarks. Since that table wouldn't have a PK, or would probably have a complex PK, It doesn't really fit very well in the above method - 1 row = 1 instance, because there is just no way to set up the "contact" object in the pattern shown above. Yeah I could probably do something like
class Contact {
protected $forUser;
protected $contact;
}
Where forUser
would be the user who's contact that is and contact
would be the actual user the contact contains, but as I said it is just not looking very well and doesn't make perfect sense for me.
The method I've come up with is to have additional methods in the initial class itself that manage the "connecting" tables, something like
class User {
...
public function addContact(\User $user){
...
}
}
Could you give me some guidance and tell me what are good and bad practices on this matter?