views:

411

answers:

1

I have a database structure that has a Person table which contains fields such as name, email, company_id, personType and the like. Because not all Person's are necessarily system user's, I have a separate table User that defines userName and password for those Person's that are User's in the system.

I have the following code to define the Table Data Gateway for the Person Table:

class Model_Table_Person extends Zend_Db_Table_Abstract
{
    protected $_name = 'person';
    protected $_primary = 'person_id';

    protected $_referenceMap = array(
     'Company' => array(
      'columns' => 'company_id',
      'refTableClass' => 'Company',
      'refColumns' => 'id'
     ),
     'Store' =>  array(
      'columns' => 'store_id',
      'refTableClass' => 'Store',
      'refColumns' => 'id'
     )
    );

    public function findByPersonType(string $personType)
    {
     $where = $this->getAdapter()->quoteInto('personType = ?', $personType);
     return $this->fetchAll($where);
    }
}

And this code defines the domain object for Person:

class Model_Person
{
    protected static $_gateway;

    protected $_person;

    public static function init()
    {
     if(self::$_gateway == null)
     {
      self::$_gateway = new Model_Table_Person();
     }
    }

    public static function get(string $searchString, string $searchType = 'id') 
    {
     self::init();

     switch($searchString)
     {
      case 'id':
       $row = self::$_gateway->find($id)->current();
       break;
     }

     return self::factory($row);
    }

    public static function getCollection(string $searchString, string $searchType = null)
    {
     self::init();

     switch($searchType)
     {
      case 'userType':
       $row = self::$_gateway->findByPersonType($searchString);
       break;
      default:
       $personRowset = self::$_gateway->fetchAll();
       break;
     }

     $personArray = array ();

     foreach ($personRowset as $person)
     {
      $personArray[] = self::factory($person);
     }

     return $personArray;
    }

    public function getCompany()
    {
     return $this->_person->findParentRow('Company');
    }

    public function getStore()
    {
     return $this->_person->findParentRow('Store');
    }

    protected static function factory(Zend_Db_Table_Row $row) 
    {
     $class = 'Model_Person_' . ucfirst($row->personType);
     return new $class($row);
    }

    // protected constructor can only be called from this class, e.g. factory()
    protected function __construct(Zend_Db_Table_Row $personRow)
    {
     $this->_person = $personRow;
    }
}

Lastly, I have another Table Data Gateway for User:

class Model_Table_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';
    protected $_primary = 'person_id';

    public function findByUserName()
    {

    }
}

And a basic class that extends the Model_Person table like this:

class Model_User extends Model_Person
{    
    public function login()
    {

    }

    public function setPassword()
    {

    } 
}

How do I properly extend the "Model_User" class (which serves a basic type for all other types of users but one) to use the "Model_Person" class functions which map to one table while also mapping the actual "Model_User" functions to use a second table?

+1  A: 

This is a huge weakness for PHP (prior to version 5.3.0) -- its lack of support for late static binding.

That is, when one static method such as get() calls another static method such as init(), it always uses the init() method defined in that class. If a subclass defines an alternative method init() and calls get(), expecting it to call the overridden version of init(), this won't happen.

class A
{
  static $var = null;
  static function init() { self::$var = 1234; }
  static function get() { self::init(); }
}

class B extends A
{
  static function init() { self::$var = 5678; }
}

B::get();
print B::$var . "\n";

This prints "1234" whereas you might expect it to print "5678". It's as if A::get() doesn't know that it's part of class B. The workaround has been that you have to copy the implementation for the get() method into the subclass, even if it does nothing differently from the superclass. This is very unsatisfying.

PHP 5.3.0 attempts to fix this, but you have to code it slightly differently in get():

function get() {
  static::init();
}

PHP 5.3.0 is still in alpha release currently.


There are a few possible workarounds:

  • Don't subclass Person as User, but instead add the login and password attributes to the Person class. If those attributes are NULL, then it's a non-user person, and the functions login() and setPassword() should heed this and throw an exception or return false or something.

  • Use a different get() method per subtype: getUser(), getPerson(), etc. Each of these would initialize its own respective Table object.

Bill Karwin
Would you design the relationships between the User and Person classes differently then? I thought to try this method of extending the Model_Person class and then implement a second $_gateway object for the User object but it doesn't seem to be coming together well.
Noah Goodrich
I think I've decided that I need to use the $_referenceMap's of User and Person tables to define usage of the data in the User table rather than try to create some sort of multiple gateway inheritance? Does that more or less sound correct?
Noah Goodrich
Yeah, I have the feeling that the PHP language doesn't currently support what you're trying to do. You might need a factory class after all.
Bill Karwin