i am currently working on a module, where i want to create tables and columns dynamically. for the tables this is now problem, because i can define them in pntables.php. but i also need to create columns, that i need to append to the pntable['tbl_column'] and pntable['tbl_column_def'] arrays in pntables.php.
i already searched the forums and found a few hints, but actually nothing that i tried really worked.
first i tried to call an API function from within pntables.php, but since i need a mysql select, when using DBUtil it gives me an error. i think the reason is, because DBUtil itself needs/depends on pntables.php, and therefore ends in some kind of loop.
so i tried to use DBUtil:metaColumns, since this seemed more promising, but again - it didnt work.
so now im stuck.is there an easy way to dynamically generate the necessary information and append it to the pntable array?
so this is what i currently have:
for my defense: its my first real PHP project, and i pray to the PN devs for this wonderfully easy API :)
Code
function pnAddressBook_pntables()
{
// Initialise table array
$pntable = array();
// Full table definition
$pntable['pnaddressbook_address'] = DBUtil::getLimitedTablename('pnaddressbook_address');
$pntable['pnaddressbook_address_column'] = array('adrid' => 'adr_id',
'adrprefix' => 'adr_prefix',
'adrname' => 'adr_name',
'adrfname' => 'adr_fname',
'adrsortname' => 'adr_sortname',
'adrtitle' => 'adr_title',
'adrcompany' => 'adr_company',
'adrsortcompany' => 'adr_sortcompany',
'adrimg' => 'adr_img',
'adrzip' => 'adr_zip',
'adrcity' => 'adr_city',
'adraddress1' => 'adr_address1',
'adraddress2' => 'adr_address2',
'adrstate' => 'adr_state',
'adrcountry' => 'adr_country',
/* 'adrcontact_1' => 'adr_contact_1',
'adrcontact_2' => 'adr_contact_2',
'adrcontact_3' => 'adr_contact_3',
'adrcontact_4' => 'adr_contact_4',
'adrcontact_5' => 'adr_contact_5',
'adrc_label_1' => 'adr_c_label_1',
'adrc_label_2' => 'adr_c_label_2',
'adrc_label_3' => 'adr_c_label_3',
'adrc_label_4' => 'adr_c_label_4',
'adrc_label_5' => 'adr_c_label_5',
*/ 'adrc_main' => 'adr_c_main',
/* 'adrcustom_1' => 'adr_custom_1',
'adrcustom_2' => 'adr_custom_2',
'adrcustom_3' => 'adr_custom_3',
'adrcustom_4' => 'adr_custom_4',
*/ 'adrnote' => 'adr_note',
'adruser_id' => 'adr_user',
'adrluser_id' => 'adr_luser',
'adrprivate' => 'adr_private',
'adrdate' => 'adr_date');
$pntable['pnaddressbook_address_column_def'] = array('adrid' => "I(11) NOTNULL AUTOINCREMENT PRIMARY",
'adrprefix' => "I(11)",
'adrname' => "C(100) DEFAULT ''",
'adrfname' => "C(100) DEFAULT ''",
'adrsortname' => "C(200) DEFAULT ''",
'adrtitle' => "C(100) DEFAULT ''",
'adrcompany' => "C(255) DEFAULT ''",
'adrsortcompany' => "C(255) DEFAULT ''",
'adrimg' => "C(100) DEFAULT ''",
'adrzip' => "C(30) DEFAULT ''",
'adrcity' => "C(100) DEFAULT ''",
'adraddress1' => "C(255) DEFAULT ''",
'adraddress2' => "C(255) DEFAULT ''",
'adrstate' => "C(60) DEFAULT ''",
'adrcountry' => "C(60) DEFAULT ''",
/* 'adrcontact_1' => "C(200) DEFAULT ''",
'adrcontact_2' => "C(200) DEFAULT ''",
'adrcontact_3' => "C(200) DEFAULT ''",
'adrcontact_4' => "C(200) DEFAULT ''",
'adrcontact_5' => "C(200) DEFAULT ''",
'adrc_label_1' => "I(4) DEFAULT",
'adrc_label_2' => "I(4) DEFAULT",
'adrc_label_3' => "I(4) DEFAULT",
'adrc_label_4' => "I(4) DEFAULT",
'adrc_label_5' => "I(4) DEFAULT",
*/ 'adrc_main' => "I(4) DEFAULT",
/* 'adrcustom_1' => "C(200) DEFAULT ''",
'adrcustom_2' => "C(200) DEFAULT ''",
'adrcustom_3' => "C(200) DEFAULT ''",
'adrcustom_4' => "C(200) DEFAULT ''",
*/ 'adrnote' => "X DEFAULT ''",
'adruser_id' => "I(11) NOTNULL",
'adrluser_id' => "I(11) NOTNULL",
'adrprivate' => "I(4) NOTNULL",
'adrdate' => "datetime");
// this is really no good solution i fear, but it might work.
// could also use array_push
$custs = DBUtil::metaColumns('pnaddressbook_address');
// $carray = array();
// $darray = array();
foreach ($custs as $cust) {
$tmp = get_object_vars($cust);
if (strpos($tmp['name'], 'custom') || strpos($tmp['name'], 'contact') || strpos($tmp['name'], 'label')) {
$tmp2 = preg_replace('/^adr_/', 'adr', $tmp['name']);
$pntable['pnaddressbook_address_column'][$tmp2] = $tmp['name'];
// needs to be adjusted a little more
if ($tmp['type'] == 'text') {
$pntable['pnaddressbook_address_column_def'][$tmp2] = $tmp['type'];
} else {
$pntable['pnaddressbook_address_column_def'][$tmp2] = "{$tmp['type']}({$tmp['max_length']})";
}
}
}
return $pntable;
}
{
// Initialise table array
$pntable = array();
// Full table definition
$pntable['pnaddressbook_address'] = DBUtil::getLimitedTablename('pnaddressbook_address');
$pntable['pnaddressbook_address_column'] = array('adrid' => 'adr_id',
'adrprefix' => 'adr_prefix',
'adrname' => 'adr_name',
'adrfname' => 'adr_fname',
'adrsortname' => 'adr_sortname',
'adrtitle' => 'adr_title',
'adrcompany' => 'adr_company',
'adrsortcompany' => 'adr_sortcompany',
'adrimg' => 'adr_img',
'adrzip' => 'adr_zip',
'adrcity' => 'adr_city',
'adraddress1' => 'adr_address1',
'adraddress2' => 'adr_address2',
'adrstate' => 'adr_state',
'adrcountry' => 'adr_country',
/* 'adrcontact_1' => 'adr_contact_1',
'adrcontact_2' => 'adr_contact_2',
'adrcontact_3' => 'adr_contact_3',
'adrcontact_4' => 'adr_contact_4',
'adrcontact_5' => 'adr_contact_5',
'adrc_label_1' => 'adr_c_label_1',
'adrc_label_2' => 'adr_c_label_2',
'adrc_label_3' => 'adr_c_label_3',
'adrc_label_4' => 'adr_c_label_4',
'adrc_label_5' => 'adr_c_label_5',
*/ 'adrc_main' => 'adr_c_main',
/* 'adrcustom_1' => 'adr_custom_1',
'adrcustom_2' => 'adr_custom_2',
'adrcustom_3' => 'adr_custom_3',
'adrcustom_4' => 'adr_custom_4',
*/ 'adrnote' => 'adr_note',
'adruser_id' => 'adr_user',
'adrluser_id' => 'adr_luser',
'adrprivate' => 'adr_private',
'adrdate' => 'adr_date');
$pntable['pnaddressbook_address_column_def'] = array('adrid' => "I(11) NOTNULL AUTOINCREMENT PRIMARY",
'adrprefix' => "I(11)",
'adrname' => "C(100) DEFAULT ''",
'adrfname' => "C(100) DEFAULT ''",
'adrsortname' => "C(200) DEFAULT ''",
'adrtitle' => "C(100) DEFAULT ''",
'adrcompany' => "C(255) DEFAULT ''",
'adrsortcompany' => "C(255) DEFAULT ''",
'adrimg' => "C(100) DEFAULT ''",
'adrzip' => "C(30) DEFAULT ''",
'adrcity' => "C(100) DEFAULT ''",
'adraddress1' => "C(255) DEFAULT ''",
'adraddress2' => "C(255) DEFAULT ''",
'adrstate' => "C(60) DEFAULT ''",
'adrcountry' => "C(60) DEFAULT ''",
/* 'adrcontact_1' => "C(200) DEFAULT ''",
'adrcontact_2' => "C(200) DEFAULT ''",
'adrcontact_3' => "C(200) DEFAULT ''",
'adrcontact_4' => "C(200) DEFAULT ''",
'adrcontact_5' => "C(200) DEFAULT ''",
'adrc_label_1' => "I(4) DEFAULT",
'adrc_label_2' => "I(4) DEFAULT",
'adrc_label_3' => "I(4) DEFAULT",
'adrc_label_4' => "I(4) DEFAULT",
'adrc_label_5' => "I(4) DEFAULT",
*/ 'adrc_main' => "I(4) DEFAULT",
/* 'adrcustom_1' => "C(200) DEFAULT ''",
'adrcustom_2' => "C(200) DEFAULT ''",
'adrcustom_3' => "C(200) DEFAULT ''",
'adrcustom_4' => "C(200) DEFAULT ''",
*/ 'adrnote' => "X DEFAULT ''",
'adruser_id' => "I(11) NOTNULL",
'adrluser_id' => "I(11) NOTNULL",
'adrprivate' => "I(4) NOTNULL",
'adrdate' => "datetime");
// this is really no good solution i fear, but it might work.
// could also use array_push
$custs = DBUtil::metaColumns('pnaddressbook_address');
// $carray = array();
// $darray = array();
foreach ($custs as $cust) {
$tmp = get_object_vars($cust);
if (strpos($tmp['name'], 'custom') || strpos($tmp['name'], 'contact') || strpos($tmp['name'], 'label')) {
$tmp2 = preg_replace('/^adr_/', 'adr', $tmp['name']);
$pntable['pnaddressbook_address_column'][$tmp2] = $tmp['name'];
// needs to be adjusted a little more
if ($tmp['type'] == 'text') {
$pntable['pnaddressbook_address_column_def'][$tmp2] = $tmp['type'];
} else {
$pntable['pnaddressbook_address_column_def'][$tmp2] = "{$tmp['type']}({$tmp['max_length']})";
}
}
}
return $pntable;
}
thanks in advance!
philipp
