I have a question table in MySQL, a question TableGateway and a QuestionFactory Model. Based on the QuestionType received by the TableGateway I would like to instantiate a different class e.g. TextQuestion(), BoolQuestion() etc. In a fetchall situation this would return a resultset of these different Classes.
Everything so far is fairly standard:
'Application\Model\QuestionTable' => function($sm) {
$tableGateway = $sm->get('QuestionTableGateway');
$table = new \Application\Model\QuestionTable($tableGateway);
return $table;
},
'QuestionTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Application\Model\QuestionFactory());
return new \Zend\Db\TableGateway\TableGateway('questions', $dbAdapter, null, $resultSetPrototype);
},
QuestionTableGateway extends TableGatewayBase which has:
/**
* fetch all
* @return resultset
*/
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
So I need to get the QuestionFactory to return a different class based on the QuestionType in the database.
I was thinking of extending the Zend\Db\ResultSet with a application specific version, with a differently implemented of setArrayObjectPrototype() method.
Any help would be greatly appreciated.
Abor.