News Feed Comments

Releasable Revolution

Creating release quality applications with Revolution

  • Home
  • Tutorials & Manuals
  • Articles
  • Revolution Tools
    • Libraries
      • SQL Yoga
      • GLX Application Framework
      • libDatabase
    • Externals
      • EQT
    • Plugins
      • Script Format Helper
      • BBEdit Language Module
  • About
Tutorials & Manuals » SQL Yoga To-Do Application » Tell SQL Yoga About Table Relationships

Subscribe to our feed

Topics

  • Introduction 3
    • Introduction: What We Are Going to Create
    • Before You Get Started
    • The To-Do Application Database Schema
  • Getting Started 5
    • Chapter Overview
    • Open Blank Application Template
    • Unlocking the SQL Yoga Library
    • Creating A SQL Yoga Database Object
    • Initializing SQL Yoga At Launch
  • Creating Records In and Displaying Records From A Database 6
    • Chapter Overview
    • Creating a Project
    • Display Projects in the UI
    • Creating a To-Do Item
    • Display To-Do Items in the UI
    • Creating A Person And Updating Left Column Display
  • More With Displaying Database Records 3
    • Chapter Overview
    • Tell SQL Yoga About Table Relationships
    • Update the UI Code to Account For People
  • Linking and Deleting Records 4
    • Chapter Overview
    • Associating a Person With a To-Do Item
    • Removing a Person Associated With a To-Do Item
    • Deleting Projects, People and To-Do Items
  • Updating Database Records 3
    • Chapter Overview
    • Editing a Project, Person or To-Do Name
    • Updating a To-Do Items Completed State
  • Adding Search Functionality 3
    • Chapter Overview
    • Defining Scopes To Make Aggregating Search Filters Easier
    • Search for To-Dos Using Scopes
  • Conclusion 1
    • Test The Standalone Version

SQL Yoga: A Flexible Database Library for Revolution.

SQL Yoga page

Buy now

Last Updated

Oct 16, 2009

Download Lesson PDF

Download Manual PDF

Other Resources

  • How-tos

  • Lessons
  • GLX Application Framework

  • GLX Application Framework 1.1
  • Converting An Existing Application To Use The Framework
  • SQL Yoga

  • SQL Yoga
  • SQL Yoga Examples
  • SQL Yoga To-Do Application
  • SQL Yoga IDE Plugin
  • Plugins, Externals and Libraries

  • Plugins

Comments

0 comments for this lesson

  • Prev: Chapter Overview
  • Next: Update the UI Code to Account For People

Tell SQL Yoga About Table Relationships

So far we have created records in and displayed records from a database with very little code. SQL Yoga allowed you to create objects that you set properties on rather than writing out SQL queries by hand.

Now we are going to extend the basic functionality that SQL Yoga provides by defining a table objects behavior for the Database object where we can define special objects that provide extra functionality. We will start by looking at Table and Relationship objects because SQL Yoga will make working with people and to-do items much easier if we tell it about the relationship.

Let's look at how to create and define Table and Relationship objects.

Media_1253894783873

Create The 'table objects behavior'

Media_1253741073571

In Revolution a behavior script extends the functionality of the object that it is attached to. SQL Yoga allows you to set a behavior for a Database object so that you can enable additional features.

Revolution stores a behavior script in a button control so open the application stack again and add a new button to it. Name the button SQL Yoga Table Objects Behavior.

Set the 'table objects behavior' Property of the Database Object

Media_1253741161582

Next, set the table objects behavior property of the Database object to the long id of the SQL Yoga Table Objects Behavior button and save the Database object. The 'table objects behavior' property tells SQL Yoga which button contains the behavior script that will extend the functionality of the Database object.

Edit Behavior Script

Media_1253741189503

Now that you have assigned the table objects behavior of the Database object you can begin to write the behavior script. Edit the script of the SQL Yoga Table Objects Behavior script.

Set Behavior Script

Paste the following RevTalk code into the SQL Yoga Table Objects Behavior button script and compile. If there is a mouseUp handler in the script make sure and delete it. After you insert the code we will go through the relevant parts.

----------
Copy & Paste The Following Code
----------

/**
* You can trigger this message for the database object by
* calling tableobjects_reload. Anytime you want to change the defined
* objects for the Database object modify this handler and call tableobjects_reload.
* You should then call dbobject_save.
*/
on dbobject.createTables
    ## Create Table Objects in order to define
    ## relationships
    tableobj_createObject "projects"
    tableobj_createObject "todo_items"
    tableobj_createObject "people"
    
    ## Now that table objects exist create relationships
    _CreateRelationships
end dbobject.createTables

 

private command _CreateRelationships
    ----------
    ## Define relationship between projects and to-do items
    tblrelation_createObject "projects to todo items"
    tblrelation_set "projects to todo items", "type", "one-to-many"
    
    tblrelation_set "projects to todo items", "left table", "projects"
    tblrelation_set "projects to todo items", "left table key", "id"
    
    tblrelation_set "projects to todo items", "right table", "todo_items"
    tblrelation_set "projects to todo items", "right table key", "project_id"
    
    tblrelation_set "projects to todo items", "order by", "todo_items.sequence"
    ----------
    
    ----------
    ## Define relationship between people and to-do items
    tblrelation_createObject "people to todo items"
    tblrelation_set "people to todo items", "type", "many-to-many"
    
    tblrelation_set "people to todo items", "left table", "people"
    tblrelation_set "people to todo items", "left table key", "id"
    
    tblrelation_set "people to todo items", "cross-reference table", "people_todo"
    tblrelation_set "people to todo items", "cross-reference table key for left table", "people_id"
    tblrelation_set "people to todo items", "cross-reference table key for right table", "todo_id"
    
    tblrelation_set "people to todo items", "right table", "todo_items"
    tblrelation_set "people to todo items", "right table key", "id"
    
    tblrelation_set "people to todo items", "order by", "todo_items.name"
    ----------
end _CreateRelationships

Defining the dbobject.createTables Message For a Database Object

Media_1253825864310

With SQL Yoga you define all of the objects that extend functionality of your Database object in a special message named dbobject.createTables. Within this message you create the Table, Relationship, Scope and SQL Query Templates that your application will use.

Create Table Objects

Media_1253825921332

Since we are going to define relationships between various tables we need to create what are called Table objects. A Table object represents a table in your database and allows you to extend the basic properties of that table.

For example, you can define additional properties for a table using a Table object. Say you have a table with first_name and last_name fields. You could define a 'name' property for the table object that returned a concatenation of those two fields.

Or perhaps you have a table with a description field that can contain a lot of text. You could define a 'short description' property that returned a shortened version of the description.

Right now we are just going to extend the projects, todo_items and peoples tables by defining the relationships between them. To do this you create a Table object for each table by calling tableobj_createObject. This command will add an object to the Database object.

Create Relationship Objects

Media_1253825957172

A Relationship object provides SQL Yoga the information it needs to dynamically generate SQL queries based off of the relationships. For example, the SQL Yoga command sqlrecord_link can automatically link records in two tables for you. sqlrecord_getRelated will automatically retrieve records from a table related to a SQL Record object's table. SQL Query objects use relationships to automatically generate JOIN queries if needed. It's all automated and it's all available to you by defining Relationship objects.

Defining a Relationship only requires that you tell SQL Yoga the type of relationship that exists between the two tables as well as which fields in each table are used to link the two tables together. As you can see in this code you can also specify which field is used to sort the related records that are returned (1).

Trigger the dbobject.createTables.Default Message

Media_1253759915644

Now that we have finished writing the dbobject.createTables.Default message we need to trigger it. Whenever you make an update to this handler (i.e. you add/remove a Table or other object) you can call tableobjects_reload. This deletes any existing objects in your Database object and calls the handler.

Confirm Your Work

Media_1255723637779

If you want to confirm that dbobject.createTables was called and did what it was supposed to then you can print out the array representation of the Database object in the message box. Do this by executing:

get dbobject_getArray()
put printkeys(it)

If you search through the printout you should see an entry for relationships along with the relationships you just created. For example, projects to todo items.

  • Prev: Chapter Overview
  • Next: Update the UI Code to Account For People

Comments (0)

Add your comment

E-Mail me when someone replies to this comment
  • ScreenSteps Desktop

    ScreenSteps Desktop helps you help other people.

    Teachers can teach, Bloggers can demonstrate, Tech Support can communicate, Developers can document, Trainers can instruct.

    All in a matter of minutes.

    Learn More

  • ScreenSteps Support Suite

    We're Revolutionizing the Way Organizations Help Their Customers

    Join Us

    Create, distribute and aggregate “Just In Time” tutorials that help your customers and ease your support load.

    Learn More

  • revSelect Add-Ons

    Supercharge your Revolution development with RevSelect products.

© 2009 Releasable Revolution