문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top