我有AbstractTableGateway
这样:
class FundsTable extends AbstractTableGateway
{
protected $table = 'tb_funds';
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
$this->resultSetPrototype = new HydratingResultSet(new FundsHydrator(), new Fund());
$this->initialize();
}
public function fetchAll($year)
{
$select = new Select(array("f" => $this->table));
$resultSet = $this->selectWith($select);
$resultSet->initialize($resultSet->toArray());
return $resultSet;
}
}
我想在$resultSet
方法中返回fetchAll
之前检查一下,但我有很多这样的方法,并且不想在每个方法中放置if或a where,为了使一个函数解耦为类,我试图使用EventFeature
TableGateway
,但zend缺乏关于它的文档。
你们有什么建议吗?
由于
答案 0 :(得分:1)
我找到了一种方法,
在模型jut中,在构造中添加EventFeature
功能:
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new EventFeature());
然后在Module.php
中,您需要创建一个SharedEventManager
。这是因为当模型实例化EventFeature
时,会在其中创建一个 new EventManager
,与Zend在bootstrap中创建的EventManager
不同,所以你只需要创建并附加以下事件:
public function onBootstrap(MvcEvent $event)
{
$eventManager = $event->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach(
'Zend\Db\TableGateway\TableGateway',
EventFeature::EVENT_PRE_SELECT,
function (EventFeature\TableGatewayEvent $e) {
//Do stuff here or call some method
});
}