{"id":7234,"date":"2023-12-14T06:05:41","date_gmt":"2023-12-14T06:05:41","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=7234"},"modified":"2023-12-14T06:05:41","modified_gmt":"2023-12-14T06:05:41","slug":"explain-the-active-record-pattern-with-an-example-in-php","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/","title":{"rendered":"Explain the Active Record pattern with an example in PHP"},"content":{"rendered":"<p>The <strong>Active Record<\/strong> pattern is an architectural design pattern commonly used in PHP for interacting with databases. It provides a simple and intuitive way to represent database tables as objects and allows developers to perform CRUD (Create, Read, Update, Delete) operations on those objects.<br \/>\nIn the Active Record pattern, each database table is typically represented by a corresponding PHP class, referred to as an <strong>&#8220;Active Record&#8221;<\/strong> class. Each instance of an Active Record class represents a single row in the database table.<\/p>\n<p>Here&#8217;s an example to illustrate the Active Record pattern in <strong>PHP<\/strong>:<br \/>\n<code>class User<br \/>\n{<br \/>\n\tprivate $id;<br \/>\n\tprivate $name;<br \/>\n\tprivate $email;<br \/>\n\tpublic function __construct($id, $name, $email)<br \/>\n\t{<br \/>\n\t\t$this->id = $id;<br \/>\n\t\t$this->name = $name;<br \/>\n\t\t$this->email = $email;<br \/>\n\t}<br \/>\n\tpublic function getId()<br \/>\n\t{<br \/>\n\t\treturn $this->id;<br \/>\n\t}<br \/>\n\tpublic function getName()<br \/>\n\t{<br \/>\n\t\treturn $this->name;<br \/>\n\t}<br \/>\n\tpublic function getEmail()<br \/>\n\t{<br \/>\n\t\treturn $this->email;<br \/>\n\t}<br \/>\npublic function setName($name)<br \/>\n\t{<br \/>\n\t\t$this->name = $name;<br \/>\n\t}<br \/>\n\tpublic function setEmail($email)<br \/>\n\t{<br \/>\n\t\t$this->email = $email;<br \/>\n\t}<br \/>\n\tpublic function save()<br \/>\n\t{<br \/>\n\t\t\/\/ Implementation to save\/update the record in the database<br \/>\n\t}<br \/>\n\tpublic function delete()<br \/>\n\t{<br \/>\n\t\t\/\/ Implementation to delete the record from the database<br \/>\n\t}<br \/>\n}<br \/>\n\/\/ Usage example:<br \/>\n$user = new User(1, \"John Doe\", \"john@example.com\");<br \/>\n$user->setName(\"Jane Doe\");<br \/>\n$user->save(); \t\t\/\/ Saves or update record to the database<br \/>\n$user2 = new User(2, \"Alice Smith\", \"alice@example.com\");<br \/>\n$user2->delete(); \t\/\/ Deletes the record from the database<\/code><\/p>\n<p>In this example, the User class represents the &#8220;users&#8221; table in the database. It has private properties for id, name, and email, which correspond to the columns in the table.<br \/>\nThe class provides getter and setter methods for accessing and modifying the properties. The save() method is responsible for saving the current state of the object to the database or updating an existing record, while the delete() method deletes the corresponding record from the database.<br \/>\nTo use the Active Record pattern, you can create instances of the User class, manipulate their properties, and call the save() method to persist changes to the database or the delete() method to remove the record.<br \/>\nThe Active Record pattern simplifies the database interaction code by encapsulating database operations within the Active Record objects themselves, making it more convenient to work with and maintain database records in PHP applications.<\/p>\n<p><small><em>Being the most extensively used JavaScript library, a <a href=\"https:\/\/www.studysection.com\/jquery-advanced\">jQuery Certification<\/a> will add enormous value to your skill-set. jQuery provides various functionalities to the developer in order to develop complex applications with ease and efficiency.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases. It provides a<\/p>\n","protected":false},"author":1,"featured_media":7235,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[200,901],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Explain the Active Record pattern with example in PHP - SS Blog<\/title>\n<meta name=\"description\" content=\"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.\" \/>\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-the-active-record-pattern-with-an-example-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explain the Active Record pattern with example in PHP - SS Blog\" \/>\n<meta property=\"og:description\" content=\"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\" \/>\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-12-14T06:05:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/12\/PHP1.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<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Explain the Active Record pattern with an example in PHP\",\"datePublished\":\"2023-12-14T06:05:41+00:00\",\"dateModified\":\"2023-12-14T06:05:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\"},\"wordCount\":281,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"php\",\"Record\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\",\"url\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\",\"name\":\"Explain the Active Record pattern with example in PHP - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2023-12-14T06:05:41+00:00\",\"dateModified\":\"2023-12-14T06:05:41+00:00\",\"description\":\"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explain the Active Record pattern with an example in PHP\"}]},{\"@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":"Explain the Active Record pattern with example in PHP - SS Blog","description":"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.","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-the-active-record-pattern-with-an-example-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Explain the Active Record pattern with example in PHP - SS Blog","og_description":"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.","og_url":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2023-12-14T06:05:41+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/12\/PHP1.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"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Explain the Active Record pattern with an example in PHP","datePublished":"2023-12-14T06:05:41+00:00","dateModified":"2023-12-14T06:05:41+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/"},"wordCount":281,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["php","Record"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/","url":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/","name":"Explain the Active Record pattern with example in PHP - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2023-12-14T06:05:41+00:00","dateModified":"2023-12-14T06:05:41+00:00","description":"The Active Record pattern is an architectural design pattern commonly used in PHP for interacting with databases.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/explain-the-active-record-pattern-with-an-example-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Explain the Active Record pattern with an example in PHP"}]},{"@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":220,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7234"}],"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=7234"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7234\/revisions"}],"predecessor-version":[{"id":7237,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7234\/revisions\/7237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/7235"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=7234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=7234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=7234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}