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
如何在Joomla 2.5中添加类别_Joomla_Joomla2.5 - Fatal编程技术网

如何在Joomla 2.5中添加类别

如何在Joomla 2.5中添加类别,joomla,joomla2.5,Joomla,Joomla2.5,有人知道如何通过编程方式将新内容类别正确插入数据库吗? 对于categories表中的每个帖子,assets表中还保存了一个设置了lft和rgt的帖子。 是否有任何本机Joomla类可以用来代替普通SQL?您可能可以使用category.php文件中的save() 文件位置:root\administrator\components\com\u categories\models\category.php 它保存提供给它的表单数据 JOS_assets表用于存储所创建的每个资产的ACL 如果在以

有人知道如何通过编程方式将新内容类别正确插入数据库吗? 对于categories表中的每个帖子,assets表中还保存了一个设置了lft和rgt的帖子。
是否有任何本机Joomla类可以用来代替普通SQL?

您可能可以使用category.php文件中的save()

文件位置:
root\administrator\components\com\u categories\models\category.php

它保存提供给它的表单数据

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

如果在以编程方式创建类别时不更新此表,则将应用默认ACL。稍后,当您在管理面板中打开并保存该类别时,ACL将按core Joomla!的原样进行更新


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

请只使用本机类,这些类将无缝地为您处理。一旦你添加类别,整个事情将自动处理。只要看看任何核心组件就知道了

使用sql更新资产表并不容易,它都是经过专门管理的,是一系列复杂的外键表的一部分


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

经过一些挖掘和实验后,我为这个目的创建了一个函数

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

请注意,这是针对Joomla 3、Joomla 2.5及之前版本的,您需要将JModelLegacy更改为JModel

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;
}

这里是我刚刚拼凑的一些代码,它们只使用了
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
}

当然,您希望正确设置数据块,但这些是要设置的核心数据块。

请不要这样做。决不能直接接触资产表,而只能使用具有资产字段的表进行修改。管理嵌套集一点也不简单。