Creating a CategoryGroup in a migration - Exception: Tried to save a category group that is missing site settings
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
Description
When trying to create a new CategoryGroup within a migration, the migration fails with an error saying it cannot save a category group which is missing site settings:
Exception: Tried to save a category group that is missing site settings (/project/vendor/craftcms/cms/src/services/Categories.php:325)
Here the code for my migration file
public function safeUp()
{
$siteId = Craft::$app->sites->getPrimarySite()->id;
$blogCategoryGroup = new \craft\models\CategoryGroup([
"name" => "Blog Categories",
"handle" => "blogCategories",
]);
$blogCatSettings = new \craft\models\CategoryGroup_SiteSettings([
"siteId" => $siteId,
"uriFormat" => "/category/{slug}",
"template" => "/category/_entry",
"hasUrls" => 1,
]);
$blogCategoryGroup->setSiteSettings([$blogCatSettings]);
return Craft::$app->categories->saveGroup($blogCategoryGroup);
}
I have also tried setting the settings directly, which worked when creating a new Section:
$blogCategoryGroup = new \craft\models\CategoryGroup([
"name" => "Blog Categories",
"handle" => "blogCategories",
"siteSettings" => [
new \craft\models\CategoryGroup_SiteSettings([
"siteId" => $siteId,
"uriFormat" => "/category/{slug}",
"template" => "/category/_entry",
"hasUrls" => 1,
])
]
]);
The error comes from the categories service here:
// Make sure they're all there
foreach (Craft::$app->getSites()->getAllSiteIds() as $siteId) {
if (!isset($allSiteSettings[$siteId])) {
throw new Exception('Tried to save a category group that is missing site settings');
}
}
Here is the dump from Craft::$app->getSites()->getAllSiteIds():
array(1) {
[0]=>
int(1)
}
It seems to me that it's searching for a siteSettings object at the index of the siteId (1), but when I dump the $allSiteSettings, there is no item at $allSiteSettings[1] because the siteSettings is a zero-indexed array:
array(1) {
[0]=>
object(craft\models\CategoryGroup_SiteSettings)#147 (13) {
["id"]=>
NULL
["groupId"]=>
NULL
["siteId"]=>
string(1) "1"
["hasUrls"]=>
int(1)
["uriFormat"]=>
string(16) "/category/{slug}"
["template"]=>
string(16) "/category/_entry"
["_group":"craft\models\CategoryGroup_SiteSettings":private]=>
*RECURSION*
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL
}
}
Steps to reproduce
- Create a new migration
- Try to create a new CategoryGroup inside the safeUp method
- Run
.craft migrate/up - Receive Exception
Additional info
- Craft version: 3.0.0-RC17
- PHP version: 7.1.12
- Database driver & version: MySQL 5.6.38
- Plugins & versions: Redactor 1.0.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in vendor/craftcms/cms/src/services/Categories.php around line 325, then inspect how CategoryGroup site settings are assembled and keyed during saveGroup(). Reproduce the migration from the issue with Craft 3.0.0-RC17 and verify that saving a group with the primary site's settings no longer raises the missing-site-settings exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100