[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Model Creation

Models can be created in 3 ways

  1. Manual written with property lists.
  2. Hard coding the model in objective-c.
  3. Creation of plists with the DBModeler application.

while DBModeler provides the easiest way, followed by manually writing the property lists, and hard coding the model is both tedious and complicated.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.0.1 Example model file

Below is a example property list model created with DBModeler, it contains a Model for a library 2 entities, author and book

author contains 2 attributes, authorID the primary key number, and name a string book contains 3 attributes, bookID the primary key number, authorID a foreign key number, and title a string.

author and book each contain a relationship author a to-many relationship to each of the authors books, and book a to-one relationship to the books author for the sake of demonstration i’m ignoring books with multiple authors.

it also contains an adaptor name, and an adaptor specific connection dictionary.

{
    EOModelVersion = 2;
    adaptorName = SQLite3;
    connectionDictionary = {
	databasePath = "/tmp/example.db";
    };
    entities = (
	{
	    attributes = (
		{
		    columnName = authorID;
		    externalType = integer;
		    name = authorID;
		    valueClassName = NSNumber;
		},
		{
		    columnName = name;
		    externalType = varchar;
		    name = name;
		    valueClassName = NSString;
		}
	    );
	    className = EOGenericRecord;
	    classProperties = (
		name,
		toBooks
	    );
	    externalName = authors;
	    name = authors;
	    primaryKeyAttributes = (
		authorID
	    );
	    relationships = (
		{
		    destination = books;
		    isToMany = Y;
		    joinSemantic = EOInnerJoin;
		    joins = (
			{
			    destinationAttribute = authorID;
			    sourceAttribute = authorID;
			}
		    );
		    name = toBooks;
		}
	    );
	},
	{
	    attributes = (
		{
		    columnName = authorID;
		    externalType = integer;
		    name = authorID;
		    valueClassName = NSNumber;
		},
		{
		    columnName = bookID;
		    externalType = integer;
		    name = bookID;
		    valueClassName = NSNumber;
		},
		{
		    columnName = title;
		    externalType = varchar;
		    name = title;
		    valueClassName = NSString;
		}
	    );
	    className = EOGenericRecord;
	    classProperties = (
		title,
		toAuthor
	    );
	    externalName = books;
	    name = books;
	    primaryKeyAttributes = (
		bookID
	    );
	    relationships = (
		{
		    destination = authors;
		    isToMany = N;
		    joinSemantic = EOInnerJoin;
		    joins = (
			{
			    destinationAttribute = authorID;
			    sourceAttribute = authorID;
			}
		    );
		    name = toAuthor;
		}
	    );
	}
    );
    name = library;
}

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.0.2 Creating with DBModeler

To recreate the example model with DBModeler,

select Document, New from the main menu then property Add entity twice. set the name and external names to ’authors’ and ’books’

select Document, Set Adaptor Info, and select SQLite, and click Ok, this will bring up the SQLite login panel, where you need to provide it a path for where to create the model file.

Each Adaptor will have different requirements, so each login panel is quite different. Other adaptors may have a server address, username, and database names.

select the authors entity in the outline view, after expanding the model add an attribute to authors by selecting Property, Add attribute set the name and column name to ’authorID’, and select the switch button with a key icon, to set it as the primary key for the entity. Set the value class name to NSNumber and the external type to INTEGER

Add another entity, set the name and column names to ’name’. Select the switch button which looks like a jewel icon to set it as a Class Property. Set the Value Class Name to NSString and external type to TEXT.

now do the same with books, name them bookID, authorID, and title. make sure bookID is set as the primary key not authorID in the books entity. And that title is set as a class property.

title is a NSString/TEXT, where authorID and bookID are NSNumber/INTEGER

now add a relationship to authors name it toBooks, and Tools, inspector in the destination table, select To many, and books as the destination entity.

select authorID as the source and destination attributes

add a relationship to books, name it toAuthor. Select author as the destination entity, and authorID as the source and destination attributes.

The select Document, Save, from the main menu.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on January 15, 2016 using texi2html 1.82.