Creating a CategoryGroup in a migration - Exception: Tried to save a category group that is missing site settings
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
Your error is here $blogCategoryGroup->setSiteSettings([$blogCatSettings]);
You need to include an array with [$siteId => $blogCatSettings] because the condition will check if the key exists in your array.
if (!isset($allSiteSettings[$siteId])) { <--- it will search for the key siteId in your array. Since your array is [$blogCatSettings] there will be no chance to find the siteId since your key is always 0
Yes that does remove the error, thanks! I guess this should be more consistent between the Section and CategoryGroup settings objects though. When saving the Section it only checks if the array is null, rather than looking for specific indexes.
if (empty($allSiteSettings)) {
throw new Exception('Tried to save a section without any site settings');
}
If the specific site ID should be checked, I would say that the object should be matched by the ID property, rather than using a keyed array. Alternatively (though I wouldn't suggest it), the ID could be stored as the key automatically when calling the set method. Either way, forcing an indexed array like this is a bit tedious, especially when the site ID is already in the settings object.