Joomla 3组件MVC模型,直接向数据库添加文章

时间:2015-03-28 13:21:06

标签: model-view-controller joomla joomla-extensions

在使用MVC和创建Joomla组件时,我都是初学者。我需要创建一个组件来读取包含文章数据的上传CSV文件,并将它们直接添加到数据库中。

到目前为止,我只创建了一个准系统组件,该组件使用直接向我找到here的数据库添加文章的示例,并且我尝试使用MVC结构实现它,但我'我不确定我是否正确地做到了。

该组件基于如何创建基本组件的教程中的com_helloworld组件,我将解释结构,然后希望有人可以告诉我我正在做的事情是否合理。

因此,当我从Joomla的管理员面板中打开组件时,它会转到

中的默认视图
com_helloworld/admin/views/helloworld 

view.html.php从相应的模型中加载任何内容,只需在

中打开default.php
com_helloworld/admin/views/helloworld/tmpl

default.php包含:

<?php?>
<a href="index.php?option=com_helloworld&view=jimport">Add an article</a>

因此,单击该链接将打开视图jimport,其中包含以下view.html.php:

<?php
defined('_JEXEC') or die('Restricted access');
class HelloWorldViewJImport extends JViewLegacy
{
        function display($tpl = null)
        {
                // Get status from the model
                $this->status=$this->get('Status');
                parent::display($tpl);
        }
}

以及tmpl文件夹中的以下default.php:

<?php echo($this->status);

view jimport从com_helloworld / admin / models / jimport.php中的相应模型调用函数getStatus(),其中包含以下代码:

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 to the database through a component';
        $catid=2;
        if ($table->addArticle($title, $alias, $introtext, $catid)) {
            return "The article has been added.";
        } else {
            return "There was an error.";
        }
    }

}

换句话说,getStatus尝试将文章添加到数据库并返回一个字符串,告诉它是否成功。

现在这个函数从

中的JTable实现中获取一个实例
com_helloworld/admin/tables

包含jimport.php:

class HelloWorldTableJImport extends JTable {

    /**
     * Constructor
     *
     * @param   JDatabaseDriver  &$db  A 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 to make sure our data is valid, raise notice if it's not.
        if (!$article->check()) {
            JError::raiseNotice(500, $article->getError());
            return FALSE;
        }

        // Now store the article, raise notice if it doesn't get 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;
    }

}

这是文章实际添加到数据库的地方,几乎解释了整个组件。

我想知道这是否是一种合理的方法。我不确定这是否应该从模型中做,或者是否应该通过控制器添加文章。

这可能是一个非常基本的问题,我一直试图自己解决这个问题,但我还没有真正找到这个特例的例子。

0 个答案:

没有答案