this is my class.DataManager.php
<?php
require_once('class.Entity.php'); //this will be needed later
require_once('class.Individual.php');
require_once('class.Organization.php');
class DataManager
{
private static function _getConnection()
{
static $hDB;
if(isset($hDB))
{
return $hDB;
}
/*$hDB = mysql_connect("localhost", "phpuser", "phppass", "pro_php5")
or
die("Failure connecting to the database!");
return $hDB;*/
$host = 'localhost';
$user = 'phpuser';
$pass = 'phppass';
$database = 'pro_php5';
$dbcnx = @mysql_connect($host, $user, $pass);//connect to the database
if (!$dbcnx)
{
echo 'no connection was made';
exit();//if no connection, exit
}
mysql_select_db($database, $dbcnx);//select database
if (! @mysql_select_db($database) )
{
echo 'no database exits';
exit();//if no database, exit
}
return $dbcnx;
}
public static function getAddressData($addressID)
{
$sql = "SELECT * FROM entityaddress WHERE addressid = $addressID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting email data for email $emailID");
}
return mysql_fetch_assoc($res);
}
public static function getEmailData($emailID)
{
$sql = "SELECT * FROM entityemail WHERE emailid = $emailID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting email data for email $emailID");
}
return mysql_fetch_assoc($res);
}
public static function getPhoneNumberData($phoneID)
{
$sql = "SELECT * FROM entityphone WHERE phoneid = $phoneID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting phone number data for phone $phoneID");
}
return mysql_fetch_assoc($res);
}
public static function getEntityData($entityID)
{
$sql = "SELECT * FROM entities WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting entity $entityID");
}
return mysql_fetch_assoc($res);
}
public static function getAddressObjectsForEntity($entityID)
{
$sql = "SELECT addressid FROM entityaddress WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());
if(!res)
{
die("Failed getting address data for entity $entityID");
}
if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new Address($rec['addressid']);
}
return $objs;
}
else
{
return array();
}
}
public static function getEmailObjectsForEntity($entityID)
{
$sql = "SELECT emailid FROM entityemail WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());
if(!res)
{
die("Failed getting email data for entity $entityID");
}
if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new EmailAddress($rec['emailid']);
}
return $objs;
}
else
{
return array();
}
}
public static function getPhoneNumberObjectsForEntity($entityID)
{
$sql = "SELECT phoneid FROM entityphone WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());
if(!res)
{
die("Failed getting phone data for entity $entityID");
}
if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new PhoneNumber($rec['phoneid']);
}
return $objs;
}
else
{
return array();
}
}
public static function getEmployer($individualID)
{
$sql = "SELECT organizationid FROM entityemployee WHERE individualid = $individualID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting employer info for individual $individualID");
}
$row = mysql_fetch_assoc($res);
if($row)
{
return new Organization($row['organizationid']);
}
else
{
return null;
}
}
public static function getEmployees($orgID)
{
$sql = "SELECT individualid FROM entityemployee WHERE organizationid = $orgID";
$res = mysql_query($sql, DataManager::_getConnection());
if(! ($res && mysql_num_rows($res)))
{
die("Failed getting employee info for org $orgID");
}
if($res)
{
$objs = array();
while( $row = mysql_fetch_assoc($res))
{
$objs[] = new Individual($row['individualid']);
}
return $objs;
}
else
{
return array();
}
}
public static function getAllEntitiesAsObjects()
{
$sql = "SELECT * FROM entities";
$res = mysql_query($sql, DataManager::_getConnection());
if(!$res)
{
die("Failed getting all entities");
}
if(mysql_num_rows($res))
{
$objs = array();
while($row = mysql_fetch_assoc($res))
{
if($row['type'] == 'I')
{
try
{
$objs[] = new Individual($row['entityid']);
}
catch(Exception $e)
{
echo 'Message: ' . $e->getMessage();
}
}
elseif($row['type'] == 'O')
{
try
{
$objs[] = new Organization($row['entityid']);
}
catch(Exception $e)
{
echo 'Message: ' . $e->getMessage();
}
}
else
{
die("Unknown entity type {$row['type']} encountered!");
}
}
return $objs;
}
else
{
return array();
}
}
}
?>
here its the sql
CREATE TABLE entities (
entityid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name1 varchar(100) NOT NULL,
name2 varchar(100) NOT NULL,
type char(1) NOT NULL
);
CREATE TABLE entityaddress (
addressid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
saddress1 varchar(255),
saddress2 varchar(255),
scity varchar(255),
cstate char(2),
spostalcode varchar(10),
stype varchar(50),
CONSTRAINT fk_entityaddress_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);
CREATE TABLE entityemail (
emailid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
semail varchar(255),
stype varchar(50),
CONSTRAINT fk_entityemail_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);
CREATE TABLE entityemployee (
individualid INT NOT NULL,
organizationid INT NOT NULL,
CONSTRAINT fk_entityemployee_individualid
FOREIGN KEY (individualid) REFERENCES entity(entityid),
CONSTRAINT fk_entityemployee_organizationid
FOREIGN KEY (organizationid) REFERENCES entity(entityid)
);
CREATE TABLE entityphone (
phoneid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
snumber varchar(20),
sextension varchar(20),
stype varchar(50),
CONSTRAINT fk_entityemail_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);
Last edited by loremipsum3891; November 12th, 2009 at 11:31 PM..
|