我有这样的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;
}
}
我想在fetchAll
方法中返回$resultSet
之前检查一些东西,但我有很多这样的方法,不想在每个方法中放置 if 或 where,想使函数与类分离,我试图使用TableGateway
的EventFeature
,但 zend 缺乏这方面的文档。
你们有什么建议吗?
谢谢
我找到了一种方法来做到这一点,
在模型中,在构造上添加EventFeature
特征:
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new EventFeature());
然后在您的Module.php
中,您需要创建一个SharedEventManager
.这是因为当模型实例化EventFeature
时,它会在其中创建一个新EventManager
,与 Zend 在引导中创建的EventManager
不同,因此您只需要创建该并附加以下事件:
public function onBootstrap(MvcEvent $event)
{
$eventManager = $event->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach(
'ZendDbTableGatewayTableGateway',
EventFeature::EVENT_PRE_SELECT,
function (EventFeatureTableGatewayEvent $e) {
//Do stuff here or call some method
});
}