Package | com.memamsa.airdb |
Class | public class Modeler |
Inheritance | Modeler flash.utils.Proxy |
Subclasses | SchemaInfo |
Modeler
provides base class functionality for Object
Relational Modeling of database tables, fields and operations. Sub-classes
of Modeler
(models) are automatically mapped to tables, and
include support for data manipulation and query methods such as load,
create, update, delete, save and findAll.
The actual schema for the model table is specified within the Modeler
sub-class, using a composite member instance of the Migrator
class. This schema information is used for checking field names and
optimizing certain operations.
The model can also define associations with other models. Associations help map table relationships in the database schema (such as many-many, one-many, etc) in terms of model objects and provide a quick and in some cases automated mechanism for querying and finding the associated table records.
[Association(type="has_many", name="comments", className="example.model.Comment")] [Association(type="belongs_to", name="author", className="example.model.Person")] dynamic class Post extends Modeler { private static var schema_migrations:Migrator = new Migrator( Post, {id: true}, [ function(my:Migrator):void { my.belongsTo(Person); my.createTable(function():void { my.column('title', DB.Field.VarChar, {limit: 255}); my.columnTimeStamps(); }); } ] ); // other class members and methods }
See also
Property | Defined by | ||
---|---|---|---|
className : String [read-only]
Returns the unqualified class name for the model.
| Modeler | ||
newRecord : Boolean [read-only]
Check if this object fields represent a new record
| Modeler | ||
unsaved : Boolean [read-only]
Check if object has unsaved changes
| Modeler |
Property | Defined by | ||
---|---|---|---|
fieldValues : Object | Modeler |
Method | Defined by | ||
---|---|---|---|
Modeler()
| Modeler | ||
count():int
Count the total number of records in the table for this model.
| Modeler | ||
countAll(query:Object):int
Count the number of records matching specified query criteria
| Modeler | ||
create(values:Object = null):Boolean
Create a new record by combining field values in this instance with
the specified values.
| Modeler | ||
data(values:Object):void
Initialize this instance with specified field values, potentially for a
new record.
| Modeler | ||
find(keyvals:Object):Boolean
Check for the existence of records matching criteria specified in terms
of field names and associated values.
| Modeler | ||
findAll(query:Object, page:int = 1, perPage:int = 0):Array
A generic sql-based find method to query and load field information for
all records based on SQL conditions, ordering and limits including join
and grouping operations.
| Modeler | ||
[static]
Create an instance of the specified
Modeler sub-class and
load it with the record information matching given key values. | Modeler | ||
getAll(query:Object, page:int = 1, perPage:int = 0):Array
A front-end to findAll method
Returns objects of the appropriate Model type
| Modeler | ||
load(keyvals:Object):Boolean
Load this instance with record information matching specified conditions.
| Modeler | ||
reload():Boolean
Reload this previously loaded instance to obtain latest field values.
| Modeler | ||
remove():Boolean
Deletes the record represented by this object.
| Modeler | ||
save():Boolean
Save the field values in this instance to the database record.
| Modeler | ||
update(values:Object = null):Boolean
Update the database record currently loaded into the model object.
| Modeler | ||
updateAll(conditions:String, values:Object):uint
Update all records matching specified conditions with new values
| Modeler |
Method | Defined by | ||
---|---|---|---|
beforeCreate():void
Overridable method called before inserting new records.
| Modeler | ||
beforeSave():void
Overridable method called before saving a newly created or updated reord.
| Modeler | ||
beforeUpdate():void
Overridable method called before update of existing records.
| Modeler | ||
callProperty(name:*, ... args):*
| Modeler | ||
getProperty(name:*):*
| Modeler | ||
hasProperty(name:*):Boolean
| Modeler | ||
resetAssociations():void
| Modeler | ||
resetFields():void
| Modeler | ||
resetState():void
| Modeler | ||
setProperty(name:*, value:*):void
| Modeler | ||
timeStamp(field:String, values:Object = null):void
| Modeler | ||
validateData():Boolean
Overridable method called before save or create to allow validation of
field data.
| Modeler |
Constant | Defined by | ||
---|---|---|---|
PER_PAGE_LIMIT : int = 50 [static]
| Modeler |
className | property |
className:String
[read-only]Returns the unqualified class name for the model.
Implementation public function get className():String
fieldValues | property |
protected var fieldValues:Object
newRecord | property |
newRecord:Boolean
[read-only]Check if this object fields represent a new record
Implementation public function get newRecord():Boolean
unsaved | property |
unsaved:Boolean
[read-only]Check if object has unsaved changes
Implementation public function get unsaved():Boolean
Modeler | () | constructor |
public function Modeler()
beforeCreate | () | method |
protected function beforeCreate():void
Overridable method called before inserting new records.
See also
beforeSave | () | method |
protected function beforeSave():void
Overridable method called before saving a newly created or updated reord.
See also
beforeUpdate | () | method |
protected function beforeUpdate():void
Overridable method called before update of existing records. Invoked
after the beforeSave()
callback.
See also
callProperty | () | method |
flash_proxy override function callProperty(name:*, ... args):*
Parameters
name:* |
|
... args |
* |
count | () | method |
public function count():int
Count the total number of records in the table for this model.
Returnsint — The total record count
|
See also
countAll | () | method |
public function countAll(query:Object):int
Count the number of records matching specified query criteria
Parametersquery:Object — An Object whose keys can map to values corresponding
to the following supported SQL clauses:
|
int — The count of records matching the query
|
See also
create | () | method |
public function create(values:Object = null):Boolean
Create a new record by combining field values in this instance with the specified values.
Parametersvalues:Object (default = null ) — An object containing field names as keys and corresponding
field values. These given values override the field values previously
stored in the instance.
|
Boolean — true if new record was successfully created,
false otherwise.
|
var post:Post = new Post(); post.title = "A new beginning"; post.author_id = 3; post.create(); // Create another post by the same author post.create({title: "A continuing saga"});
data | () | method |
public function data(values:Object):void
Initialize this instance with specified field values, potentially for a new record. Any existing field information in this instance is reset.
Parametersvalues:Object — An Object where the keys correspond to field names and
the values to be assigned to them.
|
find | () | method |
public function find(keyvals:Object):Boolean
Check for the existence of records matching criteria specified in terms of field names and associated values. This method will not change the current object state or field values.
Parameterskeyvals:Object — The conditions, expressed in terms of keys representing
field names and their associated values to be matched.
|
Boolean — true if matching records were found, otherwise
false .
|
See also
findAll | () | method |
public function findAll(query:Object, page:int = 1, perPage:int = 0):Array
A generic sql-based find method to query and load field information for all records based on SQL conditions, ordering and limits including join and grouping operations.
Parametersquery:Object — An Object whose keys can map to values corresponding
to the following supported SQL clauses:
|
|
page:int (default = 1 ) — Specify record offset in terms of integer valued pages.
|
|
perPage:int (default = 0 ) — The number of records to fetch per page
|
Array — List of Objects representing the query result.
|
findery | () | method |
public static function findery(klass:Class, keyvals:Object):Modeler
Create an instance of the specified Modeler
sub-class and
load it with the record information matching given key values.
klass:Class — The classname, which should be a sub class of
Modeler
|
|
keyvals:Object — An Object where the keys correspond to field names and
values specify the condition to be matched against that field.
|
Modeler —
If there exists a record matching the conditions specified in
the keyvals , returns an instance of the specified
Modeler sub-class loaded with the record information.
Otherwise, returns null.
|
var author:Person = Modeler.findery(Person, {name: 'Cool Dude'}); var post:Post = Modeler.findery(Post, {author_id: author.id});
getAll | () | method |
public function getAll(query:Object, page:int = 1, perPage:int = 0):Array
A front-end to findAll method Returns objects of the appropriate Model type
Parametersquery:Object |
|
page:int (default = 1 )
|
|
perPage:int (default = 0 )
|
Array |
getProperty | () | method |
flash_proxy override function getProperty(name:*):*
Parameters
name:* |
* |
hasProperty | () | method |
flash_proxy override function hasProperty(name:*):Boolean
Parameters
name:* |
Boolean |
load | () | method |
public function load(keyvals:Object):Boolean
Load this instance with record information matching specified conditions.
Parameterskeyvals:Object — The conditions, expressed in terms of keys representing
field names and their associated values to be matched.
|
Boolean — true if a matching record was found, otherwise
false .
|
reload | () | method |
public function reload():Boolean
Reload this previously loaded instance to obtain latest field values. All unsaved changes are lost.
ReturnsBoolean — true if reload was successful, false
otherwise.
|
remove | () | method |
public function remove():Boolean
Deletes the record represented by this object. The model object must have previously been "loaded" with a record.
ReturnsBoolean — true if record was successfully deleted,
false otherwise.
|
resetAssociations | () | method |
protected function resetAssociations():void
resetFields | () | method |
protected function resetFields():void
resetState | () | method |
protected function resetState():void
save | () | method |
public function save():Boolean
Save the field values in this instance to the database record. Creates a new record or updates an existing record appropriately based on how this instance was loaded or initialized.
ReturnsBoolean — true on successful save, false otherwise.
|
setProperty | () | method |
flash_proxy override function setProperty(name:*, value:*):void
Parameters
name:* |
|
value:* |
timeStamp | () | method |
protected function timeStamp(field:String, values:Object = null):void
Parameters
field:String |
|
values:Object (default = null )
|
update | () | method |
public function update(values:Object = null):Boolean
Update the database record currently loaded into the model object. Only updates changed fields or those for which new values are provided.
Parametersvalues:Object (default = null ) — An object containing field names as keys and corresponding
field values. These given values override the field values previously
stored in the instance.
|
Boolean — true if record was successfully updated,
false otherwise.
|
See also
var user:Person = Modeler.findery(Person, {id: 7}); user.update({karma: user.karma + 20});
updateAll | () | method |
public function updateAll(conditions:String, values:Object):uint
Update all records matching specified conditions with new values
Parametersconditions:String — A set of conditions in valid SQL syntax
|
|
values:Object — An Object with keys representing field names and the
associated values as updated values.
|
uint — A count of the number of rows that were updated
|
See also
var post:Post = new Post(); post.updateAll("title like '%sql%'", {keywords: "sql,database"});
validateData | () | method |
protected function validateData():Boolean
Overridable method called before save or create to allow validation of field data. Set the return value to control whether to abort or proceed with the save or create operation.
ReturnsBoolean — Upon false result, the triggering save or create is
aborted. Return true to allow processing of valid data.
|
See also
PER_PAGE_LIMIT | constant |
public static const PER_PAGE_LIMIT:int = 50