Packagecom.memamsa.airdb
Classpublic class Relater

The Relater provides a declarative mechanism to specify associations between Modeler objects.

This method is an alternative (and recommended) way. Its better than using class meta-data, since it does not require any external compiler setting, allows compile-time checking of association code, provides a richer set of options, including the ability to explicitly specify foreign key column names.

The Associator still forms the basis for accessing and operating on associations. The Relater simply provides a declarative mechanism. In addition to the previously allowed associations the Relater allows a has_many_through type, specified as a has_many with additional options.

Options supported (depending on association):


Example
The blog Post belongs-to Author and has-many comments:
  package example.model {
    dynamic class Post extends Modeler {
      private static const relations:Relater = new Relater(Post, 
        function(me:Relater):void {
          me.belongsTo('author', {class_name: 'example.model.Person'});
          me.hasMany('comments', {class_name: 'example.model.Comment'});
       });
    }
  }
  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'"
  });
  

See also

Associator
Modeler


Public Methods
 MethodDefined by
  
Relater(klass:Class, directive:Function)
Construct a Relater for a given model.
Relater
  
belongsTo(name:String, options:Object = null):void
Specify belongs_to relationship.
Relater
  
hasAndBelongsToMany(name:String, options:Object = null):void
Specify has_and_belongs_to_many relationship.
Relater
  
hasMany(name:String, options:Object = null):void
Specify has_many relationship.
Relater
  
hasOne(name:String, options:Object = null):void
Specify a has_one relationship The foreign_key if specified is the column name within this model schema to access the target model.
Relater
Constructor detail
Relater()constructor
public function Relater(klass:Class, directive:Function)

Construct a Relater for a given model.

Parameters
klass:Class — The Modeler sub-class whose associations to relate.
 
directive:Function — A Function taking in the Relater as a parameter and returning void. Use association methods on the Relater object to specify relationships.
      function(me:Relater):void {
      me.hasMany('things', {class_name: 'some.package.ThingClass'});
      }
    

See also

Method detail
belongsTo()method
public function belongsTo(name:String, options:Object = null):void

Specify belongs_to relationship.

Parameters
name:String — The name of the association, which becomes available as a property on the Modeler class objects.
 
options:Object (default = null) — An Object hash of options. Recognized options:
  • class_name: Name of target association class
  • foreign_key: Foreign key in the source table which corresponds to relationship with the target model
If foreign_key is not specified, it is deduced from model name.

See also

hasAndBelongsToMany()method 
public function hasAndBelongsToMany(name:String, options:Object = null):void

Specify has_and_belongs_to_many relationship.

Parameters
name:String — The name of the association, which becomes available as a property on the Modeler class objects.
 
options:Object (default = null) — An Object hash of options. Recognized options:
  • class_name: Name of target association class
  • foreign_key: Foreign key for this model in the join table (whose name is automatically deduced)
If foreign_key is not specified, it is deduced from model name.

See also

hasMany()method 
public function hasMany(name:String, options:Object = null):void

Specify has_many relationship.

Parameters
name:String — The name of the association, which becomes available as a property on the Modeler class objects.
 
options:Object (default = null) — An Object hash of options. Recognized options:
  • class_name: Name of target association class
  • foreign_key: Foreign key in the target table which corresponds to relationship with the source model
  • through: The intermediate association (or join table) through which this model has_many relationship with a third model.
class_name is required. There must be a Associator specified corresponding to the through property. This implies the join-table is managed via a Modeler class. If foreign_key options are not specified, they are deduced.

See also


Example
A Person has_many friendships through Relationship. This illustrates a self-referential directed many-many association. The Relationship schema has from_id, to_id and other attribute fields.
   package example.model {
     dynamic class Person extends Modeler {
       private static const relations:Relater = new Relater(Post, 
         function(me:Relater):void {
           me.hasMany('friendships', {foreign_key: 'from_id', 
                class_name: 'example.model.Relationship'});
           me.hasMany('friends', {through: 'friendships', 
               foreign_key: 'to_id', class_name: 'example.model.Person'});
        });
     }
   }
   

hasOne()method 
public function hasOne(name:String, options:Object = null):void

Specify a has_one relationship The foreign_key if specified is the column name within this model schema to access the target model.

Parameters
name:String
 
options:Object (default = null)