2012-03-27 27 views
1

我想知道如何分别处理用户和会员在所有注册用户都视为用户,谁拥有一批用户应用的应用,被认为是成员 。其他用户是嘉宾Zend框架 - 处理与用户和会员

基本上,一个简单的用户可以找到一个团队,接受团队请求等的成员都可以看到他的团队的个人资料,看到其他成员,并做了很多相对于成员和他的团队更多的事情。

目前,用户,成员和团队在三个不同的类中处理,它们都代表它们的数据库表结构。

所以,我在我的数据库3个表如下:

MySQL Workbench

我有例如这个类来表示用户:

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. 
    .......... 
} 

正如你所看到的,这个类是非常“坚持”我的数据库领域,我不知道这是否是最好的方式来做到这一点。

我的会员模式基本一样,只不过它代表的“team_users”表中,并没有设置/ getMembers方法,但具有相对于成员的方法。

现在,你明白管理这样的事情有多难,每次我想要一个成员,我需要检查一个用户是否有团队,如果是的话,我使用getMember()等创建一个成员实例

从会员方面来说,很难得到关于会员的信息,例如他的first_name,我总是需要检查user_id,创建一个用户实例并获得他的first_name

我该如何处理这种事情?我在想也许我的会员模式应该扩展用户模式,因为成员也是用户?这是个好主意吗?你会怎么做?

谢谢。

+0

无关 - 您创建的建模软件的截图与? – 2012-03-27 17:10:37

+0

MySQL Workbench;) – 2012-03-27 17:23:04

+1

好啊,从来没有尝试过。现在下载。谢谢! – 2012-03-27 17:31:08

回答

1

无论数据库的结构如何 - 以事物的方式进行建模 - 在大多数情况下,这种方法最适合工作。

  • 作为成员是某种类型的用户,Member类应扩展User类。
  • 只有成员类应该有一个方法getTeam(),因为只有成员拥有一个团队而普通用户则不具有。

我不认为真的有一个美丽的解决方案来处理您的确切情况。但是这样的事情呢?

$user = new User($data, $gateway); 
if ($user->hasTeam()) { 
    $user = Member::fromUser($user); // returns a new instance of 'Member' 
} 
+0

谢谢,我还有另一个问题。如何以及在哪里确定应该实例化哪个类?我应该使用根据他的身份(是否在teamusers表中)创建用户或成员的工厂? – 2012-03-28 07:29:48

+0

这就是我的例子:我会一直创建一个User实例,然后检查该用户是否有Team,如果是,则在Member实例中转换该User实例。 – Niko 2012-03-28 07:40:34

+0

谢谢你的解释! – 2012-03-28 08:13:28