{"id":3253,"date":"2014-05-22T18:55:44","date_gmt":"2014-05-22T18:55:44","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2014\/05\/22\/add-new-model\/"},"modified":"2023-06-07T14:20:56","modified_gmt":"2023-06-07T18:20:56","slug":"add-new-model","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/","title":{"rendered":"How to add the default Model to your Joomla 3 component"},"content":{"rendered":"<h2>How to add a new model<\/h2>\n<ol class=\"article_list\">\n<li><strong>Create the models folder<\/strong>\n<p>If you haven\u2019t already, you will need to create the <strong>models<\/strong> folder. This is where our <em>model<\/em> files will go. In our testing, we created:<br>\n\/components\/com_helloworld\/<span style=\"color: red;\">models<\/span><\/p><\/li>\n<li><strong>Create &lt;view&gt;.php within models folder<\/strong>\n<p>Each component view can have a corresponding model. In this tutorial we are creating a model for our <span style=\"color: red;\">helloworld<\/span> view, so we will be creating<\/p>\n<p>\/components\/com_helloworld\/models\/<span style=\"color: red;\">helloworld.php<\/span><\/p>\n<p>In this file, place the following code:<\/p>\n<pre class=\"code_block\">&lt;?php \/\/ No direct access to this file defined('_JEXEC') or die('Restricted access');   \/\/ import Joomla modelitem library jimport('joomla.application.component.modelitem');   \/**  * HelloWorld Model  *\/ class HelloWorldModel<span style=\"color: red;\">HelloWorld<\/span> extends JModelItem {         \/**          * @var string msg          *\/         protected $msg;           \/**          * Get the message          * @return string The message to be displayed to the user          *\/         public function getMsg()          {                 if (!isset($this-&gt;msg))                  {                         $this-&gt;msg = 'This message has been brought to you by the hello world model getMsg function.';                 }                 return $this-&gt;msg;         } }<\/pre>\n<\/li>\n<li><strong>Update your view.html.php file to call the getMsg() function<\/strong>\n<p>In our <u>components\/com_helloworld\/views\/helloworld\/<span style=\"color: red;\">view.html.php<\/span><\/u> file,<br>\nwe originally created the \u201c<em>Hello World<\/em>\u201d message via the following line:<\/p>\n<pre class=\"code_block\">$this-&gt;msg = '<span style=\"color: orange;\">Hello World<\/span>';<\/pre>\n<p>We will replace this line of code with the following:<\/p>\n<pre class=\"code_block\">$this-&gt;msg = $this-&gt;get('Msg');<\/pre>\n<p>The <em>view.html.php<\/em> file will now look like the following:<\/p>\n<pre class=\"code_block\">&lt;?php \/\/ No direct access to this file defined('_JEXEC') or die('Restricted access');   \/\/ import Joomla view library jimport('joomla.application.component.view');   \/**  * HTML View class for the HelloWorld Component  *\/ class HelloWorldViewHelloWorld extends JViewLegacy {         \/\/ Overwriting JView display method         function display($tpl = null)          {                 \/\/ Assign data to the view                 <span style=\"color: red;\">$this-&gt;msg = $this-&gt;get('Msg');<\/span>                                  \/\/ Display the view                 parent::display($tpl);         } }<\/pre>\n<\/li>\n<\/ol>\n<h2>Our new model in action<\/h2>\n<p>As you can see in the screenshot below, our new model comes into play when our view calls one of the model\u2019s functions.<\/p>\n<table class=\"article_table\">\n<tbody>\n<tr>\n<th>View<\/th>\n<td>When our <strong>view<\/strong> calls<\/td>\n<td style=\"background: #fff; text-align: right;\">$this-&gt;<span style=\"color: red;\">get<\/span>(\u2018<span style=\"color: red; font-weight: bold;\">Msg<\/span>\u2018);<\/td>\n<\/tr>\n<tr>\n<th>Model<\/th>\n<td>\u2026 the following fuction is run in our <strong>model<\/strong>:<\/td>\n<td style=\"background: #fff; text-align: right;\"><span style=\"color: red;\">get<\/span><span style=\"color: red; font-weight: bold;\">Msg<\/span>()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"\/support\/images\/stories\/edu\/joomla-3\/create-component\/tracing-the-new-model.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"max-width: 100%; max-height: 500px;\" src=\"\/support\/images\/stories\/edu\/joomla-3\/create-component\/tracing-the-new-model.png\" alt=\"Add a new model in Joomla 3\"><\/a><\/p>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to add a new model Create the models folder If you haven\u2019t already, you will need to create the models folder. This is where our model files will go. In our testing, we created: \/components\/com_helloworld\/models Create &lt;view&gt;.php within models folder Each component view can have a corresponding model. In this tutorial we are creating<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\"> Read More ><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4403,94],"tags":[4404],"class_list":["post-3253","post","type-post","status-publish","format-standard","hentry","category-joomla","category-joomla-3","tag-joomla-v3"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to add the default Model to your Joomla 3 component | InMotion Hosting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add the default Model to your Joomla 3 component | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"How to add a new model Create the models folder If you haven\u2019t already, you will need to create the models folder. This is where our model files will go. In our testing, we created: \/components\/com_helloworld\/models Create &lt;view&gt;.php within models folder Each component view can have a corresponding model. In this tutorial we are creating Read More &gt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-05-22T18:55:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-07T18:20:56+00:00\" \/>\n<meta name=\"author\" content=\"Brad Markle\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brad Markle\" \/>\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:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\"},\"author\":{\"name\":\"Brad Markle\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\"},\"headline\":\"How to add the default Model to your Joomla 3 component\",\"datePublished\":\"2014-05-22T18:55:44+00:00\",\"dateModified\":\"2023-06-07T18:20:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\"},\"wordCount\":199,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"keywords\":[\"Joomla v3\"],\"articleSection\":[\"Joomla\",\"Joomla 3\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\",\"name\":\"How to add the default Model to your Joomla 3 component | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2014-05-22T18:55:44+00:00\",\"dateModified\":\"2023-06-07T18:20:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add the default Model to your Joomla 3 component\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\",\"name\":\"Brad Markle\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add the default Model to your Joomla 3 component | InMotion Hosting","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:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/","og_locale":"en_US","og_type":"article","og_title":"How to add the default Model to your Joomla 3 component | InMotion Hosting","og_description":"How to add a new model Create the models folder If you haven\u2019t already, you will need to create the models folder. This is where our model files will go. In our testing, we created: \/components\/com_helloworld\/models Create &lt;view&gt;.php within models folder Each component view can have a corresponding model. In this tutorial we are creating Read More >","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2014-05-22T18:55:44+00:00","article_modified_time":"2023-06-07T18:20:56+00:00","author":"Brad Markle","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Brad Markle","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/"},"author":{"name":"Brad Markle","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf"},"headline":"How to add the default Model to your Joomla 3 component","datePublished":"2014-05-22T18:55:44+00:00","dateModified":"2023-06-07T18:20:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/"},"wordCount":199,"commentCount":6,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"keywords":["Joomla v3"],"articleSection":["Joomla","Joomla 3"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/","name":"How to add the default Model to your Joomla 3 component | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2014-05-22T18:55:44+00:00","dateModified":"2023-06-07T18:20:56+00:00","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/joomla\/joomla-3\/add-new-model\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to add the default Model to your Joomla 3 component"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf","name":"Brad Markle","url":"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=3253"}],"version-history":[{"count":4,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3253\/revisions"}],"predecessor-version":[{"id":104827,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3253\/revisions\/104827"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=3253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=3253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=3253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}