我在zf2中使用表网关。这是具有以下工作功能的简单模型表。我可以插入或更新记录,但我需要知道我们如何定义像beforeInsert或beforeUpdate这样的函数,就像我们在doctrine(beforePersist)中那样。
namespace Application\Model\AnyModel;
use Zend\Db\TableGateway\TableGateway;
class AnyModelTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getRecord($id) {
$id = (int) $id;
$rowSet = $this->tableGateway->select(array('id' => $id));
$row = $rowSet->current();
if (!$row) {
throw new \Exception("Could not found record with $id");
}
return $row;
}
public function saveRecord(AnyModel $record) {
$data = array(
'name' => $record->name,
'description' => $record->description,
'status' => $record->status,
'created_by' => $record->created_by,
'created_on' => $record->created_on,
);
$id = (int) $record->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getUser($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception("Could not update user with id $id");
}
}
}
public function deleteRecord($id) {
$this->tableGateway->delete(array('id' => $id));
}
public function getArrayCopy() {
return get_object_vars($this);
}
答案 0 :(得分:2)
一个简单而灵活的解决方案是将事件管理器实例用于表映射器并在正确的位置触发事件。
我将展开Event Manager's quick start example;理想情况下,这个膨胀的分配可以抽象出来。
<?php
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
class Foo implements EventManagerAwareInterface
{
const EVENT_INSERT_PRE = 'insert.pre';
const EVENT_INSERT_POST = 'insert.post';
const EVENT_INSERT_ERROR = 'insert.error';
protected $events;
public function setEventManager(EventManagerInterface $events)
{
$events->setIdentifiers(array(
__CLASS__,
get_called_class(),
));
$this->events = $events;
$this->init($events);
return $this;
}
public function getEventManager()
{
if (null === $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
protected function init(EventManagerInterface $events)
{
$events->attach(
static::EVENT_INSERT_PRE, // event that we listen to
array($this, 'onPreInsert') // The method that is triggered
100 // The priority
);
}
public function save(AnyModel $record)
{
try {
$em = $this->getEventManager();
$data = array('foo' => 'bar');
$em->trigger(static::EVENT_INSERT_PRE, compact('record', 'data'));
//...
$this->tableGateway->insert($data);
//...
$em->trigger(static::EVENT_INSERT_POST, compact('record'));
return true;
} catch (\Exception $e) {
$em->trigger(static::EVENT_INSERT_ERROR, compact('e'));
return false;
}
}
public function onPreInsert(EventInterface $event)
{
$data = $events->getParam('data', false);
if ($data !== $someValidDataTypeOrValue) {
return;
}
/** do stuff **/
}
}
答案 1 :(得分:0)
实例化TableGateway
use use Zend\Db\TableGateway\Feature;
/** @var \Zend\Db\TableGateway\TableGateway $memberTable */
$memberTable = TableFactory::get('member',new Feature\EventFeature());
/** @var \Zend\EventManager\ListenerAggregateInterface $eventService->getListener('Member') */
$eventService = $this->getServiceLocator()->get('Event');
$memberTable->getFeatureSet()
->getFeatureByClassName('Zend\Db\TableGateway\Feature\EventFeature')
->getEventManager()
->attachAggregate($eventService->getListener('Member'));
这是MemberListener
namespace Fselab\EventModel\Listeners;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventInterface;
use Zend\Db\TableGateway\Feature\EventFeature;
class Member implements ListenerAggregateInterface
{
private $handle;
protected $listeners = array();
function __construct()
{
}
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(EventFeature::EVENT_PRE_INSERT, function(EventInterface $e){
$insert = $e->getParam('insert');
return;
}, 100);
}
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
}