Instances of this class represent database records. Can be subclassed for
complicated cases, e.g. with attached files.
// representation of an existing row by id $r = new waDbRecord(new
appSomeModel(), $id);
// read info from database // (read is lazy: no queries is performed at
constructor time) echo $r->field;
// update existing database row $r->field = 'new value';
$r->save();
// create new database row $r = new waDbRecord(new appSomeModel());
$r->field = 'value'; $new_id = $r->save();
You may use array access instead of field access: $r->field is the same as
$r['field']
Methods summary
public
|
#
__construct( waModel $m, mixed $id = null )
Parameters
- $m
waModel
$m model to use for saving
- $id
mixed $id id of existing record, as accepted by model; omit to create new record.
Overrides
|
public
string|integer|null
|
#
getId( )
Id of current record as was passed to constructor.
Id of current record as was passed to constructor.
Note that when ->exists() finds out that record does not exist in DB, it
sets internal id to null and never tries to access DB again. $this['id'] and
$this->id are still accessable though and allow to create record with any
id.
Returns
string|integer|null id of the record
|
public
mixed
|
#
save( )
Save this record to database: insert or update depending on whether record
exists in DB. Silently ignores all keys that have no corresponding fields in
database table.
Save this record to database: insert or update depending on whether record
exists in DB. Silently ignores all keys that have no corresponding fields in
database table.
Returns
mixed this record id
Throws
|
public
mixed
|
#
load( mixed $field_or_db_row = null )
Load data if not loaded yet.
Load data if not loaded yet.
Called lazily when array or field access is used.
When $field_or_db_row is an array then it is used as data source and no
database queries are made. Array structure is the same as returned by
$this->toArray().
When $field_or_db_row is a field name (string) then this function ensures
that this field is loaded (when possible). It's not guaranteed to load the rest
of the fields. This gets called lazily when field/array access interface is
used.
When $field_or_db_row is empty, all available data is fetched from database
using id given to a constructor.
Parameters
- $field_or_db_row
mixed $field_or_db_row
Returns
mixed $this
Throws
|
public
|
#
delete( )
Delete this record from database. $this is not guaranteed to be accessable
via array or field access after deletion.
Delete this record from database. $this is not guaranteed to be accessable
via array or field access after deletion.
|
public
boolean
|
#
exists( )
Returns
boolean whether this record has corresponding row in database
|
protected
array
|
#
getDefaultValues( )
Generates default values for new record.
Generates default values for new record.
To be overriden in subclasses. Default implementation returns array(field
=> '') for each field from model.
Returns
array same structure as $this->toArray() returns
|
protected
|
#
getLoadableKeys( )
List of keys that are guaranteed by $this->load() to set.
List of keys that are guaranteed by $this->load() to set.
To be overriden in subclasses. Default implementation uses field names from
$this->m->getMetadata()
|
protected
|
#
beforeSave( )
Called by save() before writing to DB.
Called by save() before writing to DB.
Subclasses may override this method to tune save() behavior, e.g. to validate
and prepare $this->db_row.
Throws
|
protected
|
#
afterSave( )
Called by save() after writing to DB when $this->id is already created and
available. $this->rec_data still contains values just written to DB. When
this function returns, remaining values from $this->rec_data will be moved to
$this->persistent.
Called by save() after writing to DB when $this->id is already created and
available. $this->rec_data still contains values just written to DB. When
this function returns, remaining values from $this->rec_data will be moved to
$this->persistent.
Subclasses may override this method to tune save() behavior, e.g. to move
attachments to appropriate place using record id.
|
protected
|
#
doLoad( array $field_or_db_row = null )
Called by $this->load() to populate data into $this->persistent
Called by $this->load() to populate data into $this->persistent
Subclasses may override this method to tune load() behavior, e.g. to load
data lazily by field name passed in $field_or_db_row. Subclasses overriding this
should also call parent::doLoad() since the base class uses it to load data from
$this->m model.
Parameters
- $field_or_db_row
array $field_or_db_row see $this->load()
Throws
|
protected
|
#
afterLoad( array $field_or_db_row = null )
Called by $this->load() after data has been added to $this->persistent
either from $field_or_db_row (if it's an array), or from $this->m model.
Called by $this->load() after data has been added to $this->persistent
either from $field_or_db_row (if it's an array), or from $this->m model.
Subclasses may override this method to tune load()'s behavior, e.g. to
populate additional data into class fields or modify data fetched from DB.
Parameters
- $field_or_db_row
array $field_or_db_row see $this->load()
|
protected
|
#
beforeDelete( )
Called by $this->delete() before removing record from $this->m
Called by $this->delete() before removing record from $this->m
|
protected
|
#
afterDelete( )
Called by $this->delete() after removing record from $this->m
Called by $this->delete() after removing record from $this->m
|
public
array
|
#
toArray( )
Convert this object to a native array. Will currently cause infinite
recursion for self-containing structures.
Convert this object to a native array. Will currently cause infinite
recursion for self-containing structures.
Returns
array
Overrides
|
public
&
|
|
public
|
|
public
|
#
keyExists( mixed $name )
same as array_key_exists for native arrays
same as array_key_exists for native arrays
Overrides
|