Answers.Body: <p>Assuming <code>$parentCategory</code> is an existing <code>CategoryModel</code> (your error says that <code>$parentCategory</code> is not an object):</p>
  
  <pre><code>$childCategory = new CategoryModel();
  $childCategory-&gt;groupId = 1;
  $childCategory-&gt;newParentId = $parentCategory-&gt;id;
  $childCategory-&gt;getContent()-&gt;title = $childCategoryName;
  
  craft()-&gt;categories-&gt;saveCategory($childCategory);
  </code></pre>
  
  <p>You can see how Craft does it for it's own categories in <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/CategoriesController.php#L360" rel="nofollow noreferrer">CategoriesController->saveCategory()</a> and <a href="https://craftcms.com/classreference/services/CategoriesService#saveCategory-detail" rel="nofollow noreferrer">CategoriesService->saveCategory()</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-25T19:09:15.940Z
Answers.Text: Works like a charm, thanks Brad! Curious though... why is it necessary to specify both `newParentId` and `setParent`? Couldn't one be extrapolated from the other?
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-25T19:39:37.360Z
Answers.Text: That particular chunk of code could probably use some refactoring.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T16:54:41.093Z
Answers.Text: **Updated:** The relevant Craft code has changed, so the example has been simplified.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-25T00:53:35.157Z
Answers.LastActivityDate: 2016-11-04T16:53:21.327Z
Answers.LastEditDate: 2016-11-04T16:53:21.327Z
Answers.UserDisplayName: Brad Bell
Body: <p>In a custom plugin, I'm able to easily add a new Category:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$category = new CategoryModel();
  $category-&gt;groupId = 1;
  $category-&gt;getContent()-&gt;title = $categoryName;
  craft()-&gt;categories-&gt;saveCategory($category);
  </code></pre>
  
  <p>That part works great! But for some reason, I can't figure out how to save a <strong>nested</strong> category.</p>
  
  <p>I'd imagine it's something like this, but it doesn't seem to be working:</p>
  
  <pre class="lang-php prettyprint-override"><code>$childCategory = new CategoryModel();
  $childCategory-&gt;groupId = 1;
  $childCategory-&gt;level = 2;
  $childCategory-&gt;parent = $parentCategory-&gt;id;
  $childCategory-&gt;getContent()-&gt;title = $childCategoryName;
  craft()-&gt;categories-&gt;saveCategory($childCategory);
  </code></pre>
  
  <p>Unfortunately that code is giving me an error:</p>
  
  <blockquote>
    <p><strong>Internal Server Error</strong></p>
    
    <p>Trying to get property of non-object</p>
  </blockquote>
  
  <p>And if I drop the <code>-&gt;id</code> portion of this line:</p>
  
  <pre class="lang-php prettyprint-override"><code>$childCategory-&gt;parent = $parentCategory;
  </code></pre>
  
  <p>... it will save the category, but it <em>won't be nested</em>.</p>
  
  <p><strong>What am I doing wrong?</strong></p>
  
Tags: <plugin-development><categories>
Title: Create nested Category via PHP
ThreadText: # Create nested Category via PHP
  
  (Asked by: username_0 on 2015-06-25)
  
  In a custom plugin, I'm able to easily add a new Category:
  
  ```php
  $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:
  
  ```php
  $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:
  
  ```php
  $childCategory->parent = $parentCategory;
  
  ```
  
  ... it will save the category, but it *won't be nested*.
  
  **What am I doing wrong?**
  
  **Tags:** plugin-development, categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-25. Score: 5 | Accepted answer
  
  Assuming `$parentCategory` is an existing `CategoryModel` (your error says that `$parentCategory` is not an object):
  
  ```
  $childCategory = new CategoryModel();
  $childCategory->groupId = 1;
  $childCategory->newParentId = $parentCategory->id;
  $childCategory->getContent()->title = $childCategoryName;
  
  craft()->categories->saveCategory($childCategory);
  
  ```
  
  You can see how Craft does it for it's own categories in [CategoriesController->saveCategory()](https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/CategoriesController.php#L360) and [CategoriesService->saveCategory()](https://craftcms.com/classreference/services/CategoriesService#saveCategory-detail).
  
  > **Comments:**
  > **username_0**: Works like a charm, thanks username_1! Curious though... why is it necessary to specify both `newParentId` and `setParent`? Couldn't one be extrapolated from the other?  
  > **username_1**: That particular chunk of code could probably use some refactoring.  
  > **username_0**: **Updated:** The relevant Craft code has changed, so the example has been simplified.  
  
  ---
  
  