0

我在从视图助手设置模型表时遇到问题。我使用了与常规控制器中使用的完全相同的代码:例如:

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

use Application\Model\MenusTable;

use Zend\ServiceManager\ServiceLocatorAwareInterface;  
use Zend\ServiceManager\ServiceLocatorInterface; 

**snipped**

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)  
{  
    $this->serviceLocator = $serviceLocator;  
    return $this;  
}

public function getServiceLocator()  
{  
    return $this->serviceLocator;  
} 

public function getMenusTable()
{
    if (!$this->menusTable) {
        $sm = $this->getServiceLocator();
        $this->menusTable = $sm->get('Application\Model\MenusTable');
    }

    return $this->menusTable;
}

public function allLinks()
{
    $all = $this->getMenusTable()->fetchAll();

    return $all;
}

但是我遇到了这个错误:

Catchable fatal error: Argument 1 passed to Application\Model\MenusTable::__construct() must be an instance of Zend\Db\Adapter\Adapter, none given, called in C:\xampp\**snipped**\zend\library\Zend\ServiceManager\AbstractPluginManager.php on line 177 and defined in C:\xampp\**snipped**\Application\src\Application\Model\MenusTable.php on line 14

主控制器一切正常,但在这里我似乎遇到了一个大问题 - 我是 Zend 的新手,但似乎没有从 Module.php 文件中获取工厂 - 有没有办法得到它?

我在我的 Module.php 中有这个 - 正如所说它在常规控制器中工作正常,但在视图助手中由于某种原因它没有被处理:

public function getServiceConfig()
{
    return array
    (
        'factories' => array
        (
            'Application\Model\MenusTable' => function($sm)
            {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table     = new MenusTable($dbAdapter);
                return $table;
            },
        ),
    );
}
4

2 回答 2

0

重新阅读您的问题后,我意识到您使用的是 ZF2。

这是关于使用 ServiceLocators 的教程http://framework.zend.com/wiki/display/ZFDEV2/Proposal+for+ServiceLocator+and+DependencyInjector

从文档中,您需要定义您的数据库连接。

$services = new ServiceLocator();

// Registering an object:
$services->set('db', $db);

// Lazy-loading by registering a closure:
$services->set('db', function() use ($config) {
$db = Db::factory($config->db);
    return $db;
});

// Retrieving:
$db = $services->get('db');
于 2012-10-26T20:01:32.373 回答
0

首先,您应该在模块中使用 getServiceConfig() ::

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'MODULE\Model\MenusTable' =>  function($sm) {
                $tableGateway = $sm->get('MenusTableGateway');
                $table = new MenusTable($tableGateway);
                return $table;
            },
            'MenusTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Menus());
                return new TableGateway('menus', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

适配器可能在您的 /config/autoload/global.php 中,如下所示:

    'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
    ),
),
// CONNECTION DB
'db' => array(
    'driver' => 'Pdo',
    'dsn' => 'mysql:dbname=YOURDBNAME;host=localhost',
    'username' => 'root',
    'password' => '',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),

接下来,您的 View 助手必须扩展 AbstractHelper 并实现 ServiceLocatorAwareInterface

class MyViewHelper extends AbstractHelper implements ServiceLocatorAwareInterface

我会把代码放在我的网站上

于 2012-12-22T15:33:01.077 回答