Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Model view controller Joomla 3组件MVC模型,将文章直接添加到数据库中_Model View Controller_Joomla_Joomla Extensions - Fatal编程技术网

Model view controller Joomla 3组件MVC模型,将文章直接添加到数据库中

Model view controller Joomla 3组件MVC模型,将文章直接添加到数据库中,model-view-controller,joomla,joomla-extensions,Model View Controller,Joomla,Joomla Extensions,无论是使用MVC还是创建Joomla组件,我都是初学者。我需要创建一个组件来读取包含文章数据的上传CSV文件,并将它们直接添加到数据库中 到目前为止,我只创建了一个赤裸裸的组件,它使用了将一篇文章直接添加到我找到的数据库的示例,并且我尝试使用MVC结构来实现它,但是我不确定我是否正确地执行了它 该组件基于如何创建基本组件教程中的com_helloworld组件,我将解释其结构,然后希望有人能告诉我我所做的是否合理 因此,当我从Joomla中的adminstrator面板打开该组件时,它将转到中的

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

到目前为止,我只创建了一个赤裸裸的组件,它使用了将一篇文章直接添加到我找到的数据库的示例,并且我尝试使用MVC结构来实现它,但是我不确定我是否正确地执行了它

该组件基于如何创建基本组件教程中的com_helloworld组件,我将解释其结构,然后希望有人能告诉我我所做的是否合理

因此,当我从Joomla中的adminstrator面板打开该组件时,它将转到中的默认视图

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>
换句话说,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;
    }

}
这就是本文实际添加到数据库的地方,这基本上解释了整个组件

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


这可能是一个非常基本的问题,我一直在尝试自己解决这个问题,但我还没有找到这个特殊情况的示例。

我会跳过表,将addArticle函数添加到模型中,因为整个逻辑在模型中处理,而不是在表中处理。但基本上这是开始的方式。当你在几个月甚至几年后回首往事时,你会做出不同的改变。谢谢你,我同意并实施了这些改变!
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.";
        }
    }

}
com_helloworld/admin/tables
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;
    }

}