Register Members List Search Today's Posts Mark Forums Read

Article Options
Datamanagers: Objects as Data and Methods
Posts: n/a

by Guest190829 25 Mar 2008

If you haven't read this article on how to create custom data managers or perhaps you read it and don't see what the big deal is - please read further!

Why go through the complexity of a data manager when I can just manage my data with direct queries and procedural code?

vBulletin's data managing classes can look pretty complicated if you are unfamiliar with OO programming - but the benefits far surpass the few hurtles of adopting a more OO approach to your programming.

A Live Example

We'll start off with a project simulation. A client wants an application that keeps tracks of restaurant reviews. In order to achieve this, we need to set up a database tables that would look something like this:
Table: restaurant

| restaurant id | title | location | menuid | totalreviews

Table: review

| review id | restaurant id | rating | comments | userid

As you can, each review has a rating and user comments and links to a particular restaurant in the database.

A Procedural Approach

Tackling the project is a pretty standard task - the most obvious coding tasks being the insertion, updating, and deleting of restaurant reviews.

(Of course, there could be other aspects too this type of project, but the overall focus will be narrow only to supplement the article's message.)

Here is sample code for restaurant review insertion:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

We did basic error checking and then submitted it into the database. Now let's see some sample code for updating a restaurant review:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The code between updating and inserting looks familiar, doesn't it? Your first thought should be that it looks a little too familiar. One of the quickest signs that your application is going down hill is seeing repeated logic in your code.

But what does this have too do with OO programming and objects? Can't we just code functions to prevent this code duplication?

The OO Mentality: Objects as Data and Methods

With procedural programming, data and behavior are two completely separate entities. You input data into a function and get specific data back. This may not be a problem at times but the data manager classes bring up a specific benefits when handling data.

Since objects are both data and methods, you can avoid messy situations with verifying data. The vBulletin Data Managers offer a nice clear and encapsulated way to check inputted variables. The class contains an array of valid fields which specify methods to verify the inputted data.

Our restaurant review would look something like this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

See how the validfields key VF_METHOD maps to a specific method within the class that only performs one piece of logic. This not only offers a clear view on what method is handling what data, but it also encapsulates logic such that if you ever wanted to extend this data manager in the event of a future version with more extensive features or a simple change in feature, you can do it very easily.

Here would be some of the basic verification methods, all short and sweet:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

With the data manager object, you neatly encapsulate the data and methods into a single entity. With a procedural approach, you'd have to pass the data quite messily along each function - or even worse, couple separate logic into a single function.

Also, with the object - you get increased portability. Adopting to the original insertion and update code with the data manager.

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


  • It's not just your datamanager class data and methods you have at hand, but the base datamanager class you are extending from. This class hierarchy and code reuse you can not achieve with just procedural functions alone. You can simply alter some basic settings (the valid fields array, the table array, etc...etc...) and leave the bulk of the processing to the default class.
  • Easily portable: You can call the datamanager class from anywhere, the forums, the admin cp, and even any custom scripts you create powered by the vBulletin engine. (Calling global.php)
  • Keeps separate operations in there own methods and reduces code duplication.If you have to make an alteration to the business logic (say you want to restrict review comments to 500 characters) you only have to add a verify_comments() method to your class and alter the valid fields array.
  • Inheritance allows for easy extension of feature and entire sub-systems. If your client now wants the review system to accept not only restaurants but clubs and casinos, you can accomplish it easily by just extending from the default Review data manager class.

The intention of this article was to shed to light the benefits of OO programming through a tool utilized and available to you in vBulletin. This article is focused on custom data managers, but the sky is the limit for creating OO applications. Good luck, and don't be afraid to take the dive into the powerful paradigm of object oriented programming.

Additional Reading:

Last edited by Guest190829; 30 Mar 2008 at 04:44..
Views: 6954
Reply With Quote
Old 04 Sep 2009, 19:34
Come2Daddy Come2Daddy is offline
Join Date: May 2008
Thanks for sharing actually I'm not professional OOP, but I know about it as a philosophy I studied it & passed it in college but I don't have the skills to program in OOP style

so I'm not sure of how much have understood of this article, but I wonder why didn't you make the restaurantid auto increment

besides when we check for existence how could we know the restaurantid if we have thousands of restaurants

finally is it a must to use datamanger in inserting data into database via a form embedded in a powered by vbulletin page??? or we can just do procedurally ?
Reply With Quote
Old 06 Sep 2009, 16:40
Antivirus's Avatar
Antivirus Antivirus is offline
Join Date: Sep 2004
Originally Posted by Come2Daddy View Post
is it a must to use datamanger in inserting data into database via a form embedded in a powered by vbulletin page??? or we can just do procedurally ?
You can do it procedurally if you feel more comfortable doing it that way. It's really up to you as the coder. If a datamanager already exists however (for instance, in the case of users, posts, forums, etc...) then i would suggest ALWAYS using the datamanager supplied by vbulletin. The reason I suggest this, is because often the datamanager class will update other tables which the record in the primary table interacts with.

For instance: Say you want to write a query to create a reply by a specific user to a certain thread automatically. This could easily be done with a query, however you must also remember to do a slew of other things such as update the post count of the user making the reply, update the user's last reply dateline field, etc... The datamanager always handles
these things behind the scenes which you don't have to worry about - otherwise if you don't do all these things your forums can easily get out of whack. When creating a datamanager for yourself, the key is to code these issues into it to avoide complications later on.
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team
Reply With Quote
Old 09 Sep 2009, 07:43
Come2Daddy Come2Daddy is offline
Join Date: May 2008
Thanks Antivirus

your answer added a lot to knowledge, now the picture is clearer
thanks a gain
Reply With Quote

Similar Article
Article Author Type Replies Last Post
Miscellaneous Hacks [AJAX] Tabbed Personal Statistics (YUI) Hasann vBulletin 3.7 Add-ons 64 13 Aug 2009 22:17

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

New To Site? Need Help?

All times are GMT. The time now is 00:20.

Layout Options | Width: Wide Color: