| Package | com.memamsa.airdb |
| Class | public class Associator |
| Inheritance | Associator flash.utils.Proxy |
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
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'"
});
| Property | Defined by | ||
|---|---|---|---|
| count : int [read-only]
Count the number of associated objects.
| Associator | ||
| target : *
Get the table name for the association target
| Associator | ||
| Method | Defined by | ||
|---|---|---|---|
|
Associator(source:Modeler, target:Class, type:String)
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 | ||
| Method | Defined by | ||
|---|---|---|---|
|
callProperty(name:*, ... args):*
| Associator | ||
|
getProperty(name:*):*
| Associator | ||
|
hasProperty(name:*):Boolean
| Associator | ||
|
setProperty(name:*, value:*):void
| Associator | ||
| Constant | Defined 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_ONE : String = "has_one" [static]
| Associator | ||
| count | property |
count:int [read-only]Count the number of associated objects.
Implementation public function get count():int
| target | property |
target:* [read-write]Get the table name for the association target
Implementation public function get target():*
public function set target(value:*):void
| Associator | () | constructor |
public function Associator(source:Modeler, target:Class, type:String)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.
Parameterssource: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.
|
| callProperty | () | method |
flash_proxy override function callProperty(name:*, ... args):*Parameters
name:* |
|
... args |
* |
| countByAttr | () | method |
public function countByAttr(keyvals:*, target:*):intCount the number of associated targets matching specified criteria.
Parameterskeyvals:* — An Object whose key-value pairs specify column names
and their field values in the join table.
|
|
target:* |
int — An integer count.
|
See also
| findAll | () | method |
public function findAll(query:Object):ArrayQuery 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.
Parametersquery:Object — An Object whose keys can map to values corresponding
to the following supported SQL clauses:
|
Array — List of Objects representing the query result.
|
See also
| findAllByAttr | () | method |
public function findAllByAttr(keyvals:Object):ArrayFind 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.
Parameterskeyvals: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.
|
Array |
See also
| getAttrVal | () | method |
public function getAttrVal(name:String, target:*):*Get the value for a given attribute for a specific target
Parametersname: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.
|
* — The value for the association attribute.
|
See also
| getProperty | () | method |
flash_proxy override function getProperty(name:*):*Parameters
name:* |
* |
| hasProperty | () | method |
flash_proxy override function hasProperty(name:*):BooleanParameters
name:* |
Boolean |
| list | () | method |
public function list(params:Object = null):ArrayList all target objects. Equivalent to findAll({})
Parametersparams:Object (default = null) |
Array |
See also
| push | () | method |
public function push(obj:*, noDups:Boolean = true):BooleanCreate an association between the source and the specified target object. Saves the source and the target if either of them are new records.
Parametersobj:* — 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.
|
Boolean — true if association was successfully made, otherwise
false.
|
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:*):BooleanRemove an association which the given object might have with this source.
Parametersobj:* — The target object(s) to be dis-associated from this source model.
The parameter can be:
|
Boolean — true if association was successfully made, otherwise
false.
|
| setAttr | () | method |
public function setAttr(keyvals:Object, target:*):intSet 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.
Parameterskeyvals: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.
|
int — The number of join table rows updated.
|
See also
[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:* |
| ALL | constant |
public static const ALL:int = 0
| BELONGS_TO | constant |
public static const BELONGS_TO:String = "belongs_to"
| HAS_AND_BELONGS_TO_MANY | constant |
public static const HAS_AND_BELONGS_TO_MANY:String = "has_and_belongs_to_many"
| HAS_MANY | constant |
public static const HAS_MANY:String = "has_many"
| HAS_ONE | constant |
public static const HAS_ONE:String = "has_one"