Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created July 30, 2012 22:53
Show Gist options
  • Select an option

  • Save mbabker/3211464 to your computer and use it in GitHub Desktop.

Select an option

Save mbabker/3211464 to your computer and use it in GitHub Desktop.
Creating a category via component postflight
// Get the database object
$db = JFactory::getDbo();
// 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 = 'com_myextension';
$category->title = 'My Category';
$category->description = 'A category for my extension';
$category->published = 1;
$category->access = 1;
$category->params = '{"target":"","image":""}';
$category->metadata = '{"page_title":"","author":"","robots":""}';
$category->language = '*';
// Set the location in the tree
$category->setLocation(1, '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
$category->rebuildPath($category->id);
@laoneo

laoneo commented Jul 31, 2012

Copy link
Copy Markdown

To load the category I needed to add the following code:

JTable::addIncludePath(JPATH_LIBRARIES.DS.'joomla'.DS.'database'.DS.'table');
$category = JTable::getInstance('Category');

@elinw

elinw commented Jul 31, 2012

Copy link
Copy Markdown

We're discouraging use of DS so use slashes instead.

@mbabker

mbabker commented Jul 31, 2012

Copy link
Copy Markdown
Author

Updated to add the include path for pre-3.0 (it'll autoload in 3.0).

@laoneo

laoneo commented Jul 31, 2012

Copy link
Copy Markdown

I need also to add $category->alias= 'my-alias';

@mbabker

mbabker commented Jul 31, 2012

Copy link
Copy Markdown
Author

You can use it if you want to specify the alias, otherwise $category->check() will automatically generate the alias.

@tdzweb

tdzweb commented Aug 1, 2012

Copy link
Copy Markdown

Exactly how does the tree placement work? Will $category->setLocation(1, 'last-child'); just add it as the last child of the root category?

@mbabker

mbabker commented Aug 1, 2012

Copy link
Copy Markdown
Author

Correct. For a new component, this should be the default action. I have some other code saved that will set the location as the last child of another category, but you have to do some other lookups to get the ID for that category.

@tdzweb

tdzweb commented Aug 1, 2012

Copy link
Copy Markdown

I see. Is '1' always the root category? it occurs to me that JTableNested has a method called getRootId for a reason.

@mbabker

mbabker commented Aug 4, 2012

Copy link
Copy Markdown
Author

1 should always be the ID of the root category, at least in the CMS. You have to know the ID of the parent item you want to be a child of, so I can hard code it to 1 for CMS instances and be OK. Using getRootId should work also if you need it to.

@ckwillling

Copy link
Copy Markdown

Is this ok for joomla2.5? Because i copy it and It din't create any new category

@betweenbrain

Copy link
Copy Markdown

Just wondering why you have $db = JFactory::getDbo(); in this?

@chhrahul

chhrahul commented May 24, 2021

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment