How to create categories in custom code

More
2 years 2 months ago - 2 years 2 months ago #61681 by oleg_kosarev
i'm creating category and items from api. her is function in add new cat
function addNewCat($mainCat, $lang, $aliasCat, $titleCat) {
// Set the extension. For content categories, use 'com_content'
$extension = 'com_content';
// Set the title for the category
$title     = $titleCat;
// Type the description, this is also the 'body'. HTML allowed here.
$desc      = '';
// Set the parent category. 1 is the root item.
$parent_id = $mainCat;

// JTableCategory is autoloaded in J! 3.0, so...
if (version_compare(JVERSION, '3.0', 'lt'))
{
   JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
}

// Initialize a new category
$category = JTable::getInstance('Category');
$category->extension = $extension;
$category->title = $title;
$category->alias=$aliasCat;
$category->description = $desc;
$category->published = 1;
$category->access = 1;
$category->params = '{"target":"","image":""}';
$category->metadata = '{"page_title":"","author":"","robots":""}';
$category->language = $lang;

// Set the location in the tree
$category->setLocation($parent_id, 'last-child');

// Check to make sure our data is valid
if (!$category->check())
{
   JError::raiseNotice(500, $category->getError());
   return false;
}

// Now store the category
if (!$category->store(true))
{
   JError::raiseNotice(500, $category->getError());
   return false;
}
// Build the path for our category
$status = $category->rebuildPath($category->id);
    if (!$status) {
               return $category->id;
    } else {
 return $status;
    }
}

All cat create suceful but if open in admin panel error
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY level ASC' at line 1 SQL=SELECT id, params FROM #__categories WHERE id IN () ORDER BY level ASC

What error and what fix?
Last edit: 2 years 2 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago #61685 by ggppdk
Hello

you can create a category the same way that you create a Joomla article category
- just if it is top level category then when you login in backend and visit backend FLEXIcontent UI
you will get a message "update permissions" message, just click to update


-- FLEXIcontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star review. Thanks!

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago #61690 by oleg_kosarev
ok but what create corectory category?
include "\administrator\components\com_flexicontent\models\category.php" and call save($data)?

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago - 2 years 2 months ago #61693 by ggppdk
hello

something like this should work:
// REQUIRE some files
if (!defined('DS'))  define('DS',DIRECTORY_SEPARATOR);
require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'defineconstants.php');
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'tables');
require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'classes'.DS.'flexicontent.fields.php');
require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'classes'.DS.'flexicontent.helper.php');
require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'helpers'.DS.'permission.php');
require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'models'.DS.'category.php');

// Create an instance of the category model
$catModel = new FlexicontentModelCategory();

// Create the data
$data = array();
$data['cid'] = 0;
...

// Save the new category
$catModel ->save($data);

To load a category use:
$id = 44;
$catModel ->setId($id);
$category = $catModel->getCategory();


-- FLEXIcontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star review. Thanks!
Last edit: 2 years 2 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
© 2018 Flexicontent. All Rights Reserved.