Reputation: 149
I was wondering how to handle separately users and members in an application where all registered users are considered as users, and users who have a team, are considered as members. Others users are guest.
Basically, a simple user can find a team, receive a team request, etc. A member can see his team profile, see other members, and do a lot of more things relative to members and his team.
Currently, users, members and teams are handle in three different classes, all of them represent their database table structure.
So I have 3 tables in my database as follows:
And I have for example this class to represent a user:
class Lib_Model_Users_User implements Zend_Auth_Adapter_Interface, Zend_Acl_Role_Interface
{
protected $_gateway;
protected $_roleId;
protected $_member = null;
protected $_team = null;
protected $_data = array(
'user_id' => null,
'email' => null,
'password' => null,
'first_name' => null,
'last_name' => null,
'gender' => null,
'address' => null,
'city' => null,
'country' => null,
'postal_code' => null,
'phone_mobile' => null,
'phone_home' => null,
'date_of_birth' => null,
'occupation' => null,
'active' => false,
'created_on' => null,
'last_login' => null,
'last_update' => null);
public function __construct($data, $gateway)
{
$this->setGateway($gateway);
$this->populate($data);
if (!isset($this->user_id)) {
if (!isset($this->email)) {
if (!isset($this->first_name, $this->last_name))
throw new Lib_Exception('Initial data must contain an email or a user id or a pair firstname/lastname');
}
}
}
public function setMember($data)
{
$gateway = new Lib_Model_Teams_Gateway();
$this->_member = $gateway->createMember($data);
}
public function getMember($status = 'active')
{
if (null === $this->getTeam()) {
return null;
}
if (null === $this->_member) {
$this->_member = $this->getTeam()->getMember($this->user_id);
}
if (is_string($status)) {
$status = array($status);
}
foreach ($status as $state) {
if ($this->_member->status == $state) {
return $this->_member;
}
}
return null;
}
public function getTeam($active = true)
{
if ($this->_team === null) {
$this->_team = $this->getGateway()->fetchTeam($this->user_id, $active);
}
return $this->_team;
}
public function save()
{
$gateway = $this->getGateway();
$dbTable = $gateway->getDbTable();
$row = $dbTable->find($this->user_id)->current();
if ($row) {
foreach ($this->_data as $key => $value) {
$row->$key = $value;
}
return $row->save();
}
$this->user_id = $dbTable->insert($this->_data);
return $this->user_id;
}
public function getRoleId()
{
if (null === $this->_roleId)
$this->_roleId = $this->user_id;
return $this->_roleId;
}
..........
// etc.
..........
}
As you can see, this class is very "stick" to my database fields, I don't know if it's the best way to do it though.
My Members model is basically the same thing, except that it represents the "team_users" table and it doesn't have the set/getMembers methods but have methods relative to members.
Now, you understand how hard is it to manage such a thing, each time I want to get a member, I need to check if a user has a team, if so, I create a member instance using getMember()
, etc.
From the member side, it's hard to get an information about a member such as his first_name
, I always need to check the user_id
, create a user instance and get his first_name
.
How can I handle this kind of thing? I was thinking maybe my Member model should just extends the Users model since a member is also a user? Is it a good idea? How would you do it?
Thank you.
Upvotes: 1
Views: 343
Reputation: 26730
Model it the way the things are, regardless how your database is structured - that will turn out to work best in most cases.
I don't think that there really is a beautiful solution to handle your exact situation. But how about something like this?
$user = new User($data, $gateway);
if ($user->hasTeam()) {
$user = Member::fromUser($user); // returns a new instance of 'Member'
}
Upvotes: 1