在没有

时间:2015-10-23 18:26:13

标签: php zend-framework2

使用Zend Framework 2,我们在getServiceConfig()中设置Module.php,以便我们有类似于

的内容
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Application\Model\StudiesTable' =>  function($sm) {
                $tableGateway = $sm->get('StudiesTableGateway');
                $table = new StudiesTable($tableGateway);
                return $table;
            },
            'StudiesTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Study());
                return new TableGateway('studies', $dbAdapter, null, $resultSetPrototype);
            },
//...
}

然后,当从数据库中检索项目时,我们将使用它或其他类似的项目:

 public function getSessionsByParticipant($pId)
 {
    $select = new Select;
    $select->from('studySessions')->join("participantsToSessions",
        'studySessions.id = participantsToSessions.studySessionsId'
        )->where(array("participantsToSessions.participantId" => $pId)
        );
    $adapter = $this->tableGateway->getAdapter();
    $sgateway = new TableGateway('studySessions', $adapter);
    return $sgateway->selectWith($select);
 }

这适用于单个表操作,因为该表通常映射到模型类,如Participant或类Session。我们的Model/目录中的类具有由Zend填充的公共成员,并且它们易于访问和使用。

但是,使用JOIN,我没有很好的填充变量的策略。这导致了像Participant这样的笨拙的类,其中私有成员实际上就在那里,以便可以填充特定的联接,但不必处理Participant

有没有更好的方法来实现这一点,以便在连接表时,我仍然可以获得我需要的列而不会笨拙地将它们添加到模型类中?

提前致谢

0 个答案:

没有答案