Perhaps my
Active Record system would be of help.
In your case you would create a DB-NPC called: Item
PHP Code:
function onCreated() {
// DB Migrations
this.migrations = {
{"new", "itemname:string:unique", "description:string", "price:float"}, // Creates Table
{"add_index", "itemname"}, // Adds Index to Table
{"echo", "Migrations Completed!"} // Optional Message
};
// Configuration
// Model Object's Specific Class
this.model_class = "activerecord_object_" @ this.name.lower();
// Model Functionality
this.join("activerecord_model");
}
public function validations(obj) {
// Model specific validations
return true;
}
class: activerecord_object_item
PHP Code:
public function setItemName(new_name) {
this.itemname = new_name;
}
public function setItemDescription(new_desc) {
this.description = new_desc;
}
public function setItemPrice(new_price) {
this.price = new_price;
}
public function getItemName() {
return this.itemname;
}
public function getItemDescription() {
return this.description;
}
public function getItemPrice() {
return this.price;
}
public function pretty() {
return format("Name: %s Description: %s Price: %s", this.itemname, this.description, this.price);
}
You can then create Item objects like this:
PHP Code:
function onCreated() {
// Creates Item Object
temp.item = Item.init();
// Sets Item Variables
temp.item.setItemName("Sword");
temp.item.setItemDescription("It's sharp and will poke your eye out!");
temp.item.setItemPrice(100);
// Saves Item to DB
if (temp.item.save()) {
echo("Item saved with ID " @ temp.item.id);
}
}
and look them up and modify them like this:
PHP Code:
function onCreated() {
// Lookup all items
temp.items = Item.all();
for (temp.item: temp.items) {
echo(temp.item.pretty());
}
// Lookup specific item
temp.sword = Item.where("itemname = 'Sword'").get();
if (temp.sword != NULL) {
echo(temp.sword.pretty());
}
// If you know the ID
temp.sword = Item.find(1);
if (temp.sword != NULL) {
// Increase the price for good measure... (example)
temp.sword.setItemPrice(temp.sword.getItemPrice()+100);
if (temp.sword.save()) {
echo(temp.sword.pretty());
}
}
}