dongya9346
2013-11-13 12:17
浏览 52
已采纳

使用TableGateway和Class Factory创建不同类的ResultSet - 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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题