Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Record_PubSubRecord Class Reference

A 'pub-sub' record is a record that can be modified via pub/sub. More...

Inheritance diagram for P4Cms_Record_PubSubRecord:
P4Cms_Record P4Cms_Record_Connected P4Cms_Model P4Cms_ModelInterface P4Cms_Content

List of all members.

Public Member Functions

 delete ($description=null)
 Extend basic delete to allow third-party participation via pub/sub.
 save ($description=null, $options=null)
 Extend basic save to allow third-party participation via pub/sub.

Static Public Member Functions

static count (P4Cms_Record_Query $query=null, P4Cms_Record_Adapter $adapter=null)
 Extended basic count to provide a 'query' pub/sub topic.
static fetchAll ($query=null, P4Cms_Record_Adapter $adapter=null)
 Extended basic fetchAll to provide a 'query' pub/sub topic.
static getTopic ()
 Get the topic for publishing this record.
static hasTopic ()
 Check if a topic has been set for this form.

Public Attributes

const SAVE_SKIP_PUBSUB = 'skipPubSub'

Static Protected Attributes

static $_topic = null

Detailed Description

A 'pub-sub' record is a record that can be modified via pub/sub.

Topics published:

<record-topic>.preSave - do work (e.g. manipulate record) before it is saved <record-topic>.postSave - do work after record is saved, but before it is committed <record-topic>.query - influence query options for fetch/fetchAll/count/exists

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

static P4Cms_Record_PubSubRecord::count ( P4Cms_Record_Query query = null,
P4Cms_Record_Adapter adapter = null 
) [static]

Extended basic count to provide a 'query' pub/sub topic.

Parameters:
P4Cms_Record_Query$queryoptional - query options to augment result.
P4Cms_Record_Adapter$adapteroptional - storage adapter to use.
Returns:
integer The count of all matching records

Reimplemented from P4Cms_Record.

    {
        $query = static::_normalizeQuery($query);

        // if no adapter given, use default.
        $adapter = $adapter ?: static::getDefaultAdapter();

        // give third-parties a change to influence query.
        P4Cms_PubSub::publish(
            static::getTopic() . P4Cms_PubSub::TOPIC_DELIMITER . "query",
            $query,
            $adapter
        );

        return parent::count($query, $adapter);
    }
P4Cms_Record_PubSubRecord::delete ( description = null)

Extend basic delete to allow third-party participation via pub/sub.

Parameters:
string$descriptionoptional - a description of the change.
Returns:
P4Cms_Record provides fluent interface.

Reimplemented from P4Cms_Record.

Reimplemented in P4Cms_Content.

    {
        // ensure we have a change description.
        $description = $description ?: $this->_generateSubmitDescription();

        // start the batch
        $adapter = $this->getAdapter();
        $batch   = !$adapter->inBatch()
            ? $adapter->beginBatch($description)
            : false;

        // wrap in a try/catch so we can cleanup if something goes wrong.
        try {

            // allow third-parties to interact with record prior to delete.
            P4Cms_PubSub::publish(
                static::getTopic() . P4Cms_PubSub::TOPIC_DELIMITER . "delete",
                $this
            );

            // now let parent do a normal delete.
            parent::delete($description);

        } catch (Exception $e) {
            if ($batch) {
                $adapter->revertBatch();
            }
            throw $e;
        }

        // commit the batch.
        if ($batch) {
            $adapter->commitBatch();
        }

        return $this;
    }
static P4Cms_Record_PubSubRecord::fetchAll ( query = null,
P4Cms_Record_Adapter adapter = null 
) [static]

Extended basic fetchAll to provide a 'query' pub/sub topic.

Parameters:
P4Cms_Record_Query | array | null$queryoptional - query options to augment result.
P4Cms_Record_Adapter$adapteroptional - storage adapter to use.
Returns:
P4Cms_Model_Iterator all records of this type.

Reimplemented from P4Cms_Record.

    {
        $query = static::_normalizeQuery($query);

        // if no adapter given, use default.
        $adapter = $adapter ?: static::getDefaultAdapter();

        // give third-parties a change to influence query.
        P4Cms_PubSub::publish(
            static::getTopic() . P4Cms_PubSub::TOPIC_DELIMITER . "query",
            $query,
            $adapter
        );

        return parent::fetchAll($query, $adapter);
    }
static P4Cms_Record_PubSubRecord::getTopic ( ) [static]

Get the topic for publishing this record.

Returns:
string the topic for publishing this record.
Exceptions:
InvalidArgumentExceptionif no topic is set.
    {
        if (static::$_topic === null) {
            throw new P4Cms_Record_Exception("No topic set for this record");
        }

        return static::$_topic;
    }
static P4Cms_Record_PubSubRecord::hasTopic ( ) [static]

Check if a topic has been set for this form.

Returns:
bool true if a topic has been set, false otherwise.
    {
        return !is_null(static::$_topic);
    }
P4Cms_Record_PubSubRecord::save ( description = null,
options = null 
)

Extend basic save to allow third-party participation via pub/sub.

Two topics are published:

preSave - fires after batch is started, but before record is saved postSave - fires after record is saved, but before batch is committed

The entire operation is wrapped in a batch (unless there is already a batch underway). The batch will be committed automatically unless an exception occurs, in which case the batch will be reverted.

Parameters:
string$descriptionoptional - a description of the change.
null | string | array$optionsoptional - augment save behavior:

SAVE_THROW_CONFLICTS - throw exceptions on conflicts, default is to silently overwrite SAVE_SKIP_PUBSUB - don't publish pre/post save topics

Returns:
P4Cms_Record provides a fluent interface

Reimplemented from P4Cms_Record.

Reimplemented in P4Cms_Content.

    {
        // if we are skipping pub/sub, let parent take care of everything.
        if (in_array(static::SAVE_SKIP_PUBSUB, (array) $options)) {
            return parent::save($description, $options);
        }

        // ensure we have a save description.
        $description = $description ?: $this->_generateSubmitDescription();

        // start the batch
        $adapter = $this->getAdapter();
        $batch   = !$adapter->inBatch()
            ? $adapter->beginBatch($description)
            : false;

        // wrap in a try/catch so we can cleanup if something goes wrong.
        try {

            // allow third-parties to manipulate record prior to save.
            P4Cms_PubSub::publish(
                static::getTopic() . P4Cms_PubSub::TOPIC_DELIMITER . "preSave",
                $this
            );

            // now let parent do a normal save.
            parent::save($description, $options);

            // allow third-parties to interact with record post save.
            P4Cms_PubSub::publish(
                static::getTopic() . P4Cms_PubSub::TOPIC_DELIMITER . "postSave",
                $this
            );

        } catch (Exception $e) {
            if ($batch) {
                $adapter->revertBatch();
            }
            throw $e;
        }

        // commit the batch.
        if ($batch) {
            $adapter->commitBatch(null, $options);
        }

        return $this;
    }

Member Data Documentation

P4Cms_Record_PubSubRecord::$_topic = null [static, protected]

Reimplemented in P4Cms_Content.


The documentation for this class was generated from the following file: