public function update($record) { if ($record->exists()) { // Save revision $cls = $this->_revisionModelClassName; if (!$cls) { $cls = get_class($record->model()) . 'Revision'; } if (class_exists($cls)) { $revMod = new $cls(); $revRec = new Dbi_Record($revMod); $primary = $record->model()->primary(); if (count($primary['fields']) != 1) { throw new Exception("Model must have exactly one field in primary key"); } if ($record->model()->field($primary['fields'][0])) { // Foreign key in revision table matches source table's primary key $revKey = $primary['fields'][0]; } else { if ($record->model()->field($record->model()->name() . $primary['fields'][0])) { // Foreign key in revision table matches source table's name + "id" $revKey = $record->model()->name() . $primary['fields'][0]; } else { throw new Exception('Could not identify foreign key in revision table'); } } $revRec[$revKey] = $record[$primary['fields'][0]]; $init = $record->initArray(); $revRec['data'] = $init; $revRec['datemodified'] = $init[$this->_dateModifiedField]; $revRec->save(); } else { throw new Exception("{$cls} is not a valid model class"); } } }
public function deprecated_testModelSaveUpdate() { /* Assertions: * Record exists after select * Record marked dirty after change * Record unmarked dirty after save * Record exists after save * Unsaveable records throw exception on save */ $cls = $this->modelClass; $model = new $cls(); $record = new Dbi_Record($model, $this->mockRecordData()); $this->assertTrue($record->exists(), "{$cls} record 'does not exist' after select"); $primary = $record->model()->primary(); $changed = false; foreach ($record->model()->fields() as $key => $field) { if (!in_array($key, $primary['fields'])) { $record[$key] = 'foobar'; $changed = true; } } $this->assertTrue(!$changed || $record->dirty(), "{$cls} record was not marked dirty after change"); if ($record->saveable()) { $record->save(); $this->assertFalse($record->dirty(), "{$cls} record was still marked dirty after update"); $this->assertTrue($record->exists(), "{$cls} record 'does not exist' after update"); } else { $this->expectException('Exception', 'Saving a record that is not saveable should throw an exception'); $record->save(); } }