2015-12-07 2 views
5

に私はZend Frameworkの2を使用してシステムを開発し、キーconfig_cache_enabledapplication.config.phpでクロージャを回していますが、エラーを受信:Zend Frameworkの2 - 工場で閉鎖を交換Module.php

Fatal error: Call to undefined method set_state Closure::__()in /home/user/www/myProject.com/data/cache/module-config-cache.app_config.php online 185.

より良い検索私はそれがありませんでしたModule.phpでクロージャを使用することを推奨しました。これは、これがコンフィギュレーションキャッシュでこのエラーの原因となったためです。私はクロージャを工場で置き換えることを推奨する記事を読んでいます。

私は工場を作り、TableGatewayのDIをModule.phpに置き換えて工場で完璧に働いていました。私のやり方はOKかどうかわかりません。

これが問題を解決する正しい方法だと誰にでも教えてください。

application.config.php - 前: - はい、これは工場を行う方法である

namespace Admin\Service; 

use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

use Admin\Model\Pedidos; 
use Admin\Model\PedidosTable; 

class PedidosTableFactory implements FactoryInterface 
{ 
    public function createService(ServiceLocatorInterface $serviceLocator) 
    { 
     $dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter'); 

     $resultSetPrototype = new ResultSet(); 
     $resultSetPrototype->setArrayObjectPrototype(new Pedidos()); 

     $tableGateway = new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype); 
     $table = new PedidosTable($tableGateway); 

     return $table; 
    } 
} 
+1

これは正しく、はいです。しかし、__invokeという魔法の方法に切り替えることをお勧めします。私は後で説明をつけてコードサンプルを投稿します。 – Stanimir

+0

はい、問題ありません... – tasmaniski

答えて

0

:後

'Admin\Model\PedidosTable' => function($sm) { 
    $tableGateway = $sm->get('PedidosTableGateway'); 
    $table = new PedidosTable($tableGateway); 
    return $table; 
}, 
'PedidosTableGateway' => function($sm) { 
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
    $resultSetPrototype = new ResultSet(); 
    $resultSetPrototype->setArrayObjectPrototype(new Pedidos()); 
    return new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype); 
}, 

application.config.php:

'factories' => array(
    'PedidosTable' => 'Admin\Service\PedidosTableFactory', 
), 
'aliases' => array(
    'Admin\Model\PedidosTable' => 'PedidosTable', 
), 

TableFactory。例えばZF3 MVC Zend\Authentication as a Service FactoryのようなSOの例や、Zendの "詳細な解説"チュートリアル:https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-classを参照してください。このチュートリアルがZF3用に書かれていても、この部分は最新のZF2バージョンと完全に互換性があります。