Packagecom.memamsa.airdb
Classpublic class Modeler
InheritanceModeler Inheritance flash.utils.Proxy
SubclassesSchemaInfo

The 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.


Example
A model to represent a blog Post, each of which has many Comments
  [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

com.memamsa.airdb.Migrator
com.memamsa.airdb.Associator


Public Properties
 PropertyDefined 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
Protected Properties
 PropertyDefined by
  fieldValues : Object
Modeler
Public Methods
 MethodDefined by
  
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
  
findery(klass:Class, keyvals:Object):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
Protected Methods
 MethodDefined 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
  
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
Public Constants
 ConstantDefined by
  PER_PAGE_LIMIT : int = 50
[static]
Modeler
Property detail
classNameproperty
className:String  [read-only]

Returns the unqualified class name for the model.

Implementation
    public function get className():String
fieldValuesproperty 
protected var fieldValues:Object
newRecordproperty 
newRecord:Boolean  [read-only]

Check if this object fields represent a new record

Implementation
    public function get newRecord():Boolean
unsavedproperty 
unsaved:Boolean  [read-only]

Check if object has unsaved changes

Implementation
    public function get unsaved():Boolean
Constructor detail
Modeler()constructor
public function Modeler()
Method detail
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

Returns
*
count()method 
public function count():int

Count the total number of records in the table for this model.

Returns
int — The total record count

See also

countAll()method 
public function countAll(query:Object):int

Count the number of records matching specified query criteria

Parameters
query:Object — An Object whose keys can map to values corresponding to the following supported SQL clauses:
  • conditions: SQL conditions including AND, OR, etc.
  • group: field names that follow a GROUP BY
  • order: describe sorting as in ORDER BY, e.g. "name ASC"
  • limit: LIMIT clause, e.g. 10
  • joins: table join claues, e.g. inner join table on ...

Returns
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.

Parameters
values: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.

Returns
Booleantrue if new record was successfully created, false otherwise.

Example
Create the record for a new blog post
    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.

Parameters
values: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.

Parameters
keyvals:Object — The conditions, expressed in terms of keys representing field names and their associated values to be matched.

Returns
Booleantrue 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.

Parameters
query:Object — An Object whose keys can map to values corresponding to the following supported SQL clauses:
  • select: fields and sub-selects, e.g. field as something, etc.
  • conditions: SQL conditions including AND, OR, etc.
  • group: field names that follow a GROUP BY
  • order: describe sorting as in ORDER BY, e.g. "name ASC"
  • limit: LIMIT clause, e.g. 10
  • joins: table join claues, e.g. inner join table on ...
 
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

Returns
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.

Parameters
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.

Returns
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.

Example
Find blog Posts by a particular author
  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

Parameters
query:Object
 
page:int (default = 1)
 
perPage:int (default = 0)

Returns
Array
getProperty()method 
flash_proxy override function getProperty(name:*):*

Parameters
name:*

Returns
*
hasProperty()method 
flash_proxy override function hasProperty(name:*):Boolean

Parameters
name:*

Returns
Boolean
load()method 
public function load(keyvals:Object):Boolean

Load this instance with record information matching specified conditions.

Parameters
keyvals:Object — The conditions, expressed in terms of keys representing field names and their associated values to be matched.

Returns
Booleantrue 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.

Returns
Booleantrue 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.

Returns
Booleantrue 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.

Returns
Booleantrue 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):voidParameters
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.

Parameters
values: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.

Returns
Booleantrue if record was successfully updated, false otherwise.

See also


Example
Update a person's karma score
  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

Parameters
conditions: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.

Returns
uint — A count of the number of rows that were updated

See also


Example
Add keyword string to all posts about SQLite
  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.

Returns
Boolean — Upon false result, the triggering save or create is aborted. Return true to allow processing of valid data.

See also

Constant detail
PER_PAGE_LIMITconstant
public static const PER_PAGE_LIMIT:int = 50