2013-02-21 115 views
2

有谁知道如何以编程方式正确地向数据库插入新的内容类别? 对于类别表中的每个帖子,还有一个帖子保存在资产表中,并且设置了lft和rgt。 是否有任何原生Joomla类可以用来代替普通SQL?如何在Joomla 2.5中添加类别

回答

0

您也许可以在category.php文件中使用save()

文件位置:root\administrator\components\com_categories\models\category.php

它节省了提供给它的表单数据!

JOS_assets表用于存储创建的每个资产的ACL。

如果在编程式创建类别时未更新此表,则将应用默认ACL。之后当您在管理面板中打开并保存该类别时,ACL将会更新,正如核心Joomla!应该已经更新一样。

虽然您可以非常轻松地创建SQL查询并更新资产表。一旦你在phpmyadmin中打开表格的内容就很容易理解。

+2

请不要这样做。应始终不要直接触摸资产表,只能通过使用具有资产字段的表进行修改。管理嵌套集合并非一帆风顺。 – Elin 2013-02-21 22:43:02

2

请仅使用本地类,这些类将为您无缝地处理。只要你添加类别,整个事情将被自动处理。看看任何核心组件,看看如何。

使用sql更新资产表并不容易,它都是非常特别的管理和复杂的外键系列表的一部分。

扩展JTable或JTableContent来处理这个问题。

1

这是我为此创建的一个函数,在一些挖掘&实验之后。

它使用核心类,所以它需要访问它们(对我来说它基本上是Joomla组件的一部分)。

心灵,它是为Joomla 3,为Joomla 2.5之前,您需要更改JModelLegacyJModel

function createCategory($name, $parent_id, $note) 
{ 
    JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_categories/tables'); 

    $cat_model = JModelLegacy::getInstance('Category', 'CategoriesModel'); 

    $data = array (
     'id' => 0, 
     'parent_id' => $parent_id, 
     'extension' => 'com_content', 
     'title' => $name, 
     'alias' => '', 
     'note' => $note, 
     'description' => '', 
     'published' => '1', 
     'access' => '1', 
     'metadesc' => '', 
     'metakey' => '', 
     'created_user_id' => '0', 
     'language' => '*', 
     'rules' => array(
      'core.create' => array(), 
      'core.delete' => array(), 
      'core.edit' => array(), 
      'core.edit.state' => array(), 
      'core.edit.own' => array(), 
     ), 
     'params' => array(
      'category_layout' => '', 
      'image' => '', 
     ), 
     'metadata' => array(
      'author' => '', 
      'robots' => '', 
     ), 
    ); 

    if(!$cat_model->save($data)) 
    { 
     return NULL; 
    } 

    $categories = JCategories::getInstance('Content'); 
    $subcategory = $categories->get($cat_model->getState("category.id")); 
    return $subcategory; 
} 
2

下面是一些代码,我只是鞭打在一起,只是使用JTableCategory类,所以可以简单地用正面或管理方的Joomla

$table = JTable::getInstance('category'); 

$data = array(); 
// name the category 
$data['title'] = $title; 
// set the parent category for the new category 
$data['parent_id'] = $parent_id; 
// set what extension the category is for 
$data['extension'] = $extension; 
// Set the category to be published by default 
$data['published'] = 1; 

// setLocation uses the parent_id and updates the nesting columns correctly 
$table->setLocation($data['parent_id'], 'last-child'); 
// push our data into the table object 
$table->bind($data); 
// some data checks including setting the alias based on the name 
if ($table->check()) { 
    // and store it! 
    $table->store(); 
    // Success 
} else { 
    // Error 
} 

你自然会想要得到的数据片正确设置,但这些是设置的核心部分。