{"id":6752,"date":"2023-05-16T04:42:39","date_gmt":"2023-05-16T04:42:39","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6752"},"modified":"2023-10-16T07:20:42","modified_gmt":"2023-10-16T07:20:42","slug":"explain-this-pattern-with-an-example-in-php-table-data-gateway","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/","title":{"rendered":"Explain this pattern with an example in PHP &#8211; Table Data Gateway"},"content":{"rendered":"<p>A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table. The goal is to keep the task of retrieving items from a database apart from how those objects are actually used. As a result, modifications to the database&#8217;s object storage scheme are protected from users of the gateway.<\/p>\n<p>Now, let us understand how this pattern helps with SQL operations. I am creating a database helper class based on the TDB pattern to perform basic CRUD operations.<\/p>\n<ol>\n<li>\n<h2>Create an AbstractTableGateway class:<\/h2>\n<p><code>&lt;?php<br \/>\nabstract class AbstractTableGateway<br \/>\n{<br \/>\nprotected $db;<br \/>\nprotected $tablename;<br \/>\nprotected $columns;<br \/>\nprotected $primary = 'id';<br \/>\npublic function __construct(\\PDO $db)<br \/>\n{<br \/>\n$this-&gt;db = $db;<br \/>\n}<br \/>\npublic function listallrecordsBySQL(string $sql, array $params): array<br \/>\n{<br \/>\n$stmt = $this-&gt;db-&gt;prepare($sql);<br \/>\n$stmt-&gt;execute($params);<br \/>\nreturn $stmt-&gt;fetchAll();<br \/>\n}<br \/>\npublic function getRecordbyID($id): ?array<br \/>\n{<br \/>\n$ret = $this-&gt;listallrecordsBySQL(\"SELECT * FROM $this-&gt;tablename WHERE $this-&gt;primary=?\", [$id]);<br \/>\nreturn $ret ? reset($ret) : null;<br \/>\n}<br \/>\npublic function updateRecordbyID(array $data, int $id): void<br \/>\n{<br \/>\n$this-&gt;validate($data);<br \/>\n$params = [];<br \/>\n$set = \"\";<br \/>\nforeach($data as $key =&gt; $value)<br \/>\n{<br \/>\n$set .= \"\u2019$key\u2019 = ?,\";<br \/>\n$params[] = $value;<br \/>\n}<br \/>\n$set = rtrim($set, \",\");<br \/>\n$params[] = $id;<br \/>\n$sql = \"UPDATE $this-&gt;tablename SET $set WHERE $this-&gt;primary=?\";<br \/>\n$this-&gt;db-&gt;prepare($sql)-&gt;execute($params);<br \/>\n}<br \/>\npublic function insertData($data): int<br \/>\n{<br \/>\n$this-&gt;validate($data);<br \/>\n$columns = \"'\".implode(\"\u2019,\u2019\", array_keys($data)).\"\u2019\";<br \/>\n$placeholders = str_repeat('?,', count($data) - 1) . '?';<br \/>\n$sql = \"INSERT INTO $this-&gt;tablename ($columns) VALUES ($placeholders)\";<br \/>\n$this-&gt;db-&gt;prepare($sql)-&gt;execute(array_values($data));<br \/>\nreturn $this-&gt;db-&gt;lastInsertId();<br \/>\n}<br \/>\npublic function delete($id)<br \/>\n{<br \/>\n$sql = \"DELETE FROM $this-&gt;tablename WHERE $this-&gt;primary=?\";<br \/>\n$this-&gt;db-&gt;prepare($sql)-&gt;execute([$id]);<br \/>\n}<br \/>\nprotected function validate($data)<br \/>\n{<br \/>\n$diff = array_diff(array_keys($data), $this-&gt;columns);<br \/>\nif ($diff) {<br \/>\nthrow new \\InvalidArgumentException(\"Unknown field(s): \". implode($diff));<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/li>\n<li><strong>Now we create a child class named as \u201cUserTableGateway\u201d where we first set the properties of the \u201cAbstractTableGateway\u201d class as below:<\/strong><br \/>\n<code>&lt;?php<br \/>\nclass UserTableGateway extends AbstractTableGateway {<br \/>\nprotected $tablename = 'users';<br \/>\nprotected $columns = ['name', 'email', 'password'];<br \/>\n}<\/code><\/li>\n<li><strong>Now, we are going to call the AbstractTableGateway class functions to perform the CRUD operations: <\/strong><br \/>\n<code>&lt;?php<br \/>\n$data = [<br \/>\n'name' =&gt; 'Test User',<br \/>\n'email' =&gt; 'testuser@gmail.com',<br \/>\n'password' =&gt; 'hashed'<br \/>\n];<br \/>\n$id = $UserTableGateway-&gt;insertData($data); \/\/ Insert the record<br \/>\n$user_record = $UserTableGateway-&gt;getRecordbyID($id); \/\/Get record by ID<br \/>\nprint_r( json_encode($user_record));<br \/>\n$UserTableGateway-&gt;update(['name' =&gt; 'Test Account'], $id); \/\/Update the name from \u201cTest User\u201d to \u201cTest Account\u2019<br \/>\n$user_record = $UserTableGateway-&gt;getRecordbyID($id);  \/\/Get record by ID<br \/>\nprint_r( json_encode($user_record));<br \/>\n$UserTableGateway-&gt;delete($id); \/\/Delete the \u201cTest Account\u201d record<\/code><\/li>\n<\/ol>\n<p><small><em>Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification exam conducted by StudySection. After going through this <a href=\"https:\/\/www.studysection.com\/computer-applications-diploma-advanced\">Computer Certification Exam<\/a>, you will be able to evaluate your basic knowledge of computers.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table. The<\/p>\n","protected":false},"author":1,"featured_media":6753,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[792,200],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Table Data Gateway - StudySection Blog<\/title>\n<meta name=\"description\" content=\"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Table Data Gateway - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-16T04:42:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-16T07:20:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/05\/Gateway1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Explain this pattern with an example in PHP &#8211; Table Data Gateway\",\"datePublished\":\"2023-05-16T04:42:39+00:00\",\"dateModified\":\"2023-10-16T07:20:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\"},\"wordCount\":175,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Gateway\",\"php\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\",\"url\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\",\"name\":\"Table Data Gateway - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2023-05-16T04:42:39+00:00\",\"dateModified\":\"2023-10-16T07:20:42+00:00\",\"description\":\"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explain this pattern with an example in PHP &#8211; Table Data Gateway\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Table Data Gateway - StudySection Blog","description":"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/","og_locale":"en_US","og_type":"article","og_title":"Table Data Gateway - StudySection Blog","og_description":"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.","og_url":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2023-05-16T04:42:39+00:00","article_modified_time":"2023-10-16T07:20:42+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/05\/Gateway1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Explain this pattern with an example in PHP &#8211; Table Data Gateway","datePublished":"2023-05-16T04:42:39+00:00","dateModified":"2023-10-16T07:20:42+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/"},"wordCount":175,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Gateway","php"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/","url":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/","name":"Table Data Gateway - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2023-05-16T04:42:39+00:00","dateModified":"2023-10-16T07:20:42+00:00","description":"A design pattern called Table Data Gateway uses an object to serve as a gateway to a database table in php.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/explain-this-pattern-with-an-example-in-php-table-data-gateway\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Explain this pattern with an example in PHP &#8211; Table Data Gateway"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":166,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6752"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=6752"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6752\/revisions"}],"predecessor-version":[{"id":7098,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6752\/revisions\/7098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6753"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}