【问题标题】:How to add categories in Joomla 2.5如何在 Joomla 2.5 中添加类别
【发布时间】:2013-02-21 13:51:45
【问题描述】:

有谁知道如何以编程方式将新内容类别正确插入数据库? 对于 category 表中的每个帖子,在 assets 表中还保存了一个设置了 lft 和 rgt 的帖子。 有没有我可以使用的本地 Joomla 类来代替普通的 SQL?

【问题讨论】:

    标签: joomla joomla2.5


    【解决方案1】:

    请仅使用本地类,哪些类别将为您无缝处理。一旦您添加类别,整个事情就会自动处理。只需查看任何核心组件即可。

    使用 sql 更新 assets 表并不容易,它都是非常专门管理的,并且是一系列复杂的外键表的一部分。

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

    【讨论】:

      【解决方案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
      }
      

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

      【讨论】:

        【解决方案3】:

        这是我专门为此目的创建的一个函数,经过一些挖掘和实验。

        它使用核心类,因此需要访问它们(对我来说,它基本上是 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;
        }
        

        【讨论】:

          【解决方案4】:

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

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

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

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

          如果您在以编程方式创建类别时不更新此表,则将应用默认 ACL。当您稍后在管理面板中打开并保存类别时,ACL 将被更新,因为它应该由核心 Joomla! 更新。

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

          【讨论】:

          • 请不要这样做。永远不要直接接触资产表,而只能使用具有资产字段的表进行修改。管理嵌套集一点也不简单。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-16
          • 1970-01-01
          • 2023-03-16
          • 2014-03-11
          • 2013-07-08
          • 1970-01-01
          • 2012-06-30
          相关资源
          最近更新 更多