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?