In a custom plugin, I'm able to easily add a new Category:
$category = new CategoryModel(); $category->groupId = 1; $category->getContent()->title = $categoryName; craft()->categories->saveCategory($category);
That part works great! But for some reason, I can't figure out how to save a nested category.
I'd imagine it's something like this, but it doesn't seem to be working:
$childCategory = new CategoryModel(); $childCategory->groupId = 1; $childCategory->level = 2; $childCategory->parent = $parentCategory->id; $childCategory->getContent()->title = $childCategoryName; craft()->categories->saveCategory($childCategory);
Unfortunately that code is giving me an error:
Internal Server Error
Trying to get property of non-object
And if I drop the
->id portion of this line:
$childCategory->parent = $parentCategory;
... it will save the category, but it won't be nested.
What am I doing wrong?