* @license PHP License * @package Wombat * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableLoadRealtion * * Load list of n:m relations from foreign table * * @version 0.1.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableLoadRelation extends WBEvent_Handler { /** * handler config * * - field column to select and add to event data * - relationkey id to load data from relation table * - relationtable table to load data from * - relationfield column in relation table * * @var array */ protected $config = array( 'field' => '', 'relationkey' => '', 'relationtable' => '', 'relationfield' => '' ); /** * Update data in table using data from related table * * Update exactly one column in one table for one row using primary key * Hence primary key is required, as well as column and table name. * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { foreach ($this->config as $k => $v) { if (empty($v)) { WBClass::load('WBException_Config'); throw new WBException_Config('Config parameter "' . $k . '" must not be empty!', 1, __CLASS__); } } // if column is already set, we are done already $old = $e->get($this->config['field']); if (!empty($old)) { return; } $table = WBClass::create('WBDatasource_Table'); $clause = array(); $clause[] = array( 'field' => $this->config['relationfield'], 'value' => $e->get($this->config['relationkey']) ); $list = $table->get($this->config['relationtable'], null, null, $clause, $options); if (empty($list)) { return true; } $value = array(); foreach ($list as $l) { $value[] = $l[$this->config['field']]; } $e->set($this->config['field'], $value); return true; } }