Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving new section programmatically missing section ID #3943

Closed
maxxwv opened this issue Mar 4, 2019 · 19 comments
Closed

Saving new section programmatically missing section ID #3943

maxxwv opened this issue Mar 4, 2019 · 19 comments
Assignees

Comments

@maxxwv
Copy link

maxxwv commented Mar 4, 2019

Description

When using the sample code for saveSection() at https://docs.craftcms.com/api/v3/craft-services-sections.html#public-methods, I get a \Throwable error "Entry type is missing its section ID". I'm new to Craft and have the code in the main plugin file for a plugin that I've written and activated.

I'm using a custom spun Vagrant box, if that would possibly make a difference.

Steps to reproduce

Copy and paste into a plugin's main theme - in this case, my plugin is named 'Testing' and 'Testing.php' is the main entry point. I used pluginfactory.io to create the scaffold.

use craft\models\Section;
use craft\models\Section_SiteSettings;

$section = new Section([
    'name' => 'News',
    'handle' => 'news',
    'type' => Section::TYPE_CHANNEL,
    'siteSettings' => [
        new Section_SiteSettings([
            'siteId' => Craft::$app->sites->getPrimarySite()->id,
            'enabledByDefault' => true,
            'hasUrls' => true,
            'uriFormat' => 'foo/{slug}',
            'template' => 'foo/_entry',
        ]),
    ]
]);

try{
   $success = Craft::$app->sections->saveSection($section);
}catch(\craft\errors\EntryTypeNotFoundException $e){
   die("<pre>".var_export("From \\craft\\errors\\EntryTypeNotFoundException: {$e->getMessage()}", true)."</pre>");
}catch(\Throwable $e){
   die("<pre>".var_export("From \\Throwable: {$e->getMessage()}", true)."</pre>");
}finally{
   die("<pre>".var_export($success, true)."</pre>");
}

Instance of \Throwable is thrown with getMessage() =="Entry type is missing its section ID"

Additional info

  • Craft version: 3.1.15 Solo
  • PHP version: 7.2
  • Database driver & version: MySQL 5.7.25
  • Plugins & versions: None
@brandonkelly
Copy link
Member

Can you search for that error in your storage/logs/ folder and post the stack trace that follows it in the logs?

@maxxwv
Copy link
Author

maxxwv commented Mar 5, 2019

Sorry about the delay in answering - here's the stack trace from /storage/logs/web.log:

2019-03-04 13:00:47 [-][-][-][profile end][yii\db\Command::query] SELECT MAX(`sortOrder`)
FROM `entrytypes`
WHERE `sectionId` IS NULL
2019-03-04 13:00:47 [-][-][-][trace][yii\db\Transaction::rollBack] Roll back transaction
2019-03-04 13:00:47 [-][-][-][error][yii\base\InvalidConfigException] yii\base\InvalidConfigException: Entry type is missing its section ID in /var/www/vendor/craftcms/cms/src/models/EntryType.php:173
Stack trace:
#0 /var/www/vendor/craftcms/cms/src/services/Sections.php(1062): craft\models\EntryType->getSection()
#1 /var/www/vendor/craftcms/cms/src/services/Sections.php(522): craft\services\Sections->saveEntryType(Object(craft\models\EntryType))
#2 /var/_assets/testing/src/Testing.php(162): craft\services\Sections->saveSection(Object(craft\models\Section), false)
#3 /var/_assets/testing/src/Testing.php(98): mdg\testing\Testing->createSection()
#4 /var/www/vendor/yiisoft/yii2/base/BaseObject.php(109): mdg\testing\Testing->init()
#5 /var/www/vendor/yiisoft/yii2/base/Module.php(158): yii\base\BaseObject->__construct(Array)
#6 /var/www/vendor/craftcms/cms/src/base/Plugin.php(127): yii\base\Module->__construct('testing', Object(craft\web\Application), Array)
#7 [internal function]: craft\base\Plugin->__construct('testing', Object(craft\web\Application), Array)
#8 /var/www/vendor/yiisoft/yii2/di/Container.php(384): ReflectionClass->newInstanceArgs(Array)
#9 /var/www/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build('mdg\\testing\\Tes...', Array, Array)
#10 /var/www/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->get('mdg\\testing\\Tes...', Array, Array)
#11 /var/www/vendor/craftcms/cms/src/services/Plugins.php(897): yii\BaseYii::createObject(Array, Array)
#12 /var/www/vendor/craftcms/cms/src/services/Plugins.php(230): craft\services\Plugins->createPlugin('testing', Array)
#13 /var/www/vendor/craftcms/cms/src/base/ApplicationTrait.php(1223): craft\services\Plugins->loadPlugins()
#14 /var/www/vendor/craftcms/cms/src/web/Application.php(112): craft\web\Application->_postInit()
#15 /var/www/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#16 /var/www/vendor/yiisoft/yii2/base/Application.php(206): yii\base\BaseObject->__construct(Array)
#17 /var/www/vendor/craftcms/cms/src/web/Application.php(100): yii\base\Application->__construct(Array)
#18 [internal function]: craft\web\Application->__construct(Array)
#19 /var/www/vendor/yiisoft/yii2/di/Container.php(384): ReflectionClass->newInstanceArgs(Array)
#20 /var/www/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#21 /var/www/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#22 /var/www/vendor/craftcms/cms/bootstrap/bootstrap.php(255): yii\BaseYii::createObject(Array)
#23 /var/www/vendor/craftcms/cms/bootstrap/web.php(42): require('/var/www/vendor...')
#24 /var/www/web/index.php(20): require('/var/www/vendor...')

@RudolfBos
Copy link

I'm having the same issue when creating a section in the Craft CMS interface.
These are the reproduction steps in Craft CMS:

  1. Go to "Settings"
  2. Go to "Sections"
  3. Click on the button "New section"
  4. Fill in the fields and save the section

This is the error:
error

@maxxwv
Copy link
Author

maxxwv commented Mar 18, 2019

@RudolfBos - this is happening when you're manually setting up the section in the admin back-end?

@PT-Studios
Copy link

PT-Studios commented Mar 19, 2019

Also happening for me, although, it's only happened after cloning my website and importing DB. The site it came from is fine. Additionally, I'm not able to update any System Settings on the admin backend.

Here's the stack trace:

yii\base\InvalidConfigException: Entry type is missing its section ID in C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\models\EntryType.php:173
Stack trace:
#0 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\services\Sections.php(1061): craft\models\EntryType->getSection()
#1 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\services\Sections.php(521): craft\services\Sections->saveEntryType(Object(craft\models\EntryType))
#2 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\controllers\SectionsController.php(186): craft\services\Sections->saveSection(Object(craft\models\Section))
#3 [internal function]: craft\controllers\SectionsController->actionSaveSection()
#4 C:\dev\sites\localhost.craft\craft-wine\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#5 C:\dev\sites\localhost.craft\craft-wine\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#6 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\web\Controller.php(109): yii\base\Controller->runAction('save-section', Array)
#7 C:\dev\sites\localhost.craft\craft-wine\vendor\yiisoft\yii2\base\Module.php(528): craft\web\Controller->runAction('save-section', Array)
#8 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\web\Application.php(297): yii\base\Module->runAction('sections/save-s...', Array)
#9 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\web\Application.php(561): craft\web\Application->runAction('sections/save-s...', Array)
#10 C:\dev\sites\localhost.craft\craft-wine\vendor\craftcms\cms\src\web\Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#11 C:\dev\sites\localhost.craft\craft-wine\vendor\yiisoft\yii2\base\Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#12 C:\dev\sites\localhost.craft\craft-wine\web\index.php(21): yii\base\Application->run()
#13 {main}

@RudolfBos
Copy link

@RudolfBos - this is happening when you're manually setting up the section in the admin back-end?

@maxxwv Yes!

@andris-sevcenko andris-sevcenko self-assigned this Mar 25, 2019
@philbrookweb
Copy link

philbrookweb commented Apr 2, 2019

Happening here too:

function createEventSection()
    {
        $section = new SectionModel([
            'name'          => 'Tessitura Events',
            'handle'        => 'tessituraEvents',
            'type'          => SectionModel::TYPE_CHANNEL,
            'siteSettings'  => [
                new SectionSettingsModel([
                    'siteId' => Craft::$app->sites->getPrimarySite()->id,
                    'enabledByDefault' => true,
                    'hasUrls' => true,
                    'uriFormat' => 'tessEvents/{slug}',
                    'template' => 'tessEvents/_entry',
                ]),
            ]
        ],true);

        try {
            $save = Craft::$app->sections->saveSection($section);
        } catch (\craft\errors\EntryTypeNotFoundException $e) {
            var_dump($section->getErrors());
        }
    }
> yii\base\InvalidConfigException: Entry type is missing its section ID in C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\models\EntryType.php:173
> Stack trace:
> #0 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Sections.php(1106): craft\models\EntryType->getSection()
> #1 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Sections.php(523): craft\services\Sections->saveEntryType(Object(craft\models\EntryType))
> #2 C:\dev\sites\localhost.craft\craft-plugins\plugins\tessituraeventsmanager\src\TessituraEventsManager.php(138): craft\services\Sections->saveSection(Object(craft\models\Section))
> #3 C:\dev\sites\localhost.craft\craft-plugins\plugins\tessituraeventsmanager\src\TessituraEventsManager.php(73): philbrookweb\tessituraeventsmanager\TessituraEventsManager->createEventSection()
> #4 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\BaseObject.php(109): philbrookweb\tessituraeventsmanager\TessituraEventsManager->init()
> #5 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\Module.php(158): yii\base\BaseObject->__construct(Array)
> #6 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\base\Plugin.php(127): yii\base\Module->__construct('tessitura-event...', Object(craft\web\Application), Array)
> #7 [internal function]: craft\base\Plugin->__construct('tessitura-event...', Object(craft\web\Application), Array)
> #8 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(384): ReflectionClass->newInstanceArgs(Array)
> #9 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('philbrookweb\\te...', Array, Array)
> #10 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('philbrookweb\\te...', Array, Array)
> #11 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Plugins.php(897): yii\BaseYii::createObject(Array, Array)
> #12 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Plugins.php(230): craft\services\Plugins->createPlugin('tessitura-event...', Array)
> #13 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\base\ApplicationTrait.php(1225): craft\services\Plugins->loadPlugins()
> #14 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\web\Application.php(112): craft\web\Application->_postInit()
> #15 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\BaseObject.php(109): craft\web\Application->init()
> #16 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\Application.php(206): yii\base\BaseObject->__construct(Array)
> #17 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\web\Application.php(100): yii\base\Application->__construct(Array)
> #18 [internal function]: craft\web\Application->__construct(Array)
> #19 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(384): ReflectionClass->newInstanceArgs(Array)
> #20 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
> #21 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
> #22 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\bootstrap\bootstrap.php(255): yii\BaseYii::createObject(Array)
> #23 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\bootstrap\web.php(42): require('C:\\dev\\sites\\lo...')
> #24 C:\dev\sites\localhost.craft\craft-plugins\web\index.php(20): require('C:\\dev\\sites\\lo...')
> #25 {main}

@brandonkelly
Copy link
Member

Can you folks please try running ./craft project-config/rebuild and then run your code again and see if that helps?

@philbrookweb
Copy link

PS C:\dev\sites\localhost.craft\craft-plugins> ./craft project-config/rebuild
Rebuilding the project config from the current state ... done

Unfortunately, still occurring:

yii\base\InvalidConfigException: Entry type is missing its section ID in C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\models\EntryType.php:173
Stack trace:
#0 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Sections.php(1106): craft\models\EntryType->getSection()
#1 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Sections.php(523): craft\services\Sections->saveEntryType(Object(craft\models\EntryType))
#2 C:\dev\sites\localhost.craft\craft-plugins\plugins\tessituraeventsmanager\src\TessituraEventsManager.php(138): craft\services\Sections->saveSection(Object(craft\models\Section))
#3 C:\dev\sites\localhost.craft\craft-plugins\plugins\tessituraeventsmanager\src\TessituraEventsManager.php(73): philbrookweb\tessituraeventsmanager\TessituraEventsManager->createEventSection()
#4 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\BaseObject.php(109): philbrookweb\tessituraeventsmanager\TessituraEventsManager->init()
#5 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\Module.php(158): yii\base\BaseObject->__construct(Array)
#6 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\base\Plugin.php(127): yii\base\Module->__construct('tessitura-event...', Object(craft\web\Application), Array)
#7 [internal function]: craft\base\Plugin->__construct('tessitura-event...', Object(craft\web\Application), Array)
#8 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(384): ReflectionClass->newInstanceArgs(Array)
#9 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('philbrookweb\\te...', Array, Array)
#10 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('philbrookweb\\te...', Array, Array)
#11 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Plugins.php(897): yii\BaseYii::createObject(Array, Array)
#12 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\services\Plugins.php(230): craft\services\Plugins->createPlugin('tessitura-event...', Array)
#13 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\base\ApplicationTrait.php(1225): craft\services\Plugins->loadPlugins()
#14 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\web\Application.php(112): craft\web\Application->_postInit()
#15 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\BaseObject.php(109): craft\web\Application->init()
#16 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\base\Application.php(206): yii\base\BaseObject->__construct(Array)
#17 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\src\web\Application.php(100): yii\base\Application->__construct(Array)
#18 [internal function]: craft\web\Application->__construct(Array)
#19 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(384): ReflectionClass->newInstanceArgs(Array)
#20 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#21 C:\dev\sites\localhost.craft\craft-plugins\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#22 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\bootstrap\bootstrap.php(255): yii\BaseYii::createObject(Array)
#23 C:\dev\sites\localhost.craft\craft-plugins\vendor\craftcms\cms\bootstrap\web.php(42): require('C:\\dev\\sites\\lo...')
#24 C:\dev\sites\localhost.craft\craft-plugins\web\index.php(20): require('C:\\dev\\sites\\lo...')
#25 {main}

@brandonkelly
Copy link
Member

@philbrookweb Can you email your composer.json and composer.lock files, and a database backup, over to support@craftcms.com, and reference this GitHub issue URL?

@maxxwv
Copy link
Author

maxxwv commented Apr 3, 2019

I get the following:

Rebuilding the project config from the current state ... error: file_put_contents(/var/www/storage/config-backups/project.yaml): failed to open stream: Permission denied

Running an Ubuntu 18.04 Vagrant box on Windows 10 from the Admin Power Shell. Note that the storage directory has 777 permissions and is part of the www-data group, owned by vagrant user.

@philbrookweb
Copy link

@brandonkelly Done!

@brandonkelly
Copy link
Member

@maxxwv Check the permissions on storage/config-backups/.

@maxxwv
Copy link
Author

maxxwv commented Apr 5, 2019

@brandonkelly - I must've changed something on the last spin-up of the container because this morning I'm not having permissions problems. Sorry about that.

Now having successfully run the project-config/rebuild task, Craft::$app->sections->saveSection() is no longer throwing an exception, but it is returning false and I'm not seeing the section in my Sections area of the admin back-end, and I'm getting 'An unknown error occurred.' every time I refresh the plugins screen.

@maxxwv
Copy link
Author

maxxwv commented Apr 8, 2019

Just an update on this - it appears to be working now. It does appear, though, the the handle can't have a dash in it. Is that correct? I changed the handle to "news-testing" and the unknown error appears. Dumping $section->getErrors() reporting that '"news-testing" isn't a valid handle'

@brandonkelly
Copy link
Member

@maxxwv huh, well glad it’s working now! You are correct that sections cannot have dashes. (There’s an open request to allow them - #2276.)

Going to close this. @philbrookweb I still have your email in my inbox and will get to it ASAP.

@brandonkelly
Copy link
Member

brandonkelly commented Apr 23, 2019

@philbrookweb’s issue ended up being an order of operations bug – plugins are getting loaded before Craft registers its project config event handlers, so a call to saveSection() from a plugin’s init() method wasn’t getting handled properly. I’ve fixed that for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#0523421d91592d0f3dcc550b8a6739c63b8912ba as 3.1.24",
  "...": "..."
}

Then run composer update.

@philbrookweb
Copy link

@philbrookweb’s issue ended up being an order of operations bug – plugins are getting loaded before Craft registers its project config event handlers, so a call to saveSection() from a plugin’s init() method wasn’t getting handled properly. I’ve fixed that for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#0523421d91592d0f3dcc550b8a6739c63b8912ba as 3.1.24",
  "...": "..."
}

Then run composer update.

This is working for me, thank you Brandon.

@proimage
Copy link

proimage commented Aug 9, 2019

@philbrookweb’s issue ended up being an order of operations bug – plugins are getting loaded before Craft registers its project config event handlers, so a call to saveSection() from a plugin’s init() method wasn’t getting handled properly. I’ve fixed that for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#0523421d91592d0f3dcc550b8a6739c63b8912ba as 3.1.24",
  "...": "..."
}

Then run composer update.

Is this fix in 3.1.26? I'm encountering this as well, and it seems to me that 3.1.24 < 3.1.26, unless there's something I'm not understanding about dev-develop numbers being different...

EDIT: Running ./craft project-config/rebuild seems to have fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants