* @license PHP License * @package Wombat * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableAddRealtion * * Update a column in table * * @version 0.1.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableAddRelation extends WBEvent_Handler { /** * handler config * * - table name of table to update * - field name of column to update * - key id of data to update * - relationkey id to load data from relation table * - relationtable table to load data from * - relationfield column in relation table * * @var array */ protected $config = array( 'table' => '', 'field' => '', 'key' => '', '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) { $id = $e->get($this->config['key']); if (empty($id) || '__new' == $id) { WBClass::load('WBException_Datasource'); throw new WBException_Config('Id must neither be "__new" nor empty', 1, __CLASS__); } foreach ($this->config as $k => $v) { if (empty($v)) { WBClass::load('WBException_Config'); throw new WBException_Config('Config parameter "' . $k . '" must not be empty!', 2, __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['relationkey'], 'value' => $e->get($this->config['relationkey']) ); $options = array( 'limit' => 1 ); $list = $table->get($this->config['relationtable'], null, null, $clause, $options); if (empty($list)) { return true; } $save = array( $this->config['relationfield'] => $list[0][$this->config['relationfield']] ); $table->save($this->config['table'], $id, $save); return true; } }