php - ZF2,添加多个表

标签 php zend-framework2

我遵循了为新的 ZF2 教程创建的 Album 示例。对于第一个模块,一切都很好,我能够修改以显示我想用于我的应用程序的数据。

我的问题是添加第二个模块,更复杂的是,它管理 2 个以上的数据表。每次我转到该页面时,我都会看到 Apache 错误页面,这不是很有帮助。我修改的唯一主要类是第二个模块目录中的 Module.php。也许有人能看出我的猜测有多么错误。

模块背景:该 View 将显示 4 种类型的表格,其中包括 Fosters 和 Volunteers。我为每个创建了一个模型和表。一切似乎都很好,除了我确实修改了工厂设置以包括每个表(FosterTable 和 VolTable)。你认为这是我的错误吗?

我在这个页面上工作:http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html

对于 /TeamMgr/Module.php,我添加了以下内容:

namespace TeamMgr;

use TeamMgr\Model\FosterTable;
use TeamMgr\Model\VolTable;

class Module
{
 public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'TeamMgr\Model\VolTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new VolTable($dbAdapter);
                    return $table;
                },

                'TeamMgr\Model\FosterTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new FosterTable($dbAdapter);
                    return $table;
                },
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}

然后在我的 Controller 中添加:

public function indexAction(){
    return new ViewModel(array(
            'fosters'=>$this->getFosterTable()->fetchAll(),
    ));
    return new ViewModel(array(
            'vols'=>$this->getVolTable()->fetchAll(),
    ));     

}

那么我有多不正常?

最佳答案

您不能多次调用return。正在发生的事情是函数的执行在第一次 return 调用时终止,而第二次调用从未被调用。你需要写:

return new ViewModel(array(
        'fosters'=>$this->getFosterTable()->fetchAll(),
        'vols'=>$this->getVolTable()->fetchAll(),
));

这是基本的 PHP。所以我建议先学PHP,再学ZF。即使对于使用普通 PHP 经验丰富的人来说,ZF 也非常复杂。有适合初学者的更好的框架(如 code igniter、kohana 或 cakephp)——我认为你应该从那里开始……

关于php - ZF2,添加多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12450653/

相关文章:

javascript - 无法在 <script> 标签内回显 PHP 变量?

PHP 循环 x 次,使用 while 和 if

php - 从 PHP Zend 2 中的 POST 请求中解码 JSON 字符串

zend-framework2 - Zend FrameWork 2 Get ServiceLocator In Form 并填充下拉列表

rest - 如何在 Zend Framework 2 中发送 PUT 请求?

php - ZF2+Doctrine2 - 在 Mac 上突然出现错误 : "Doctrine\ORM\EntityManager"; no instance returned

php - 意外的 T_ENCAPSED_AND_WHITESPACE,预期 T_STRING 或 T_VARIABLE 或 T_NUM_STRING 错误

php - Laravel View Composer 为每个 View 复制 SQL 查询

mysql - Zend\Db : Select from subquery

php - 获取所有 POST 数据并通过电子邮件发送