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:
- has_many: Maps a foreign key for this table into the
belongs_to counter-part.
- has_and_belongs_to_many: Creates a join table
mapping the corresponding foreign keys
- belongs_to Specifies the foreign key in this table
for mapping the association
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'"
});
count:int
[read-only]
Count the number of associated objects.
Implementation
public function get count():int
public var sourceForeignKey:String
target:*
[read-write]
Get the table name for the association target
Implementation
public function get target():*
public function set target(value:*):void
public var targetForeignKey:String
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 )
|
flash_proxy override function callProperty(name:*, ... args):*
Parameters
Returns
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
See also
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
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
See also
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
flash_proxy override function getProperty(name:*):*
Parameters
Returns
flash_proxy override function hasProperty(name:*):Boolean
Parameters
Returns
public function list(params:Object = null):Array
List all target objects. Equivalent to findAll({})
Parameters
| params:Object (default = null )
|
Returns
See also
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
| Boolean — true 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'}));
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
| Boolean — true if association was successfully made, otherwise
false .
|
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
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});
flash_proxy override function setProperty(name:*, value:*):void
Parameters
public static const ALL:int = 0
public static const BELONGS_TO:String = "belongs_to"
public static const HAS_AND_BELONGS_TO_MANY:String = "has_and_belongs_to_many"
public static const HAS_MANY:String = "has_many"
public static const HAS_MANY_THROUGH:String = "has_many_through"
public static const HAS_ONE:String = "has_one"