// a short sample using db_view extending some OsCommerce class
// just to look at.
// no samples yet to insert/update/delete multiple tables
class companyObj extends db_view {
var $company_id;
// class constructor
function companyObj($cID = false) {
global $current_user, $languages_id;
// set db_view options
$this->db_tables = array(TABLE_COMPANY,TABLE_COMPANY_INFO,TABLE_ADDRESS_BOOK,TABLE_USERS);
$this->db_tables_aliases = array('c','ci','ab','u');
$this->db_tables_fields = array();
// use only these fields, not all
$this->db_tables_fields[TABLE_USERS] = 'users_email_address,users_firstname,users_lastname,org_id,users_default_address_id,users_telephone,users_fax,users_mobile_telephone';
$this->db_select = "WHERE c.company_id=ci.company_id and u.users_default_address_id=ab.address_book_id and c.user_id=u.user_id and ci.languages_id='$languages_id'";
// if set disallow named fields. faster.
//$this->_db_no_named_arrays = 1;
// init the db_view class
$this->db_init($cID);
// register callbacks to get/set non-atomic data
$this->create_function('SELECT',TABLE_COMPANY,'company_c4y_offered','unserialize');
$this->create_function('INSERT|UPDATE',TABLE_COMPANY,'company_c4y_offered','serialize');
// $this->create_function('SELECT',TABLE_COMPANY,'email_b4y_periodic','ur_flag2array');
// $this->create_function('INSERT|UPDATE',TABLE_COMPANY,'email_b4y_periodic','ur_array2flag');
$this->fields =& $this->db_fields;
$this->company_fields = join(',',$this->db_tables_fields[TABLE_COMPANY]);
$this->company_info_fields = join(',',$this->db_tables_fields[TABLE_COMPANY_INFO]);
$this->address_book_fields = join(',',$this->db_tables_fields[TABLE_ADDRESS_BOOK]);
// initialization: with array, id or nothing
if (! $cID ) {
// try to find the current company
if (!empty($current_user->company_id)) $cID = $current_user->company_id;
else $cID = ur_db_fetch_id('select company_id from ' . TABLE_COMPANY . " where user_id = '$uID'");
$company = $this->init($cID);
} elseif (is_array($cID)) {
$company = $cID;
} else { // numeric database id
// init the company class with all values from the tables
$company = $this->init($cID);
}
}
// restore object vars from db. here no override.
function init ($id) {
$values = db_view::init($id);
// you can do something with the values here, which are not done in the SELECT callbacks.
return $values;
}
// insert or update the current object
function store () {
if ($this->select_row($this->id)) {
if ($this->update())
$id = $this->company_id;
} else {
$id = $this->insert();
$this->company_id = $id;
}
return $id;
}
// override db_view::as_array() to exclude private creditcard information
function as_array ($exclude = false, $fields = false) {
$a = db_view::as_array($exclude,$fields);
$a['cc_number'] = mangle_cc_number ($a['cc_number']);
return $a;
}
}
$current_company = new companyObj();
$current_company->delete($current_company->company_id);
// ...
|