0

Hi I need some help creating a category from inside an upgrade method for a custom component.

This is what I have tried to do, any help will be great!

// start up the DB
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Select all fields
$query->select($db->quoteName(array('a.category_name','a.desc','a.published'),array('title','description','published')));
$query->from($db->quoteName('jos_oldcomponent_categories', 'a'));
$query->where($db->quoteName('a.id') . ' = '. (int) $id);
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
    // get the category
    $item = $db->loadAssoc();
    $item['id'] = 0;
    $item['alias'] = $this->getAlias($item['title']);
    $item['path'] = $item['alias'];
    $item['access'] = 1;
    $item['created_user_id'] = 50;
    $item['created_time'] = '2012-09-18 10:09:09';
    $item['version'] = 1;
    $item['language'] = "*";
    $item['parent_id'] = 1;
    $item['level'] = 1;
    $item['extension'] = 'com_newcomponent';
    // load some joomla helpers
    JLoader::import('joomla.application.component.model');
    // load the model file
    JLoader::import( 'category', JPATH_ADMINISTRATOR.'/components/com_categories/models' );
    // return instance
    $model = JModelLegacy::getInstance( 'category', 'CategoriesModel' );
    // now save the data
    $model->save($item);
}

But this does not work, it seems like the model does not see the table.

1

In my installer script of DPCalendar I'm using the following code to create a default category:

        JTable::addIncludePath(JPATH_LIBRARIES . '/joomla/database/table');
        $category = JTable::getInstance('Category');
        $category->extension = 'com_dpcalendar';
        $category->title = 'Uncategorised';
        $category->alias = 'uncategorised';
        $category->description = '';
        $category->published = 1;
        $category->access = 1;
        $category->params = '{"category_layout":"","image":"","color":"3366CC"}';
        $category->metadata = '{"author":"","robots":""}';
        $category->language = '*';
        $category->setLocation(1, 'last-child');
        $category->store(true);
        $category->rebuildPath($category->id);

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.