* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableUpdate * * Update a column in table * * @version 0.2.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableUpdate extends WBEvent_Handler { /** * handler config * * - table name of table to update * - column name of column to update * - value new value * - valueindex data index where to load the value from * - key primary key of row to update * - keyindex data index where to achieve the primary key from * * @var array */ protected $config = array( 'table' => '', 'column' => '', 'value' => '', 'valueindex' => '', 'key' => '', 'keyindex' => '', 'dbconfig' => '' ); /** * 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__); } if (empty($this->config['column'])) { WBClass::load('WBException_Config'); throw new WBException_Config('Column must not be empty!', 2, __CLASS__); } if (empty($this->config['dbconfig'])) { $params['dbconfig'] = 'config'; } $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__); } $value = $this->config['value']; if (empty($value) && !empty($this->config['valueindex'])) { $value = $e->get($this->config['valueindex']); } $params = array( 'dbconfig' => $this->config['dbconfig'] ); /** @var $table WBDatasource_Table */ $table = WBClass::create('WBDatasource_Table', $params); WBClass::load('WBString'); $value = WBString::populate($value, $e->get()); // update $save = array( $this->config['column'] => $value ); $table->save($this->config['table'], $key, $save); return true; } }