Search code examples
phpdatabasezend-framework2factorytablegateway

Using TableGateway and a Class Factory to create a ResultSet of different Classes - ZF2


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.


Solution

  • I did this small app in zend 2 with solution for your problem. Most interesting will be FabResiltSet class which extends from AbstractResultSet and QuestionFab which has array as a map for your classes you want to make.

    https://github.com/Kil0p/FabResultSet