* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableUpdate * * Update a column in table * * @version 0.1.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableLoad extends WBEvent_Handler { /** * handler config * * - table name of table to update * - key primary key of row to update * - keyindex data index where to achieve the primary key from * - target name of event's data array to fill with table values * - prefix merge loaded data with event data using prefix * * @var array */ protected $config = array( 'table' => '', 'key' => '', 'keyindex' => 'id', 'target' => '', 'prefix' => '' ); /** * update column in 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) { if (empty($this->config['table'])) { WBClass::load('WBException_Config'); throw new WBException_Config('Table must not be empty!', 1, __CLASS__); } $key = $this->config['key']; if (empty($key) && !empty($this->config['keyindex'])) { $key = $e->get($this->config['keyindex']); } if (empty($key) || $key == '__new') { WBClass::load('WBException_Config'); throw new WBException_Config('Key may not be empty or "__new"', 3, __CLASS__); } $table = WBClass::create('WBDatasource_Table'); $data = $table->get($this->config['table'], $key); if (1 != count($data)) { return false; } $data = $data[0]; // store in target if (!empty($this->config['target'])) { $e->set($this->config['target'], $data); return true; } // merge with event data foreach ($data as $k => $v) { if (!empty($this->config['prefix'])) { $k = $this->config['prefix'] . $k; } $e->set($k, $v); } return true; } }