{"id":1533,"date":"2018-01-19T19:22:57","date_gmt":"2018-01-19T19:22:57","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/value-objects-and-data-access-objects-with-php-4-2-x\/"},"modified":"2018-01-19T19:24:51","modified_gmt":"2018-01-19T19:24:51","slug":"value-objects-and-data-access-objects-with-php-4-2-x","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/value-objects-and-data-access-objects-with-php-4-2-x\/","title":{"rendered":"Value Objects and Data Access Objects with PHP 4.2.x"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Geno Timlin<\/div>\n<div class=\"\">on June 1, 2004<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Introduction<\/h2>\n<div class=\"articlePara\">\nMy real job involves working with J2EE applications at this time.  It pays the bills.<br \/>\nI&#8217;ve worked with ASP in the past and have recently (2003) gotten more into working with PHP.<br \/>\nMost of my sidework these days is data driven PHP\/MySQL web sites.  It&#8217;s unfortunate that<br \/>\nsidework doesn&#8217;t pay the bills, because I have found PHP to be a very powerful programming platform.<br \/>\nMy attempt here is to apply some principles and concepts that I have learned in my work with<br \/>\nJ2EE into my work with PHP.<\/div>\n<h2>Value Objects<\/h2>\n<div class=\"articlePara\">\nThe purpose of a Value Object is to represent a business entity.  In it&#8217;s simplest form, a<br \/>\nVO is a simple data mapping class that mirrors the entity as it exists in the database.  A<br \/>\nmore complex VO may incorporate other functionality in its methods.  For example, a VO may<br \/>\nhave a method that transforms it&#8217;s data into an XML node or a VO may be able to read it&#8217;s<br \/>\ndata from an HTML form.  The one thing the VO doesn&#8217;t have to worry about is the database.<br \/>\nThe main purpose of the VO is to store data as the data comes in and out of the database.<\/div>\n<h2>Data Access Objects<\/h2>\n<div class=\"articlePara\">\nData Access Objects act as the middle man between the database and the Value Objects.<br \/>\nThe base DAO class knows specifically how to talk to the database.  The base DAO knows which<br \/>\ndatabase is being used, what kind of database it is and whether it&#8217;s supposed to be working with<br \/>\nmysql_*, mssql_* or pg_* methods.  The base DAO is essentially the Data layer or Model part of the MVC<br \/>\nThe DAO classes that extend the base DAO class can use the base DAO&#8217;s methods to select, insert,<br \/>\nupdate and delete data.  The extended DAO classes don&#8217;t have to worry about whether they are talking<br \/>\nto a MySQL database or a PostGreSQL database.  If you switch from MySQL to PostGreSQL, you can change<br \/>\nthe base DAO class and the extended DAO&#8217;s will not be affected.  The extended DAO&#8217;s are the business<br \/>\nlogic layer or the Controller part of the MVC.<\/div>\n<div class=\"articleHeader\">\nDAO&#8217;s and VO&#8217;s Working Together<\/div>\n<div class=\"articlePara\">\nThe extended DAO classes utilize the VO&#8217;s to retrieve data and update data.  If you want a single<br \/>\nrow of data from the database, based on the record&#8217;s primary key value, the extended DAO will<br \/>\nhave a method that returns a single VO.  The extended DAO will have a method that returns an array of<br \/>\nVO&#8217;s if you want more than one row of data.  The extended DAO handles inserts, updates and deletes<br \/>\nwith methods that take a single VO as an argument.<\/div>\n<div class=\"articleHeader\">\nThe Application<\/div>\n<div class=\"articlePara\">\nIn the example code, I&#8217;m presenting some of the functionality of a simple auto sales web site.<br \/>\nWe&#8217;ll look at the vehicles that are stored in the database.  The database is a MySQL database and<br \/>\nthe vehicles table is created with the following properties:<\/p>\n<div class=\"example\">\n<pre>\nCREATE TABLE `vehicles` (\n`vehicleid` INT NOT NULL AUTO_INCREMENT,\n`year` VARCHAR( 4 ) NOT NULL ,\n`make` VARCHAR( 25 ) NOT NULL ,\n`model` VARCHAR( 25 ) NOT NULL ,\n`color` VARCHAR( 25 ) NOT NULL ,\n`price` DOUBLE NOT NULL ,\nPRIMARY KEY ( `vehicleid` ) \n);\n<\/pre>\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin200405284658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin200405289ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin20040528fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin20040528af4d.html?page=5\">5<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin20040528c575.html?page=6\">6<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"timlin20040528235c.html?page=7\">7<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"timlin200405284658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n<div style=\"background-color:#EDEDED; width:628px;  \"><!-- START COMMENTING CODE --><\/p>\n<p><script src=\"..\/js\/boxover.js\" type=\"text\/javascript\"><![CDATA[\n]]><\/script><script type=\"text\/javascript\" src=\"..\/..\/www.google.com\/recaptcha\/api\/js\/recaptcha_ajax.js\"><![CDATA[\n]]><\/script><script type=\"text\/javascript\"><![CDATA[\n    \n        \n    function stripslashes (str) {\n    \/\/ +   original by: Kevin van Zonneveld (http:\/\/kevin.vanzonneveld.net)\n    \/\/ +   improved by: Ates Goral (http:\/\/magnetiq.com)\n    \/\/ +      fixed by: Mick@el\n    \/\/ +   improved by: marrtins    \/\/ +   bugfixed by: Onno Marsman\n    \/\/ +   improved by: rezna\n    \/\/ +   input by: Rick Waldron\n    \/\/ +   reimplemented by: Brett Zamir (http:\/\/brett-zamir.me)\n    \/\/ +   input by: Brant Messenger (http:\/\/www.brantmessenger.com\/)    \/\/ +   bugfixed by: Brett Zamir (http:\/\/brett-zamir.me)\n    \/\/ *     example 1: stripslashes('Kevin's code');\n    \/\/ *     returns 1: \"Kevin's code\"\n    \/\/ *     example 2: stripslashes('Kevin's code');\n    \/\/ *     returns 2: \"Kevin's code\"    return (str + '').replace(\/(.?)\/g, function (s, n1) {\n        switch (n1) {\n        case '':\n            return '';\n        case '0':            return 'u0000';\n        case '':\n            return '';\n        default:\n            return n1;        }\n    }\n\n    function readCookie(name) {\n\tvar nameEQ = name + \"=\";\n\tvar ca = document.cookie.split(';');\n\tfor(var i=0;i < ca.length;i++) {\n\t\tvar c = ca[i];\n\t\twhile (c.charAt(0)==' ') c = c.substring(1,c.length);\n\t\tif (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);\n\t}\n\treturn null;\n    }\n    \nfunction validateCaptcha()\n{\n\n    var user = document.forms[\"commentform\"][\"username\"].value;\n    if (user == null || user == \"\")\n    {\n        alert(\"Username name must be filled out\");\n        return false;\n    }\n\n    \/\/email validation\n    var email = document.forms[\"commentform\"][\"email\"].value;\n    var atpos = email.indexOf(\"@\");\n    var dotpos = email.lastIndexOf(\".\");\n    if (atpos<1 || dotpos<atpos+2 || dotpos+2>= email.length)\n    {\n        alert(\"Please enter a valid e-mail address\");\n        return false;\n    }\n\n    \/\/comment validation\n    var comment=document.forms[\"commentform\"][\"countdown\"].value;\n    if (comment == 1200)\n    {\n        alert(\"You must enter a commment\");\n        return false;\n    }\n    else if (comment > 1195) \/\/count the characters in the comment (which the site already does) if user types less than 10 characters tell them to enter more.\n    {\n        alert(\"You must enter a longer comment\");\n        return false;\n    }\n\n    var userResponse = document.forms[\"commentform\"][\"recaptcha_response_field\"].value;\n\n    var challenge = document.forms[\"commentform\"][\"recaptcha_challenge_field\"].value;\n\n\n\n    var length = userResponse.length;\n    if (length == 0)\n    {\n        alert (\"Please enter recaptcha text that is shown in the box\");\n        return false;\n    }\n    var blurbKey = document.forms[\"commentform\"][\"blurbKey\"].value;\n    var comment = document.forms[\"commentform\"][\"limitedtextarea\"].value;\n    var username = document.forms[\"commentform\"][\"username\"].value;\n    var email = document.forms[\"commentform\"][\"email\"].value;\n    var parentKey = document.forms[\"commentform\"][\"parentKey\"].value;\n    \n    var result;\n    var xmlhttp;\n    if (window.XMLHttpRequest)\n      {\/\/ code for IE7+, Firefox, Chrome, Opera, Safari\n      xmlhttp=new XMLHttpRequest();\n      }\n    else\n      {\/\/ code for IE6, IE5\n      xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");\n      }\n    xmlhttp.onreadystatechange=function()\n      {\n      if (xmlhttp.readyState==4 && xmlhttp.status==200)\n        {\n            \n            result = xmlhttp.responseText;        \n        }\n      }\n\n    xmlhttp.open(\"POST.html\",\"..\/..\/..\/commentAdd\",false);\n\n\n    xmlhttp.setRequestHeader(\"Content-type\",\"application\/x-www-form-urlencoded\");\n    xmlhttp.send(\"recaptcha_response_field=\" + userResponse + \"&recaptcha_challenge_field=\" + challenge + \"&blurbKey=\" + blurbKey + \"&comment=\" + comment + \"&username=\" + username + \"&email=\" + email + \"&parentKey=\" + parentKey);\n\n\n\n    if (result == \"false\")\n    {\n        alert ('Your response did not match the image. Try again');\n        Recaptcha.create(\"6LfoP8wSAAAAAJHAw37OAAdm56OaVpgHvqHZv1dd\",\n            \"recaptcha\",\n            {\n              theme: \"red\",\n              callback: Recaptcha.focus_response_field\n            }\n          );\n        return false;\n    }\n    else if (result == \"true\")\n    {\n        \/\/set variables\n        var author = escape(document.getElementById(\"username\").value);\n        \/\/author = document.getElementById(\"comment_username\").value;\n        var comment = escape(document.getElementById(\"comment_body\").value);\n        \n        \/\/update divs with the recently submitted comment author and comment\n        document.getElementById(\"commentOutput\").innerHTML= unescape(comment);\n        \n        document.getElementById(\"author\").innerHTML= unescape(author);\n        \n        \/\/show div that says comment submitted\n        document.getElementById(\"commentSubmitted\").style.display = 'block';\n        \/\/check for existing cookie\n        \n        var previous = readCookie('comment');\n        \n        if (!previous) \/\/if there is a prev\n        {\n            document.getElementById(\"previousComment\").style.display = 'block';\n        }\n        \n        \/\/set cookies for 1 day so upon refresh user will still see comment was submited\n        author = unescape(author);\n        comment = unescape(comment);\n\n        document.cookie = \"username=\" + unescape(author);\n        document.cookie = \"comment=\" + comment;\n\n        \/\/reset username\n        document.getElementById(\"username\").value= '';\n        document.getElementById(\"parentKey\").value= '';\n        document.getElementById(\"comment_email\").value= '';\n        document.getElementById(\"comment_body\").value= '';\n        document.getElementById(\"comment_countdown\").value= '1200';\n        \n        \/\/generate a new captcha\n        Recaptcha.create(\"6LfoP8wSAAAAAJHAw37OAAdm56OaVpgHvqHZv1dd\",\n            \"recaptcha\",\n            {\n              theme: \"red\",\n              callback: Recaptcha.focus_response_field\n            }\n          );\n        return false;\n    }\n    else \/\/if there is no response from the server making the ajax call\n    {\n        return false;\n    }\n\n}\n\n]]><\/script><\/p>\n<style><![CDATA[\n    #commentSubmitted {\n        display: none;\n        padding-left: 10px;\n    }\n    #previousComment {\n        display: none;\n        padding-left: 10px;\n        padding-bottom: 15px;\n    }\n    #previousComment2 {\n        padding-left: 10px;\n        padding-bottom: 15px;\n    }\n    \n\n    .hand { cursor:pointer; cursor:hand; }\n\n    .topdivdemo2 { padding:5px; margin:0 0 00;height:10px;width:120px;font-weight:bold; border:1px solid #CCFFFF;background:#3366FF;color:#FFFFFF;text-align: center; }\n    .botdivdemo2 { padding:5px; margin:0 0 0 0;height:10px;width:120px;border:1px solid #CCFFFF; background:#66FF33;color:#FFFFFF; }\n    .topdivdemo4 { padding:5px; margin:0 0 0 0;text-align:center;height:10px;width: 250px;font-weight:bold; border:1px solid #CCFFFF;background:#99CCCC; color:#FFFFFF; }\n    .botdivdemo4 { padding:5px; margin:0 0 0 0;height:5px;width:250px;border:1px solid #CCFFFF; background:#3399CC;color:#FFFFFF; }\n    .alerttop { padding:10px; margin:0 0 0 0;height:10px;font-weight:bold;border:1px solid #333; background:#cccccc;color:#333; font-family:arial, verdana, hellvetica; font-size: 11px; }\n]]><\/style>\n<p class=\"style1\">\n<p><noscript>Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.<\/noscript> <\/p>\n<div id=\"comment_response\"\/>\n<div id=\"comment_error\" class=\"error\"\/>\n<div id=\"commentForm\" class=\"formHeader\">\n    Comment and Contribute\n<\/div>\n<div class=\"comment_wrapper\">\n<div style=\"font-weight: bold;\"><font id=\"commentCount\">0<\/font> Comments\u00a0<a href=\"#comment_form\" style=\"text-decoration: underline;\">(click to add your comment)<\/a><\/div>\n<div id=\"comment_pagination_top\"\/>\n<div id=\"comment_page_container\"\/>\n<div id=\"comment_pagination_bottom\"\/>\n<div class=\"formFooter\"\/>\n\t<!-- <script src=\"\/js\/comments.js\"\ttype=\"text\/javascript\"><\/script> --><br \/>\n\t<!-- <script src=\"\/js\/boxover.js\"\ttype=\"text\/javascript\"><\/script> -->\n    <\/div>\n<div style=\"clear: both;\"\/>\n<div id=\"commentSubmitted\">\n<h2>Your comment has been submitted and is pending approval.<\/h2>\n<\/div>\n<div id=\"previousComment\">\n<p\/>\n<div style=\"float:left; font-weight: bold; padding-right: 4px;\">Author:<\/div>\n<div style=\"float:left;\" id=\"author\">\n<p>Geno Timlin                                    <\/p><\/div>\n<div style=\"clear:both\"\/>\n<p\/>\n<div style=\"float:left; font-weight: bold; padding-right: 4px;\">Comment:<\/div>\n<div style=\"float:left;\" id=\"commentOutput\">\n    <\/div>\n<\/div>\n<div style=\"clear:both\"\/>\n<p><span id=\"comment\"\/><\/p>\n<div class=\"comment_wrapper\">\n<div style=\"display: inline; margin-left: 0px;\" id=\"comment_page1\"\/>\n    <!-- <script type=\"text\/javascript\" src=\"http:\/\/commufnity.internet.com\/earthweb\/functions.js\"><\/script> --><\/p>\n<div style=\" \">\n<form method=\"post\" action=\"https:\/\/phpbuilder.com\/commentAdd\" name=\"commentform\" onsubmit=\"return validateCaptcha()\">\n            <input type=\"hidden\" id=\"blurbKey\" value=\"54486510\" name=\"blurbKey\"\/><input type=\"hidden\" value=\"Teleworkers are a Boon to the Bottom Line\" id=\"comment_title\" name=\"comment_title\"\/><input type=\"hidden\" value=\"\" id=\"parentKey\" name=\"parentKey\"\/><input type=\"hidden\" value=\"cioupdate\" id=\"sitename\" name=\"sitename\"\/><input type=\"hidden\" value=\"\" id=\"articleLink\" name=\"articleLink\"\/><a name=\"comment_form\"\/><br \/>\n            <img loading=\"lazy\" decoding=\"async\" class=\"greyCommentBar\" src=\"..\/img\/img_trans.gif\" width=\"1\" height=\"1\"\/><\/p>\n<div style=\"font-weight: bold; font-size: 14pt; font-family: arial,helvetica,sans-serif; color: black; background-color: rgb(255, 255, 255); width: 100%;\"><\/div>\n<div id=\"submitCommentForm\" style=\"width: 80%; margin-left: 10px;\">\n                <label for=\"username\"><strong>Name\/nickname:<\/strong> <\/label> <input type=\"text\" value=\"\" name=\"username\" id=\"username\" size=\"30\"\/><br \/><label><strong>Email:<\/strong><\/label><input type=\"text\" value=\"\" name=\"email\" id=\"comment_email\" size=\"30\"\/><\/p>\n<p style=\"font-size: 8pt;\"><strong>Comment:<\/strong> <\/p>\n<div>\n                    <textarea id=\"comment_body\" name=\"limitedtextarea\" onkeydown=\"limitText(this.form.limitedtextarea,this.form.countdown,1200);\" onkeyup=\"limitText(this.form.limitedtextarea,this.form.countdown,1200);\" style=\"font: 9pt arial,helvetica,sans-serif; overflow: auto;\" rows=\"10\" cols=\"80\"\/><\/div>\n<div style=\"font-size: 8pt;\">\n                    (Maximum characters: 1200). You have <input type=\"text\" size=\"4\" id=\"comment_countdown\" value=\"1200\" name=\"countdown\"\/> characters left.\n                <\/div>\n<p><\/p>\n<div style=\"margin-left:8px; text-align: center;\" id=\"imgTagElem\">\n<p>                    <script type=\"text\/javascript\" src=\"http:\/\/api.recaptcha.net\/challenge?k=6LfoP8wSAAAAAJHAw37OAAdm56OaVpgHvqHZv1dd\"><![CDATA[\n]]><\/script><noscript><br \/>\n          <iframe loading=\"lazy\" src=\"http:\/\/api.recaptcha.net\/noscript?k=6LfoP8wSAAAAAJHAw37OAAdm56OaVpgHvqHZv1dd\" height=\"300\" width=\"500\" frameborder=\"0\"\/><br \/><textarea name=\"recaptcha_challenge_field\" rows=\"3\" cols=\"40\"\/><input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\"\/><\/noscript>                        <\/p><\/div>\n<div id=\"recaptcha\"><\/div>\n<p>                <input type=\"hidden\" value=\"11032202083023848\" id=\"comment_hash\" name=\"hash\"\/><\/p>\n<p\/>\n<p><\/p>\n<div style=\"float:left; margin-left:20px;\">\n                    <input type=\"image\" id=\"commentSubmit\" src=\"..\/img\/img_trans.gif\" value=\"Submit\" alt=\"Submit\"\/><\/div>\n<p><\/div>\n<\/p><\/form>\n<p>        <script type=\"text\/javascript\"><![CDATA[\n        function setReply (parentKey)\n        {\n            document.getElementById(\"parentKey\").value= parentKey;\n        }\n        \n]]><\/script><\/div>\n<p>\n        <a name=\"nav\">\u00a0<\/a><br \/>\n        <input type=\"hidden\" value=\"1\" id=\"comment_curpage\" name=\"comment_curpage\"\/><\/p>\n<\/div>\n<p><!-- END COMMENTING CODE --><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Geno Timlin defines what VO&#8217;s and DAO&#8217;s are in detail, then goes on to explain how and when to call on each one.\u00c2\u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1533","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1533"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1533\/revisions"}],"predecessor-version":[{"id":3363,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1533\/revisions\/3363"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}