Joomla 3 component MVC model, adding article directly to the database -
i beginner both when comes using mvc , creating joomla components. need create component reads uploaded csv-file contains article data, , add them directly database.
so far i've created barebone component uses example of adding article directly database found here, , i've tried implement using mvc structure, i'm not sure if i'm doing correctly.
the component based on com_helloworld component tutorial on how create basic component, explain structure, , can tell me whether i'm doing seems reasonable or not.
so when open component adminstrator panel in joomla, goes default view in
com_helloworld/admin/views/helloworld
view.html.php loads nothing corresponding model, , opens default.php in
com_helloworld/admin/views/helloworld/tmpl
default.php contains:
<?php?> <a href="index.php?option=com_helloworld&view=jimport">add article</a>
so clicking link opens view jimport, has following view.html.php:
<?php defined('_jexec') or die('restricted access'); class helloworldviewjimport extends jviewlegacy { function display($tpl = null) { // status model $this->status=$this->get('status'); parent::display($tpl); } }
and following default.php in tmpl folder:
<?php echo($this->status);
the view jimport calls function getstatus() corresponding model in com_helloworld/admin/models/jimport.php, contains following code:
class helloworldmodeljimport extends jmodellist { public function getstatus($type = 'jimport', $prefix = 'helloworldtable', $config = array()) { $table = jtable::getinstance($type, $prefix, $config); $title='article'; $alias=$title; $introtext='this article has been added automatically database through component'; $catid=2; if ($table->addarticle($title, $alias, $introtext, $catid)) { return "the article has been added."; } else { return "there error."; } } }
in other words, getstatus tries add article database , returns string telling whether succesful or not.
now function gets instance jtable implementation in
com_helloworld/admin/tables
which contains jimport.php:
class helloworldtablejimport extends jtable { /** * constructor * * @param jdatabasedriver &$db database connector object */ function __construct(&$db) { } public function addarticle($title, $alias, $introtext, $catid) { if (version_compare(jversion, '3.0', 'lt')) { addincludepath(jpath_platform . 'joomla/database/table'); } $article = $this->createarticle($title, $alias, $introtext, $catid); // check make sure our data valid, raise notice if it's not. if (!$article->check()) { jerror::raisenotice(500, $article->geterror()); return false; } // store article, raise notice if doesn't stored. if (!$article->store(true)) { jerror::raisenotice(500, $article->geterror()); return false; } return true; } private function createarticle($title, $alias, $introtext, $catid) { $article = jtable::getinstance('content'); $article->title = $title; $article->alias = jfilteroutput::stringurlsafe($alias); $article->introtext = $introtext; $article->catid = $catid; $article->created = jfactory::getdate()->tosql(); ; $article->created_by_alias = 'super user'; $article->state = 1; $article->access = 1; $article->metadata = '{"page_title":"","author":"","robots":""}'; $article->language = '*'; return $article; } }
this article added database, , pretty explains entire component.
i know if seems reasonable way this. i'm not sure if should model, or if article should added through controller.
this basic question, , have been trying figure out on own, haven't found example particular case.
Comments
Post a Comment