About Us
Team
Values
Support
Open Source: dbentity
Overview
Download
Presentations and Articles
Examples: DBEntity Calling
Examples: DBEntity Classes
Examples: DBEntity Syntax
Examples: DB
Examples: DBQUERY
Examples: RBA
FAQ
Contact
ABOUT US
DBEntity - Class examples
class products extends dbentity {
    /**
     * references: specify foreign keys in *this* table
     */    
    function references() {
        return array('brand_id' => 'brands'
                    , 'category_id' => 'categories');
    }
    /**
     * sort_key: specify columns that should stay sequential
     */    
    function sort_key() {
        return array('sort_order', array('category_id'));
    }

    /**
     * mappings: specify many-to-many relationships
     */    
    function mappings() {
        return array(
                'related_products' => array('product_relations', 'from_product_id', 'to_product_id', 'products')
                ,'suppliers' => array('supplier_prices', 'product_id', 'supplier_id', 'suppliers', array('wholesale_price' => 'price', 'sku' => 'sku'))
                );

    }
    /**
     * foreign_references: specify what other tables reference *this* table.
     */    
    function foreign_references() {
        return array('specials' => array('product_specials', 'product_id')
                , 'attributes' => array('product_attributes', 'product_id')
                , 'so_line_items' => array('so_line_items', 'sol_product_id')
                , 'po_line_items' => array('po_line_items', 'product_id')
                );
    }
    /**
     * default_args: specify default arguments
     */    
    function default_args() {
        return array('price' => 0     , 'tax_class_id' => 1
                , 'listed_p' => 1 , 'default_supplier_price' => 0
                , 'image' => '' , 'bimage' => ''
                );
    }
    /**
     * to_string: specify how to display a single entity
     */    
    function to_string($id_or_obj) {
        if (rbok::id($id_or_obj)) {
            $id = $id_or_obj;
            $name = $this->get_name($id);
        }
        elseif (is_array($id_or_obj)) {
            $arr = $id_or_obj;
            $obj = rba::to_object($arr);
            $id = $obj->id;
            if (empty($obj->name)) {
                $name = $this->get_name($id);
            }
            else {
                $name = $obj->name;
            }
        }
        elseif (is_object($id_or_obj)) {
            $obj = $id_or_obj;
            $id = $obj->id;
            $name = $obj->name;
        }
        else {
            $this->fail('to_string only takes an "id" or object');
        }
        return $name;
    }

    /**
     * allow people to use '$' for price and 'init' for initial inventory.
     */
    function rename_keys($args) {
        $args = rba::rename_keys($args,'$','price');
        $args = rba::rename_keys($args,'init','initial_inventory');
        return parent::rename_keys($args);
    }
}