开发者

Programmatically adding an article to Joomla

I am very new to Joomla (frankly just started exploring the possibility of using Joomla) and need help with programmatically adding articles to Joomla backend tables (please see details below). Also along the same lines, I would like to understand how should values for the columns:

  • parent_id
  • lft
  • rgt
  • level

be generated for the table jos_assets (#__assets) and what is their functional role (eg are they “pointers/indexes” analogous to, say, an os inode to uniquely indentify a file or are they more functional attributes such as identifying the category, subcategory etc)

It might help to use the following SIMPLIFIED example to illustrate what I am trying to do. Say we have a program that collects various key information such as names of the authors of web articles, the subject type of the articles, the date of articles as well as a link to the article. I want to be able to extend this program to programmatically store this information in Joomla. Currently this information is stored in a custom table and the user, through a custom php web page, can use search criteria say by author name, over a certain range of dates to find the article(s) of interest. The result of this search is then displayed along with a hyperlink to the actual article. The articles 开发者_如何转开发are stored locally on the web server and are not external links. The portion of the hyperlink stored in the custom table includes the relative path of the physical document (relative to the web root), so for example:

Author   date         type    html_file
Tom      08-14-2011   WEB     /tech/11200/ar_324.html
Jim      05-20-2010   IND     /tech/42350/ar_985.html

etc.

With all the advantages that Joomla offers over writing custom php search and presentation pages as well as trending etc, we would really like to switch to it. It seems that among other tables for example that #__assets and #__content can be populated programmatically to populate Joomla from our existing php program (which is used to compile the data) and then use Joomla.

Any examples, suggestions and help is greatly appreciated

Kindest regards Gar


Just an initial note: Joomla 1.6/1.7 are pretty similar. 1.5 not so much. I'll assume 1.6/1.7, as that's what I'd recommend as a base for a new project.

First up, you'll need to be running with access to the Joomla framework. You could do this through a Component, or a module, or a cron that bootstraps it or whatever. I won't go though how to do that.

But once you do that, creating an article is reasonably simple.

<?php
require_once JPATH_ADMINISTRATOR . '/components/com_content/models/article.php';

$new_article = new ContentModelArticle();

$data = array(
    'catid' => CATEGORY_ID,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);
$new_article->save($data);

The actual list of fields will be a bit longer than that (required fields etc), but you should get sane error messages etc from the Joomla framework which illuminate that.

So in summary:

  • Load up the Joomla framework so you have access to the DB, components, models, etc
  • Include the com_content article class, which will handle validation, saving to the database etc for you
  • Create an article instance with the required fields filled in as appropriate
  • Call save()

Now that I think about it, that'll probably work in 1.5...


Found a better way to do this without any errors Create a Joomla! Article Programatically

$table = JTable::getInstance('Content', 'JTable', array());

$data = array(
    'catid' => 1,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);

// Bind data
if (!$table->bind($data))
{
    $this->setError($table->getError());
    return false;
}

// Check the data.
if (!$table->check())
{
    $this->setError($table->getError());
    return false;
}

// Store the data.
if (!$table->store())
{
    $this->setError($table->getError());
    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜