1283 Posts
Bucklash
8 years ago
Topic

Hi Klas

Can you confirm if I am able to create categories dynamically using Seblod please?

I am able to do so with articles, but categories has a bit more going on that complicates it (nested stuff).

I wrote this ages ago but it does not apply to categories...

create-content-dynamiclly-using-code

These links offer Joomla! solutions (though not filling the rules in my assets table...)

Creating a category via component postflight

how to add category programmatic?

JTableCategory::store fails if $updateNulls=true

From 1st link...

// 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);<br>
Get a Book for SEBLOD
4229 Posts
Kadministrator
8 years ago
1
Level 1

You should be able to create category using category object in seblod, but you will probably need to use some obligatory additional fields like parent (see category content type)

1283 Posts
Bucklash
8 years ago
0
Level 2

Hi Klas

This seems to be a Joomla 'issue' as the Seblod part is fine...

The errors are with the Joomla bit i.e. 

a) #__assets.rules = empty, 

b) #__categories.parent_id = 0

The only workaround I can think of is to create with Seblod, and then re-order the category using JTable and Nested Sets

    $newCategory = new JCckContent( 
        array('joomla_category') 
    );


    $addCategory = $newCategory->create(
        'test_category',
        array(
            // #__categories
            'id' => '',
            'title' => 'Some Title',
            'alias' => 'some_title',
            'parent_id' => 17,
            'extension' => 'com_content',
            'path' => 'my-path',
            'level' => 1,
            'access' => 1,
        ),
        array(
            // #__cck_store_form_test_category
            'some_padlocked_field' => 'some-value'
        ),
        array(
            // #__cck_store_item_categories
            'some_unpadlocked_field' => 'some-other-value' 
        )
    );
<br>

Jon

1283 Posts
Bucklash
8 years ago
0
Level 1

Hi Klas

Not sure if this request is appropriate but having looked through various forum posts, there seems to be no success for creating categories using jCckContent....

Would you be able to confirm if it is possible to create category using JCckContent in Afterstore?

Thanks either way

Bucklash

4229 Posts
Kadministrator
8 years ago
1
Level 1

Hi,

I will check and report back.

1283 Posts
Bucklash
8 years ago
0
Level 2

Hi Klas

Did you get a chance to check?

Is your parent_id being set correctly? and the level?

Bucklash

4229 Posts
Kadministrator
8 years ago
2
Level 1

You are right, I created PR to fix this

https://github.com/Octopoos/SEBLOD/pull/307

1283 Posts
Bucklash
8 years ago
1
Level 2

Hi Klas

Thanks so much for doing that.  

Really appreciated

If all the objects could be created as easily as articles then the world would be a better place :)

Hot-diggidy!

1283 Posts
Bucklash
7 years ago
0
Level 3

When creating category you might want to manipulate #__assets.rules

assign-value-to-rules

Get a VIP membership