5 use RedBeanPHP\Observer as Observer;
6 use RedBeanPHP\OODBBean as OODBBean;
7 use RedBeanPHP\Observable as Observable;
10 * RedBean Model Helper.
12 * Connects beans to models.
13 * This is the core of so-called FUSE.
15 * @file RedBeanPHP/ModelHelper.php
16 * @author Gabor de Mooij and the RedBeanPHP Community
20 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
21 * This source file is subject to the BSD/GPLv2 License that is bundled
22 * with this source code in the file license.txt.
24 class SimpleModelHelper implements Observer
27 * Gets notified by an observable.
28 * This method decouples the FUSE system from the actual beans.
29 * If a FUSE event happens 'update', this method will attempt to
30 * invoke the corresponding method on the bean.
32 * @param string $eventName i.e. 'delete', 'after_delete'
33 * @param OODBean $bean affected bean
37 public function onEvent( $eventName, $bean )
43 * Attaches the FUSE event listeners. Now the Model Helper will listen for
44 * CRUD events. If a CRUD event occurs it will send a signal to the model
45 * that belongs to the CRUD bean and this model will take over control from
46 * there. This method will attach the following event listeners to the observable:
48 * - 'update' (gets called by R::store, before the records gets inserted / updated)
49 * - 'after_update' (gets called by R::store, after the records have been inserted / updated)
50 * - 'open' (gets called by R::load, after the record has been retrieved)
51 * - 'delete' (gets called by R::trash, before deletion of record)
52 * - 'after_delete' (gets called by R::trash, after deletion)
53 * - 'dispense' (gets called by R::dispense)
55 * For every event type, this method will register this helper as a listener.
56 * The observable will notify the listener (this object) with the event ID and the
57 * affected bean. This helper will then process the event (onEvent) by invoking
58 * the event on the bean. If a bean offers a method with the same name as the
59 * event ID, this method will be invoked.
61 * @param Observable $observable object to observe
65 public function attachEventListeners( Observable $observable )
67 foreach ( array( 'update', 'open', 'delete', 'after_delete', 'after_update', 'dispense' ) as $eventID ) {
68 $observable->addEventListener( $eventID, $this );