const ATTR_CACHE_LIFESPAN = 151;
const ATTR_RESULT_CACHE_LIFESPAN = 151;
const ATTR_LOAD_REFERENCES = 153;
- const ATTR_RECORD_LISTENER = 154;
const ATTR_THROW_EXCEPTIONS = 155;
const ATTR_DEFAULT_PARAM_NAMESPACE = 156;
const ATTR_QUERY_CACHE = 157;
case IPF_ORM::ATTR_EXPORT:
case IPF_ORM::ATTR_DECIMAL_PLACES:
case IPF_ORM::ATTR_LOAD_REFERENCES:
- case IPF_ORM::ATTR_RECORD_LISTENER:
case IPF_ORM::ATTR_THROW_EXCEPTIONS:
case IPF_ORM::ATTR_DEFAULT_PARAM_NAMESPACE:
case IPF_ORM::ATTR_AUTOLOAD_TABLE_CLASSES:
return true;
}
- public function addRecordListener($listener, $name = null)
- {
- if ( ! isset($this->attributes[IPF_ORM::ATTR_RECORD_LISTENER]) ||
- ! ($this->attributes[IPF_ORM::ATTR_RECORD_LISTENER] instanceof IPF_ORM_Record_Listener_Chain)) {
-
- $this->attributes[IPF_ORM::ATTR_RECORD_LISTENER] = new IPF_ORM_Record_Listener_Chain();
- }
- $this->attributes[IPF_ORM::ATTR_RECORD_LISTENER]->add($listener, $name);
-
- return $this;
- }
-
- public function getRecordListener()
- {
- if ( ! isset($this->attributes[IPF_ORM::ATTR_RECORD_LISTENER])) {
- if (isset($this->parent)) {
- return $this->parent->getRecordListener();
- }
- return null;
- }
- return $this->attributes[IPF_ORM::ATTR_RECORD_LISTENER];
- }
-
- public function setRecordListener($listener)
- {
- if ( ! ($listener instanceof IPF_ORM_Record_Listener_Interface)
- && ! ($listener instanceof IPF_ORM_Overloadable)
- ) {
- throw new IPF_ORM_Exception("Couldn't set eventlistener. Record listeners should implement either IPF_ORM_Record_Listener_Interface or IPF_ORM_Overloadable");
- }
- $this->attributes[IPF_ORM::ATTR_RECORD_LISTENER] = $listener;
-
- return $this;
- }
-
public function getAttribute($attribute)
{
if (is_string($attribute)) {
if ($record->isValid()) {
$event = new IPF_ORM_Event($record, IPF_ORM_Event::RECORD_SAVE);
$record->preSave($event);
- $record->getTable()->getRecordListener()->preSave($event);
+ $record->getTable()->notifyRecordListeners('preSave', $event);
$state = $record->state();
if ( ! $event->skipOperation) {
$pendingDelete->delete();
}
- $record->getTable()->getRecordListener()->postSave($event);
+ $record->getTable()->notifyRecordListeners('postSave', $event);
$record->postSave($event);
} else {
$conn->transaction->addInvalid($record);
$record->preSave($event);
- $record->getTable()->getRecordListener()->preSave($event);
+ $record->getTable()->notifyRecordListeners('preSave', $event);
if ( ! $event->skipOperation) {
switch ($record->state()) {
}
}
- $record->getTable()->getRecordListener()->postSave($event);
+ $record->getTable()->notifyRecordListeners('postSave', $event);
$record->postSave($event);
}
{
$event = new IPF_ORM_Event($record, IPF_ORM_Event::RECORD_DELETE);
$record->preDelete($event);
- $record->getTable()->getRecordListener()->preDelete($event);
+ $record->getTable()->notifyRecordListeners('preDelete', $event);
return $event->skipOperation;
}
{
$event = new IPF_ORM_Event($record, IPF_ORM_Event::RECORD_DELETE);
$record->postDelete($event);
- $record->getTable()->getRecordListener()->postDelete($event);
+ $record->getTable()->notifyRecordListeners('postDelete', $event);
}
public function saveAll()
$event = new IPF_ORM_Event($record, IPF_ORM_Event::RECORD_UPDATE);
$record->preUpdate($event);
$table = $record->getTable();
- $table->getRecordListener()->preUpdate($event);
+ $table->notifyRecordListeners('preUpdate', $event);
if ( ! $event->skipOperation) {
$identifier = $record->identifier();
$record->assignIdentifier(true);
}
- $table->getRecordListener()->postUpdate($event);
+ $table->notifyRecordListeners('postUpdate', $event);
$record->postUpdate($event);
$event = new IPF_ORM_Event($record, IPF_ORM_Event::RECORD_INSERT);
$record->preInsert($event);
$table = $record->getTable();
- $table->getRecordListener()->preInsert($event);
+ $table->notifyRecordListeners('preInsert', $event);
if ( ! $event->skipOperation) {
if ($table->getOption('joinedParents')) {
}
$table->addRecord($record);
- $table->getRecordListener()->postInsert($event);
+ $table->notifyRecordListeners('postInsert', $event);
$record->postInsert($event);
return true;
return $dataSet;
}
-}
\ No newline at end of file
+}
$isSimpleQuery = count($this->_queryComponents) <= 1;
// Holds the resulting hydrated data structure
$result = array();
- // Holds hydration listeners that get called during hydration
- $listeners = array();
// Lookup map to quickly discover/lookup existing records in the result
$identifierMap = array();
// Holds for each component the last previously seen element in the result set
// Initialize
foreach ($this->_queryComponents as $dqlAlias => $data) {
$componentName = $data['table']->getComponentName();
- $listeners[$componentName] = $data['table']->getRecordListener();
$identifierMap[$dqlAlias] = array();
$prev[$dqlAlias] = null;
$idTemplate[$dqlAlias] = '';
// Ticket #1115 (getInvoker() should return the component that has addEventListener)
$event->setInvoker($table);
$event->set('data', $rowData[$rootAlias]);
- $listeners[$componentName]->preHydrate($event);
+ $table->notifyRecordListeners('preHydrate', $event);
$index = false;
if ($isSimpleQuery || ! isset($identifierMap[$rootAlias][$id[$rootAlias]])) {
$element = $driver->getElement($rowData[$rootAlias], $componentName);
$event->set('data', $element);
- $listeners[$componentName]->postHydrate($event);
+ $table->notifyRecordListeners('postHydrate', $event);
// do we need to index by a custom field?
if ($field = $this->_getCustomIndexField($rootAlias)) {
$table = $map['table'];
$componentName = $table->getComponentName();
$event->set('data', $data);
- $listeners[$componentName]->preHydrate($event);
+ $table->notifyRecordListeners('preHydrate', $event);
$parent = $map['parent'];
$relation = $map['relation'];
if ( ! $indexExists || ! $indexIsValid) {
$element = $driver->getElement($data, $componentName);
$event->set('data', $element);
- $listeners[$componentName]->postHydrate($event);
+ $table->notifyRecordListeners('postHydrate', $event);
if ($field = $this->_getCustomIndexField($dqlAlias)) {
if (isset($prev[$parent][$relationAlias][$element[$field]])) {
// [FIX] Tickets #1205 and #1237
$event->set('data', $element);
- $listeners[$componentName]->postHydrate($event);
+ $table->notifyRecordListeners('postHydrate', $event);
$prev[$parent][$relationAlias] = $element;
}
{
return isset($this->_queryComponents[$alias]['map']) ? $this->_queryComponents[$alias]['map'] : null;
}
-}
\ No newline at end of file
+}
public function buildListener($listener)
{
- return PHP_EOL." ".'$this->addListener(new '.$listener.'());';
+ return PHP_EOL.' $this->getTable()->listeners[\''.$listener.'\'] = new '.$listener.'();';
}
-
public function buildAttributes(array $attributes)
{
$build = PHP_EOL;
IPF_ORM::ATTR_RESULT_CACHE => null,
IPF_ORM::ATTR_QUERY_CACHE => null,
IPF_ORM::ATTR_LOAD_REFERENCES => true,
- IPF_ORM::ATTR_RECORD_LISTENER => new IPF_ORM_Record_Listener(),
IPF_ORM::ATTR_THROW_EXCEPTIONS => true,
IPF_ORM::ATTR_QUERY_LIMIT => IPF_ORM::LIMIT_RECORDS,
IPF_ORM::ATTR_IDXNAME_FORMAT => "%s_idx",
$event = new IPF_ORM_Event($record, $callback['const'], $this, $params);
$record->$callback['callback']($event);
- $table->getRecordListener()->$callback['callback']($event);
+ $table->notifyRecordListeners($callback['callback'], $event);
}
}
return $this->_table;
}
- public function addListener($listener, $name = null)
- {
- $this->_table->addRecordListener($listener, $name = null);
- return $this;
- }
-
- public function getListener()
- {
- return $this->_table->getRecordListener();
- }
-
- public function setListener($listener)
- {
- $this->_table->setRecordListener($listener);
- return $this;
- }
-
public function index($name, array $definition = array())
{
if ( ! $definition) {
+++ /dev/null
-<?php
-
-class IPF_ORM_Record_Listener implements IPF_ORM_Record_Listener_Interface
-{
- public function preSerialize(IPF_ORM_Event $event){}
- public function postSerialize(IPF_ORM_Event $event){}
- public function preUnserialize(IPF_ORM_Event $event){}
- public function postUnserialize(IPF_ORM_Event $event){}
- public function preDqlSelect(IPF_ORM_Event $event){}
- public function preSave(IPF_ORM_Event $event){}
- public function postSave(IPF_ORM_Event $event){}
- public function preDqlDelete(IPF_ORM_Event $event){}
- public function preDelete(IPF_ORM_Event $event){}
- public function postDelete(IPF_ORM_Event $event){}
- public function preDqlUpdate(IPF_ORM_Event $event){}
- public function preUpdate(IPF_ORM_Event $event){}
- public function postUpdate(IPF_ORM_Event $event){}
- public function preInsert(IPF_ORM_Event $event){}
- public function postInsert(IPF_ORM_Event $event){}
- public function preHydrate(IPF_ORM_Event $event){}
- public function postHydrate(IPF_ORM_Event $event){}
-}
\ No newline at end of file
+++ /dev/null
-<?php
-
-class IPF_ORM_Record_Listener_Chain extends IPF_ORM_Access implements IPF_ORM_Record_Listener_Interface
-{
- protected $_listeners = array();
-
- public function add($listener, $name = null)
- {
- if ( ! ($listener instanceof IPF_ORM_Record_Listener_Interface) &&
- ! ($listener instanceof IPF_ORM_Overloadable)) {
- throw new IPF_Exception_ORM("Couldn't add eventlistener. Record listeners should implement either IPF_ORM_Record_Listener_Interface or IPF_ORM_Overloadable");
- }
- if ($name === null) {
- $this->_listeners[] = $listener;
- } else {
- $this->_listeners[$name] = $listener;
- }
- }
-
- public function get($key)
- {
- if ( ! isset($this->_listeners[$key])) {
- return null;
- }
- return $this->_listeners[$key];
- }
-
- public function set($key, $listener)
- {
- $this->_listeners[$key] = $listener;
- }
-
- public function preSerialize(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preSerialize($event);
- }
- }
-
- public function postSerialize(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preSerialize($event);
- }
- }
-
- public function preUnserialize(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preUnserialize($event);
- }
- }
-
- public function postUnserialize(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postUnserialize($event);
- }
- }
-
- public function preDqlSelect(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preDqlSelect($event);
- }
- }
-
- public function preSave(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preSave($event);
- }
- }
-
- public function postSave(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postSave($event);
- }
- }
-
- public function preDqlDelete(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preDqlDelete($event);
- }
- }
-
- public function preDelete(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preDelete($event);
- }
- }
-
- public function postDelete(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postDelete($event);
- }
- }
-
- public function preDqlUpdate(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preDqlUpdate($event);
- }
- }
-
- public function preUpdate(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preUpdate($event);
- }
- }
-
- public function postUpdate(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postUpdate($event);
- }
- }
-
- public function preInsert(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preInsert($event);
- }
- }
-
- public function postInsert(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postInsert($event);
- }
- }
-
- public function preHydrate(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->preHydrate($event);
- }
- }
-
- public function postHydrate(IPF_ORM_Event $event)
- {
- foreach ($this->_listeners as $listener) {
- $listener->postHydrate($event);
- }
- }
-}
+++ /dev/null
-<?php
-
-interface IPF_ORM_Record_Listener_Interface
-{
- public function preSerialize(IPF_ORM_Event $event);
- public function postSerialize(IPF_ORM_Event $event);
- public function preUnserialize(IPF_ORM_Event $event);
- public function postUnserialize(IPF_ORM_Event $event);
- public function preSave(IPF_ORM_Event $event);
- public function postSave(IPF_ORM_Event $event);
- public function preDelete(IPF_ORM_Event $event);
- public function postDelete(IPF_ORM_Event $event);
- public function preUpdate(IPF_ORM_Event $event);
- public function postUpdate(IPF_ORM_Event $event);
- public function preInsert(IPF_ORM_Event $event);
- public function postInsert(IPF_ORM_Event $event);
- public function preHydrate(IPF_ORM_Event $event);
- public function postHydrate(IPF_ORM_Event $event);
-}
protected $_invokedMethods = array();
protected $record;
+ public $listeners = array();
+
public function __construct($name, IPF_ORM_Connection $conn, $initDefinition = false)
{
$this->_conn = $conn;
throw new IPF_ORM_Exception(sprintf('Unknown method %s::%s', get_class($this), $method));
}
+
+ public function notifyRecordListeners($method, $event)
+ {
+ foreach ($this->listeners as $listener)
+ if (is_callable(array($listener, $method)))
+ $listener->$method($event);
+ }
}
+
<?php
-class IPF_ORM_Template_Listener_Orderable extends IPF_ORM_Record_Listener
+class IPF_ORM_Template_Listener_Orderable
{
private $columnName = 'ord';
<?php
-class IPF_ORM_Template_Listener_Sluggable extends IPF_ORM_Record_Listener
+class IPF_ORM_Template_Listener_Sluggable
{
protected $_options = array();
return $slug;
}
-}
\ No newline at end of file
+}
<?php
-class IPF_ORM_Template_Listener_Timestampable extends IPF_ORM_Record_Listener
+class IPF_ORM_Template_Listener_Timestampable
{
protected $_options = array();
public function __construct(array $options)
}
}
}
-}
\ No newline at end of file
+}
$this->columnName = $options['name'];
}
- public function getColumnName()
- {
- return $this->columnName;
- }
-
public function setTableDefinition()
{
$this->hasColumn($this->columnName, 'integer', null, '');
- $this->addListener(new IPF_ORM_Template_Listener_Orderable($this->columnName));
+ $this->getTable()->listeners['Orderable_'.$this->columnName] = new IPF_ORM_Template_Listener_Orderable($this->columnName);
}
}
$this->index($this->_options['indexName'], array('fields' => $indexFields,
'type' => 'unique'));
}
- $this->addListener(new IPF_ORM_Template_Listener_Sluggable($this->_options));
+ $this->getTable()->listeners['Sluggable_'.print_r($this->_options, true)] = new IPF_ORM_Template_Listener_Sluggable($this->_options);
}
}
if( ! $this->_options['updated']['disabled']) {
$this->hasColumn($this->_options['updated']['name'], $this->_options['updated']['type'], null, $this->_options['updated']['options']);
}
- $this->addListener(new IPF_ORM_Template_Listener_Timestampable($this->_options));
+ $this->getTable()->listeners['Timestampable_'.print_r($this->_options, true)] = new IPF_ORM_Template_Listener_Timestampable($this->_options);
}
-}
\ No newline at end of file
+}