Packagecom.memamsa.airdb
Classpublic class Associator
InheritanceAssociator Inheritance flash.utils.Proxy

The Associator transparently maps schema relationships into object aggregations.

The Modeler automatically creates appropriate Associators using class meta-data. The Associator provides methods for creating and querying associations between table rows (including through join tables). Where necessary, the Associator maps method calls directly to the target model, on which additional methods can be invoked.

The Associator includes support for the following relationships:

Multiple Associations can be specified as class meta-data in the package, with the following format.

  [Association(name="aname", className="cname", type="atype")]  
  
  where:
     aname = the name by which the association is invoked (e.g. "comments")
     cname = FQN of the associated class, e.g. com.example.model.Comment
     atype = Association type, e.g. "has_many"
  
Requires compiler setting of -keep-as3-metadata+=Association


Example
A blog Post belongs-to author and has-many comments:
  package example.model {
    [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 {
   }
  }
  var post:Post = Modeler.findery(Post, {id: 3});
  trace(post.author.name);
  // Find comments since 2009-08-01
  post.comments.findAll({
     select: " strftime("%Y-%m-%d, created_at) as cd", 
     conditions: "cd > '2009-08-01'"
  });
  



Public Properties
 PropertyDefined by
  count : int
[read-only] Count the number of associated objects.
Associator
  sourceForeignKey : String
Associator
  target : *
Get the table name for the association target
Associator
  targetForeignKey : String
Associator
Public Methods
 MethodDefined by
  
Associator(source:Modeler, target:Class, type:String, options:Object = null)
Construct an associator to map between two models representing database tables.
Associator
  
countByAttr(keyvals:*, target:*):int
Count the number of associated targets matching specified criteria.
Associator
  
findAll(query:Object):Array
Query associated objects 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.
Associator
  
findAllByAttr(keyvals:Object):Array
Find all association targets with matching association attributes.
Associator
  
getAttrVal(name:String, target:*):*
Get the value for a given attribute for a specific target
Associator
  
list(params:Object = null):Array
List all target objects.
Associator
  
push(obj:*, noDups:Boolean = true):Boolean
Create an association between the source and the specified target object.
Associator
  
remove(obj:*):Boolean
Remove an association which the given object might have with this source.
Associator
  
setAttr(keyvals:Object, target:*):int
Set attributes on the association.
Associator
Protected Methods
 MethodDefined by
  
callProperty(name:*, ... args):*
Associator
  
getProperty(name:*):*
Associator
  
hasProperty(name:*):Boolean
Associator
  
setProperty(name:*, value:*):void
Associator
Public Constants
 ConstantDefined by
  ALL : int = 0
[static]
Associator
  BELONGS_TO : String = "belongs_to"
[static]
Associator
  HAS_AND_BELONGS_TO_MANY : String = "has_and_belongs_to_many"
[static]
Associator
  HAS_MANY : String = "has_many"
[static]
Associator
  HAS_MANY_THROUGH : String = "has_many_through"
[static]
Associator
  HAS_ONE : String = "has_one"
[static]
Associator
Property detail
countproperty
count:int  [read-only]

Count the number of associated objects.

Implementation
    public function get count():int
sourceForeignKeyproperty 
public var sourceForeignKey:String
targetproperty 
target:*  [read-write]

Get the table name for the association target

Implementation
    public function get target():*
    public function set target(value:*):void
targetForeignKeyproperty 
public var targetForeignKey:String
Constructor detail
Associator()constructor
public function Associator(source:Modeler, target:Class, type:String, options:Object = null)

Construct an associator to map between two models representing database tables. The source is the subject and the target is the object of the relationship. e.g. If a customers has-many orders, then Customer is the source and Order is the target.

Parameters
source:Modeler — A model (sub-class of Modeler) object which is the subject (source) of the assocation.
 
target:Class — A Modeler derived class as the target of the association.
 
type:String — The association type.
 
options:Object (default = null)
Method detail
callProperty()method
flash_proxy override function callProperty(name:*, ... args):*Parameters
name:*
 
... args

Returns
*
countByAttr()method 
public function countByAttr(keyvals:*, target:*):int

Count the number of associated targets matching specified criteria.

Parameters
keyvals:* — An Object whose key-value pairs specify column names and their field values in the join table.
 
target:*

Returns
int — An integer count.

See also

setAttr
findAllByAttr
Migrator.joinTable
findAll()method 
public function findAll(query:Object):Array

Query associated objects 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 ...

Returns
Array — List of Objects representing the query result.

See also

findAllByAttr()method 
public function findAllByAttr(keyvals:Object):Array

Find all association targets with matching association attributes. Associations of type has_and_belongs_to_many can have attributes as part of the mapping between the source and target models. Use this method to find all associated targets matching the specified association attributes.

Parameters
keyvals:Object — An Object whose keys map to the column names for the join table attributes, and whose values specify the conditions for the field values in the query.

Returns
Array

See also

setAttr
countAllByAttr
Migrator.joinTable
getAttrVal()method 
public function getAttrVal(name:String, target:*):*

Get the value for a given attribute for a specific target

Parameters
name:String — The attribute name
 
target:* — The Modeler object, or Integer id for a specific target. Implicit in the call is a particular source object, thus defining a particular association corresponding to a single row in the join table.

Returns
* — The value for the association attribute.

See also

setAttr
getProperty()method 
flash_proxy override function getProperty(name:*):*Parameters
name:*

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

Returns
Boolean
list()method 
public function list(params:Object = null):Array

List all target objects. Equivalent to findAll({})

Parameters
params:Object (default = null)

Returns
Array

See also

push()method 
public function push(obj:*, noDups:Boolean = true):Boolean

Create an association between the source and the specified target object. Saves the source and the target if either of them are new records.

Parameters
obj:* — The target object to be associated with this source model. This can be an object of a Modeler sub-class or an integer which is interpreted as the id field value for the target.
 
noDups:Boolean (default = true) — Specifies whether duplicate associations are disallowed, particularly for has_and_belongs_to_many associations.

Returns
Booleantrue if association was successfully made, otherwise false.

Example
Add another comment to a blog post.
  var post:Post = Modeler.findery(Post, {id: 1});
  // Push (and saves) new comment
  post.comments.push(new Comment({title: '1st', text: 'i wuz here'}));
  

remove()method 
public function remove(obj:*):Boolean

Remove an association which the given object might have with this source.

Parameters
obj:* — The target object(s) to be dis-associated from this source model. The parameter can be:
  • A single object instance of a Modeler sub-class
  • An Array of Modeler objects
  • An integer as the id field value for the target
  • An Array of target ids

Returns
Booleantrue if association was successfully made, otherwise false.
setAttr()method 
public function setAttr(keyvals:Object, target:*):int

Set attributes on the association. Associations of type has_and_belongs_to_many can have attributes as part of the mapping between the source and target models. Use setAttr to set the values for these attributes, either for a single target or for all of the many targets associated with the source.

Parameters
keyvals:Object — An Object whose keys map to the column names for the join table attributes. The corresponding values are used to update the fields for the matching join table record(s).
 
target:* — A Modeler object or an Integer rowID to match a specific join table row. This value is used to match the foreign key field corresponding to the association target. Implicit in the call to this method is the source, since the method was invoked via the source object for the association.

Returns
int — The number of join table rows updated.

See also

Migrator.joinTable
findAllByAttr
countAllByAttr

Example
A blog Post has many Tags and a Tag is associated with many Post A logged in reader (User) can vote on the tagggings for the post. We can track the vote counts for each tagging
 
  [Association type="has_and_belongs_to_many" name="tags" class="example.Tag"]
  dynamic public class Post extends Modeler {
    private static var migrations:Migrator = new Migrator(
     Post, {id: true}, 
     [
      function(my:Migrator):void {
       my.createTable(function():void {
        my.column('name', DB.Field.VarChar, {limit: 255});
        my.columnTimestamps();
       });
      }, 
      function(my:Migrator):void {
       var tagVotesCol:Array = ['votes', DB.Field.Integer, {
        'default': 1}
       ];     
       my.joinTable(Photo, [tagVotesCol]);
      },
     ]
    )
  }
  // Push as usual to create a new association and corresponding join row
  var post:Post = new Post();
  var flex:Tag = new Tag().find({name: 'flex'});
  post.tags.push(flex);
  
  // Update the votes for a given for some post
  numVotes = post.tags.getAttrVal('votes', flex.id);  
  post.tags.setAttr({votes: numVotes + 1}, flex.id);
  
  // Reset the votes for all tags for a given post
  post.tags.setAttr({votes: 1});
  

setProperty()method 
flash_proxy override function setProperty(name:*, value:*):voidParameters
name:*
 
value:*
Constant detail
ALLconstant
public static const ALL:int = 0
BELONGS_TOconstant 
public static const BELONGS_TO:String = "belongs_to"
HAS_AND_BELONGS_TO_MANYconstant 
public static const HAS_AND_BELONGS_TO_MANY:String = "has_and_belongs_to_many"
HAS_MANYconstant 
public static const HAS_MANY:String = "has_many"
HAS_MANY_THROUGHconstant 
public static const HAS_MANY_THROUGH:String = "has_many_through"
HAS_ONEconstant 
public static const HAS_ONE:String = "has_one"