electronic.alchemy :: Model Reflection
where the past meets the future
pike > Fins > Developer > Model Reflection

Model Reflection

Created by hww3. Last updated by hww3, 16 years ago. Version #2.

Fins can automatically generate model objects by looking at the schema of a database table. For simple data types, this can result in a significant time savings. In order for this feature to function properly, it's important to understand how Fins generates the data definitions. This document describes some of the rules Fins will follow.

1. For a given data definition class, Fins will look in the configured database for a table matching the pluralization of the name of the class. For example, if you create a child of Fins.Model.DataObject called "PhoneNumber" and register it, Fins will look for a table in the database called "phonenumbers" to associate the data type with.

2. For each field within the associated table, Fins will attempt to create a corresponding field in the data type definition. The field in the data type definition will carry the same name as in the database table, with the exception of reference fields, which will be described in section 4.

3. If a field is made an integer auto-incrementing primary key, that field will be used to uniquely identify that field. Typically, this field is called "id", but the auto-configuration process is smart enough to work with alternative names.

4. If a field exists in an associated table that takes the form of otherdatatype_otherkey, a one to one relationship will be created linking the current object with the other object. For example, there are two tables called "documents" and "authors", both of which have primary keys called "id", and with corresponding object types of Document and Author, respectively. If the "documents" table contains a field called "author_id", the auto-configuration process will add a field to Document called Author, which when accessed, will retrieve the Author object whose unique id is provided in the "author_id" field.

At the same time, Fins will also add a reverse reference to the other object so that you can perform Author["Documents"] and get a list of all Document objects whose author_id field is set to your Author's unique id.

5. If your database contains tables that follow the format somedatatypes_otherdatatypes (note that each component is the pluralized version of the table's name), Fins will automatically link the two constituent data types in a many-to-many relationship. For example, in our Author and Document example, if we created a table called authors_documents, a multiple key reference would be created. Note that this "connenctor" table should have exactly two fields: one representing the id of the first datatype, and one representing the id of the other. The field names should be called "datatype_keyfieldname", so continuing our example, the two fields should be called "author_id" and "document_id".

Note that our use of plural and singular noun forms helps to provide context in the ways in which many and single to many and single relationships relate (and in which you may or may not expect to get single data objects or an array of data objects).

Not categorized | RSS Feed | BackLinks

comments powered by Disqus