[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActiveRecord/ -> AkObserver.php (summary)

(no description)

Author: Bermi Ferrer
Copyright: Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
License: GNU Lesser General Public License
File Size: 178 lines (5 kb)
Included or required: 5 times
Referenced: 0 times
Includes or requires: 3 files
 AkActiveRecord.php
 Ak.php
 AkInflector.php

Defines 1 class

AkObserver:: (5 methods):
  __construct()
  _initModelObserver()
  observe()
  setObservedModels()
  update()


Class: AkObserver  - X-Ref

Observer classes respond to life-cycle callbacks to implement trigger-like
behavior outside the original class. This is a great way to reduce the clutter
that normally comes when the model class is burdened with functionality that
doesn't pertain to the core responsibility of the class.

Example:

class CommentObserver extends AkObserver
{
function afterSave($comment)
{
Ak::mail("admin@example.com", "New comment was posted",
$comment->toString());
}
}

This Observer sends an email when a Comment::save is finished.

## Observing a class that can't be inferred

Observers will by default be mapped to the class with which they share a name.
So CommentObserver will be tied to observing Comment, ProductManagerObserver
to ProductManager, and so on. If you want to name your observer differently
than the class you're interested in observing, you can use the
AkActiveRecord->observe() class method:

function afterUpdate(&$account)
{
$AuditTrail =& new AuditTrail($account, "UPDATED");
$AuditTrail->save();
}

If the audit observer needs to watch more than one kind of object, this can be
specified with multiple arguments:

function afterUpdate(&$record)
{
$ObservedRecord =& new AuditTrail($record, "UPDATED");
$ObservedRecord->save();
}

The AuditObserver will now act on both updates to Account and Balance by
treating them both as records.

## Available callback methods

The observer can implement callback methods for each of these methods:
beforeCreate, beforeValidation, beforeValidationOnCreate, beforeSave,
afterValidation, afterValidationOnCreate, afterCreate and afterSave

## Triggering Observers

In order to activate an observer, you need to call create an Observer instance
and attach it to a model.

In the Akelos Framework, this can be done in controllers using the short-hand
of for example:

$ComentObserverInstance =& new CommentObserver();
$Model->addObserver(&$ComentObserverInstance);

__construct()   X-Ref
$_observing array of models that we're observing


_initModelObserver()   X-Ref
adds itself to the models which are listed
in var $observe = array(...)


observe(&$target)   X-Ref
Constructs the Observer

param: $subject the name or names of the Models to observe

setObservedModels()   X-Ref
Constructs the Observer

param: $subject the name or names of the Models to observe

update($state = '')   X-Ref




Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6