public function __construct(Dbi_Record $user) { if (!is_a($user->model(), 'Model_User')) { throw new Exception('Record used for Auth must be a User'); } $this->user = $user; }
public function insert(Dbi_Record $record) { $collection = $this->_db->{$record->model()->name()}; $data = $record->getArray(!$this->enforceSchemas); $result = $collection->insert($data, true); /*$record->set('id', "{$data['_id']}"); $record->save();*/ $primary = $record->model()->index('primary'); if (!is_null($primary) && count($primary) == 1 && $primary[0] == 'id') { $data['id'] = "{$data['_id']}"; } return $data; }
public function insert(Dbi_Record $record) { $data = $record->getArray(!$this->enforceSchemas); // Get rid of fields that are not defined in the schema. // TODO: Should undefined fields generate an error? foreach ($data as $key => $value) { if (is_null($record->model()->field($key))) { unset($data[$key]); } else { // Convert arrays to JSON // (Objects depend on __toString() for conversion) if (is_array($value)) { $data[$key] = json_encode($value); } } } $insert = new Dbi_Sql_Query_Insert(); //($record->model()->prefix() . $record->model()->name(), $data); //$this->_connection->query($insert->query()); //if ($this->_connection->errno) { // throw new Exception($this->_connection->error); //} $insert->table($record->model()->prefix() . $record->model()->name()); foreach ($data as $key => $value) { $insert->set($key, $value); } $stmt = $this->_execute($insert); $primary = $record->model()->index('primary'); if (is_array($primary) && count($primary['fields']) == 1) { $data[$primary['fields'][0]] = $this->_pdo->lastInsertId(); } // Return the data that was saved so Dbi_Record objects can update // automatically generated primary keys return $data; }
public function insert(Dbi_Record $record) { self::$queryCount++; $data = $record->getArray(!$this->enforceSchemas); // Get rid of fields that are not defined in the schema. // TODO: Should undefined fields generate an error? foreach ($data as $key => $value) { if (is_null($record->model()->field($key))) { unset($data[$key]); } else { // Convert arrays to JSON // (Objects depend on __toString() for conversion) if (is_array($value)) { $data[$key] = json_encode($value); } } } $insert = new BuildSql_Insert($record->model()->prefix() . $record->model()->name(), $data); mysql_query($insert->query()); if (mysql_error()) { throw new Exception(mysql_error()); } $primary = $record->model()->index('primary'); if (is_array($primary) && count($primary['fields']) == 1) { $data[$primary['fields'][0]] = mysql_insert_id(); } // Return the data that was saved so Dbi_Record objects can update // automatically generated primary keys return $data; }
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(); } }
/** * Fetch a single record by its primary key. * @param scalar|array $key The primary key value. If the primary * key contains more than one column, use an associative array. * @return Dbi_Record */ public static function Get($key) { $cls = get_called_class(); $model = new $cls(); $primary = $model->index('primary'); if (is_null($primary)) { throw new Exception("The schema for {$cls} does not identify a primary key"); } if (!is_array($key)) { if (count($primary['fields']) > 1) { throw new Exception("The schema for {$cls} has more than one field in its primary key"); } $key = array($primary['fields'][0] => $key); } foreach ($primary['fields'] as $field) { if (!isset($key[$field])) { throw new Exception("No value provided for the {$field} field in the primary key for " . get_called_class()); } $model->where("{$field} = ?", $key[$field]); } //$src = Dbi_Source::GetModelSource($model); $result = $model->select(); if ($result->count()) { if ($result->count() > 1) { throw new Exception("{$cls} returned multiple records for primary key {$id}"); } $record = $result[0]; } else { $record = new Dbi_Record($model, null); $record->setArray($key, false); } return $record; }