I am (in the process of) creating a system to store People and their details - names, date of birth (dob), addresses, phone numbers, etc. - and I'm curious how it is best achieved. Below is an an example of some of the SQL code with test data (the PHP classes match the structure in the db):
CREATE TABLE IF NOT EXISTS `person` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(16) DEFAULT NULL,
`forename` varchar(50) DEFAULT NULL,
`middlenames` varchar(50) DEFAULT NULL,
`surname` varchar(50) DEFAULT NULL,
`display` varchar(100) NOT NULL,
`dob` datetime DEFAULT NULL,
`dod` datetime DEFAULT NULL,
`numViews` int(4) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `person` (`id`, `title`, `forename`, `middlenames`, `surname`, `display`, `dob`, `dod`, `views`) VALUES
(1, 'Miss', 'Julie', 'Nicola Louise', 'Smith', 'Mum', '1958-09-21 00:00:00', NULL, 1),
(2, 'Mr', 'David', NULL, 'Jones', 'Dave', '1932-06-19 00:00:00', NULL, 0);
CREATE TABLE IF NOT EXISTS `email` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`contactId` int(10) unsigned DEFAULT NULL,
`address` varchar(250) DEFAULT NULL,
`type` enum('Personal','Work') DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
INSERT INTO `email` (`id`, `contactId`, `address`, `type`) VALUES
(1, 1, '[email protected]', 'Personal');
CREATE TABLE IF NOT EXISTS `address` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`line1` varchar(50) NOT NULL,
`line2` varchar(50) DEFAULT NULL,
`city` varchar(30) DEFAULT NULL,
`county` varchar(25) DEFAULT NULL,
`postcode` varchar(8) DEFAULT NULL,
`type` enum('Home','Office','Holiday','Other') DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `address` (`id`, `line1`, `line2`, `city`, `county`, `postcode`, `type`) VALUES
(1, '32 Test Road', NULL, 'Some City', NULL, 'PO1 2ST', 'Home'),
(2, '6 High Street', 'New Road', 'Chatham', NULL, 'TN6 8BL', 'Home');
CREATE TABLE IF NOT EXISTS `personaddress` (
`contactId` int(10) unsigned NOT NULL,
`addressId` int(4) unsigned NOT NULL,
PRIMARY KEY (`contactId`,`addressId`),
KEY `addressId` (`addressId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `personaddress` (`contactId`, `addressId`) VALUES
(1, 1),
(2, 2);
I did move the dob & dod fields into another Personal table, as I realised that at times - such as showing email addresses for a Person - that it wasn't necessary to load the dob & dod fields. However, when I did this and when I then wanted to get People with upcoming birthdays, to display the person's name I either had to load a Person object at the same time or add the display
field to the Personal class and join the tables in the SQL - which I really didn't want to do (loose coupling). So I ended up moving the fields back to Person.
So my question is what is the best way of implementing this structure?
Note that in a class I currently have standard getter/setter
methods, then additional methods (such as getFullName
, getFullLegalName
, etc. for Person) and finally methods for db interaction – load
, save
, etc. I've also been informed that really I should put these other methods into different classes. So my second question (which I suppose is tied to the first really) is should I also have separate classes for different sets of methods?