Question

Let's say I have users who can befriend eachother, technically, how do I solve this using DAO in PHP?

Database:

users       |  user_friends
- userid    |  - userid_user1
- username  |  - userid_user2

PHP User object:

class User
{
  private $userid;
  private $username;

  // getters and setters
}

Should I use this method to befriend users:

$u1 = UserDAO::getInstance()->findUserById(1);
$u2 = UserDAO::getInstance()->findUserById(2);

UserDAO::getInstance()->befriendUsers($u1, $u2);

OR should I use this method:

$u1 = UserDAO::getInstance()->findUserById(1);
$u2 = UserDAO::getInstance()->findUserById(2);
$u1->befriend($u2);

// befriend method would look like this in the User object
public function befriend(User $u)
{
  UserDAO::getInstance()->befriend($this, $u);
}

Or maybe another way?

Was it helpful?

Solution

Actually, none. UserDAO deals with User but not with user relationships, which is a different concern.

To keep it simple, you should have another FriendsDao which will care about the friendships. Of course, you need to update the friendships when an user is deleted.

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