Class Activerecord
Submitted by bingomanatee on 13 March, 2010 - 19:24
Methods:
This class represents a single row in the database. They should as a rule be generated via the table classes below. NOTE: the activerecord, being based on a single row from a single table, doesn't reflect joined data. The domain pattern is required for multi-row constructs.
ASSUMPTIONS: the following assumptions are made about ActiveRecord tables:
- They are SQL tables. No testing has been done in other databases.
- They have a primary key.
- The primary key is based on a single field. (but no assumption is made about type or autoincrement.
Properties:
Each field in the table has an equivalent field in an Activerecord, mapped to magic method. The only other public field is:
- status: string (either 'new' or 'saved').
Methods:
- __construct($table, stdClass $fields = NULL): $table can be a string (name of a table), or an Activerecord_table. $fields, if present, should have fields that match the structure of the base table (but the class will filter the properties into its own and use table defaults if necessary to flush out the field value list.)
- key(): scalar. The value of the key field of the record.
- key_field(): string. The name of the primary key.
- save(): triggers insert or update as needed to save data to the database.
- refresh($key = NULL): reloads data from the database into the record. any fields that have been changed (are 'dirty') will retain their value. If a parameter is passed, will refresh based on that record. This is largely a method for internal use.
- table($table = null): Activerecord_table. returns an Activerecord_table class for the record. If a string or Activerecord_table is passed, will initialize the table. (note: you can hypothetically change the table basis of the record at any time. Don't.)
- toArray(): array. returns a hashmap of the records' fields.
- delete(): deletes the record and resets the Activerecord object to that of a new record.

Post new comment