Creating a Message Board - Creating Data Objects
Before we do any coding, we are going to create some data objects that map to the tables that we created in the previous section. But before we do that, you may be wondering why we are using data objects at all. Using Stratos data objects provides us with several benefits:
- Data objects are portable and independent of the underlying database.
- We don't have to write any SQL statements. Rather than "SELECT * from users", we will use methods on our data objects such as User::getAll().
- Stratos Data Objects provide us with protection from SQL injection
- Although we won't be using the Scaffold plugin in this tutorial, we can use data objects to automatically generate CRUD interfaces for database tables.
How Are Data Objects Created?
The Stratos Framework makes it very easy to create data objects. You will not have to edit any XML files or create any classes yourself. You will create data objects by using the Stratos Developer Toolkit, which is bundled with the framework. Simply visit the Stratos Toolkit for your application, which is located at:
http://www.yourdomain.com/path/to/your/app/index.php/Stratos
Since I am developing my application in the top level directory for the litebb.com domain, my Stratos Toolkit is located at:
http://www.litebb.com/index.php/Stratos
If I chose to develop my application in a subdirectory called forums/, then my toolkit would be located at:
http://www.litebb.com/forums/index.php/Stratos
When you have located your Stratos Toolkit (it should be shown by default if you just installed Stratos), select StratosData from the left hand menu.
Creating a Dataset
In order to create data objects, we must set up a database connection. So you will first need to create a dataset, which defines a database connection and a set of data objects. There is a default dataset defined when you install Stratos, so let's go ahead and edit the default dataset. The most important setting is the DSN, which is the connection string that will be used to connect to your database. If Stratos cannot connect to your database, then your data objects are useless. So, next to the DSN field, enter the details of your database connection in the following format:
mysql://username:password@yourhost/database_name
Leave all other fields the same for now, and click submit. If you entered in everything correctly, your database connection should now be ready to use.
Creating the Data Objects
Now let's create some data objects. To do this, click the Add Data Object link next to the dataset we just edited. After clicking this link, you should see a list of all of the tables in your database. We will create the Forum data object first, so click on the forums table. You will be taken to a form that allows you to enter the Forum data object settings before it is created. Rather than making you type in all of this information yourself, Stratos attempts to provide some sensible defaults for these values based on the name of the table you selected. For instance, if you have a table called forums, then Stratos will name the data object Forum, and automatically generate some helpful methods based on the names of the columns in the table. For our Forum data object, we do not need to modify any of the information for the data object, so go ahead and click submit to create your data object.
Now that the Forum data object has been created, let's create the Post data object using the same process we just described, but using the posts table. Notice that Stratos automatically picks up the relationship between posts and forums, and posts and users. This is great because Stratos will automatically generate helpful methods based on these relationships. For instance, we will be able to use $post->getForum() on an instance of Post to access the forum that the post is associated with. Or we could write $user = $post->getUser(), then access the user's username by displaying $user->username.
Congratulations, champ. Your data objects are now ready to go. Now we can finally do some coding.