rendered paste body<?phpclass tx extends orm{ protected $lines; public function __construct() { parent::__construct('tx'); $this->lines = array(); } public function load($id) { parent::load($id); // load children $db = my_db::get_instance(); $res = $db->query('select id from tx_line where tx_id=?', $id); while($row = $res->fetch()) { $line = new orm('tx_line'); $line->load($row['id']); $this->line_add($line); } } protected function line_add($line) { $id = count($this->lines); $this->lines[$id] = array($line, 1); return $id; } public function line_new() { return $this->line_add(new orm('tx_line')); } public function line_list() { $result = array(); foreach($this->lines as $k => $v) if($v[1]) $result[] = $k; return $result; } public function line_get($line) { return $this->lines[$line][0]; } public function line_delete($line) { $this->lines[$line][1] = 0; } public function save() { $dtc = dtc::get_instance(); $in_tx = $dtc->in_tx(); if(!$in_tx) $dtc->begin(); try { $db = my_db::get_instance(); parent::save(); foreach(array_keys($this->lines) as $k) { if($this->lines[$k][1]) { $this->lines[$k][0]->tx_id = $this->data['id']; $this->lines[$k][0]->save(); } else { $this->lines[$k][0]->delete(); } } $check = $db->query('SELECT (COUNT(*) > 1) num, (SUM(amount) = 0) total FROM tx_line WHERE tx_id = ?', $this->data['id'])->fetch(); if(!$check['num']) throw new Exception('Transaction must involve at least one account.'); if(!$check['total']) throw new Exception('Transaction must total to 0.'); } catch(Exception $ex) { if(!$in_tx) $dtc->rollback(); throw $ex; } if(!$in_tx) $dtc->commit(); foreach(array_keys($this->lines) as $k) { if(!$this->lines[$k][1]) { unset($this->lines[$k]); } } return $this->data['id']; }}?>