Vigilante

Vigliant Media Framework

Documentation

Tutorials

Using Vigilante database classes

Vigilante contains a class named obj, which can simplify database interaction slightly further.

obj contains a method named GetRS(), which aliases the SelectRecord() function. Instead of specifying a table name with each call to SelectRecord(), you can instead instantiate a new obj, set the $table property with the name of the database table and pass a condition to GetRS().

obj also contains a method named GetInfo(), which aliases GetRS(). You can set the $tableIndex property with the name of the field which serves as the primary index, then pass an index value to GetInfo().

Example 2-5-1
//Retrieve all albums with a specific artist ID as a resource.
$result_01 = SelectRecord("Albums", "ArtistID=$ArtistID", "row");

//Retrieve all albums with a specific album ID as an object.
$result_02 = SelectRecord("Albums", "AlbumID=$AlbumID", "rs");
Example 2-5-2
//Instantiate a new obj
$obj = new obj;

//Specify the table the object accesses.
$obj->table = "Albums";

//Specify the primary index of the table.
$obj->tableIndex = "AlbumID";

//Retrieve all albums with a specific artist ID as a resource.
$result_01 = $obj->GetRS("ArtistID=$ArtistID", "row");

//Retrieve an album with a specific album ID as an object.
$result_02 = $obj->GetInfo($AlbumID, "rs");

Example 2-5-2 requires more code than it would need to perform the same tasks as Example 2-5-1. You can, however, extend the obj class with your own custom classes, then define the $table and $tableIndex properties within those classes.

In this example, a developer is tired of specifying the Albums table in each of his queries. So he extends the obj class with a new class named albums.

Example 2-5-3
class albums extends obj
{
     $this->table = "Albums";
     $this->tableIndex = "AlbumID";
}

Now, the developer can use this object to query the Albums table.

Example 2-5-4
// Create an albums object.
$album = new albums;

// Retrieve a record of one album as an object.
$recordset_album = $album->GetInfo($AlbumID, "rs");

// Print out the album title.
echo "<p>$recordset_album->AlbumTitle</p>";

// Retrieve all albums by one artist as a resource.
$result_all_albums = $album->GetRS("ArtistID=$ArtistID", "row");

// Print out all the albums.
while ($recordset_all_albums = mysql_fetch_object($result_all_albums))
{
     [ ...display results... ]
}