verbb / vizy

A flexible visual editor for Craft CMS
Other
43 stars 8 forks source link

Saving entry with Vizy field and nested category field results in an error #199

Closed superflausch closed 1 year ago

superflausch commented 1 year ago

Describe the bug

Saving an entry (manually or the auto saving feature of Craft) triggers an error when we change a category field within a Vizy Block resulting in an error.

Within the page (saving indicator) when auto saving:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sourceId' cannot be null The SQL being executed was: INSERT INTO `relations` (`fieldId`, `sourceId`, `sourceSiteId`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (11, NULL, NULL, 102, 1, '2023-03-22 11:10:40', '2023-03-22 11:10:40', '101edefd-470e-4576-99ba-7e0c6121da1d')

After submiting the changes (manual save):

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sourceId' cannot be null in /var/www/html/vendor/yiisoft/yii2/db/Command.php:1302
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1302): PDOStatement->execute()
#1 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1102): yii\db\Command->internalExecute('INSERT INTO `re...')
#2 /var/www/html/vendor/craftcms/cms/src/helpers/Db.php(1010): yii\db\Command->execute()
#3 /var/www/html/vendor/craftcms/cms/src/services/Relations.php(112): craft\helpers\Db::batchInsert('{{%relations}}', Array, Array, Object(craft\db\Connection))
#4 /var/www/html/vendor/craftcms/cms/src/fields/BaseRelationField.php(987): craft\services\Relations->saveRelations(Object(craft\fields\Categories), Object(verbb\vizy\elements\Block), Array)
#5 /var/www/html/vendor/verbb/vizy/src/nodes/VizyBlock.php(247): craft\fields\BaseRelationField->afterElementSave(Object(verbb\vizy\elements\Block), true)
#6 /var/www/html/vendor/verbb/vizy/src/models/NodeCollection.php(158): verbb\vizy\nodes\VizyBlock->serializeValue(Object(benf\neo\elements\Block))
#7 /var/www/html/vendor/verbb/vizy/src/fields/VizyField.php(339): verbb\vizy\models\NodeCollection->serializeValues(Object(benf\neo\elements\Block))
#8 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2419): verbb\vizy\fields\VizyField->serializeValue(Object(verbb\vizy\models\NodeCollection), Object(benf\neo\elements\Block))
#9 /var/www/html/vendor/yiisoft/yii2/base/Model.php(370): craft\base\Element->afterValidate()
#10 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2374): yii\base\Model->validate(Array, true)
#11 /var/www/html/vendor/spicyweb/craft-neo/src/elements/Block.php(276): craft\base\Element->validate(NULL, true)
#12 /var/www/html/vendor/spicyweb/craft-neo/src/Field.php(744): benf\neo\elements\Block->validate()
#13 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2512): benf\neo\Field->validateBlocks(Object(craft\elements\Entry), NULL)
#14 /var/www/html/vendor/yiisoft/yii2/validators/InlineValidator.php(77): craft\base\Element->validateCustomFieldAttribute('field:hero', Array, Object(yii\validators\InlineValidator), Object(benf\neo\elements\db\BlockQuery))
#15 /var/www/html/vendor/yiisoft/yii2/validators/Validator.php(260): yii\validators\InlineValidator->validateAttribute(Object(craft\elements\Entry), 'field:hero')
#16 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2413): yii\validators\Validator->validateAttributes(Object(craft\elements\Entry))
#17 /var/www/html/vendor/yiisoft/yii2/base/Model.php(370): craft\base\Element->afterValidate()
#18 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2374): yii\base\Model->validate(Array, true)
#19 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(2969): craft\base\Element->validate()
#20 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1062): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, true, NULL, Array, false)
#21 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(1339): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#22 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
#23 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#24 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#25 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('apply-draft', Array)
#26 /var/www/html/vendor/craftcms/cms/src/web/Application.php(302): yii\base\Module->runAction('elements/apply-...', Array)
#27 /var/www/html/vendor/craftcms/cms/src/web/Application.php(627): craft\web\Application->runAction('elements/apply-...', Array)
#28 /var/www/html/vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#29 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#30 /var/www/html/web/index.php(12): yii\base\Application->run()
#31 {main}

Next yii\db\IntegrityException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sourceId' cannot be null
The SQL being executed was: INSERT INTO `relations` (`fieldId`, `sourceId`, `sourceSiteId`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (11, NULL, NULL, 104, 1, '2023-03-22 11:21:16', '2023-03-22 11:21:16', '0e181b48-d217-4232-aaf0-5417dec86322') in /var/www/html/vendor/yiisoft/yii2/db/Schema.php:676
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1307): yii\db\Schema->convertException(Object(PDOException), 'INSERT INTO `re...')
#1 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1102): yii\db\Command->internalExecute('INSERT INTO `re...')
#2 /var/www/html/vendor/craftcms/cms/src/helpers/Db.php(1010): yii\db\Command->execute()
#3 /var/www/html/vendor/craftcms/cms/src/services/Relations.php(112): craft\helpers\Db::batchInsert('{{%relations}}', Array, Array, Object(craft\db\Connection))
#4 /var/www/html/vendor/craftcms/cms/src/fields/BaseRelationField.php(987): craft\services\Relations->saveRelations(Object(craft\fields\Categories), Object(verbb\vizy\elements\Block), Array)
#5 /var/www/html/vendor/verbb/vizy/src/nodes/VizyBlock.php(247): craft\fields\BaseRelationField->afterElementSave(Object(verbb\vizy\elements\Block), true)
#6 /var/www/html/vendor/verbb/vizy/src/models/NodeCollection.php(158): verbb\vizy\nodes\VizyBlock->serializeValue(Object(benf\neo\elements\Block))
#7 /var/www/html/vendor/verbb/vizy/src/fields/VizyField.php(339): verbb\vizy\models\NodeCollection->serializeValues(Object(benf\neo\elements\Block))
#8 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2419): verbb\vizy\fields\VizyField->serializeValue(Object(verbb\vizy\models\NodeCollection), Object(benf\neo\elements\Block))
#9 /var/www/html/vendor/yiisoft/yii2/base/Model.php(370): craft\base\Element->afterValidate()
#10 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2374): yii\base\Model->validate(Array, true)
#11 /var/www/html/vendor/spicyweb/craft-neo/src/elements/Block.php(276): craft\base\Element->validate(NULL, true)
#12 /var/www/html/vendor/spicyweb/craft-neo/src/Field.php(744): benf\neo\elements\Block->validate()
#13 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2512): benf\neo\Field->validateBlocks(Object(craft\elements\Entry), NULL)
#14 /var/www/html/vendor/yiisoft/yii2/validators/InlineValidator.php(77): craft\base\Element->validateCustomFieldAttribute('field:hero', Array, Object(yii\validators\InlineValidator), Object(benf\neo\elements\db\BlockQuery))
#15 /var/www/html/vendor/yiisoft/yii2/validators/Validator.php(260): yii\validators\InlineValidator->validateAttribute(Object(craft\elements\Entry), 'field:hero')
#16 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2413): yii\validators\Validator->validateAttributes(Object(craft\elements\Entry))
#17 /var/www/html/vendor/yiisoft/yii2/base/Model.php(370): craft\base\Element->afterValidate()
#18 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2374): yii\base\Model->validate(Array, true)
#19 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(2969): craft\base\Element->validate()
#20 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1062): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, true, NULL, Array, false)
#21 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(1339): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#22 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
#23 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#24 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#25 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('apply-draft', Array)
#26 /var/www/html/vendor/craftcms/cms/src/web/Application.php(302): yii\base\Module->runAction('elements/apply-...', Array)
#27 /var/www/html/vendor/craftcms/cms/src/web/Application.php(627): craft\web\Application->runAction('elements/apply-...', Array)
#28 /var/www/html/vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#29 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#30 /var/www/html/web/index.php(12): yii\base\Application->run()
#31 {main}
Additional Information:
Array
(
    [0] => 23000
    [1] => 1048
    [2] => Column 'sourceId' cannot be null
)

As well as the following error is visible in the console:

yii\web\BadRequestHttpException: Post request required in /var/www/html/vendor/craftcms/cms/src/web/Controller.php:454
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(1145): craft\web\Controller->requirePostRequest()
#1 [internal function]: craft\controllers\ElementsController->actionSaveDraft()
#2 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#4 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('save-draft', Array)
#5 /var/www/html/vendor/craftcms/cms/src/web/Application.php(302): yii\base\Module->runAction('elements/save-d...', Array)
#6 /var/www/html/vendor/craftcms/cms/src/web/Application.php(627): craft\web\Application->runAction('elements/save-d...', Array)
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#8 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#9 /var/www/html/web/index.php(12): yii\base\Application->run()
#10 {main}

Steps to reproduce

  1. Visit an entry with a Vizy field and Vizy blocks with a category field
  2. Select/Change a category within the Vizy block child field

Craft CMS version

4.4.5

Plugin version

2.0.12

Multi-site?

Yes

Additional context

The Vizy field ist nested within a Neo block (3.7.3). We also see the Neo field creating errors when trying to update the visible fields. But no other field triggers the issue described here.

superflausch commented 1 year ago

Quick addition. This doesn't happen with the same category field outside of the Vizy field.

engram-design commented 1 year ago

Aware of this one, caused by the new "Maintain hierarchy" option for Categories fields introduced in Craft 4.4. Disabling that will fix this issue, but I'm looking into it.

engram-design commented 1 year ago

Fixed for the next release. To get this early, run composer require verbb/vizy:"dev-craft-4 as 2.0.12"

superflausch commented 1 year ago

Works now as expected. Thank you.

engram-design commented 1 year ago

Fixed in 2.1.0