V8-archive: Hooking code throws a fatal error

Created on 5 Nov 2018  路  4Comments  路  Source: directus/v8-archive

Bug Report

Hooking code results in a fatal error. Note that it actually does its main job but it also throws a fatal error because it wants to write to the directus_revisions table but can't.

Steps to Reproduce

  1. Create an example collection:
CREATE TABLE `test` (
  `id` int(15) unsigned NOT NULL AUTO_INCREMENT,
  `t1` varchar(200) DEFAULT NULL COMMENT 'text',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  1. Write the hooking code:
<?php

return [
    'filters' => [
        'item.create:before' => function(\Directus\Hook\Payload $payload){
            $payload->set('t1',md5(time()));
            return $payload;
        }
    ]
];

3.Post to it:

curl -X POST \
  http://app.directus.local/_/items/test \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 333acf36-1b40-3138-9ef8-c7b7c9b4d12f' \
  -d '{

    "t1":"Test123123"
}'
  1. See error:
{
  "error": {
    "code": 9,
    "message": "Failed generating the SQL query. Statement could not be executed (42S22 - 1054 - Unknown column 't1' in 'field list')",
    "query": "INSERT INTO `directus_revisions` (`activity`, `collection`, `item`, `data`, `delta`, `parent_item`, `parent_collection`, `parent_changed`, `t1`) VALUES ('92', 'test', '42', '{\\\"id\\\":\\\"42\\\",\\\"t1\\\":\\\"2d3d29533b34330b074fa0c22ff5d9cf\\\"}', '{\\\"t1\\\":\\\"Test123123\\\"}', NULL, NULL, NULL, '2d3d29533b34330b074fa0c22ff5d9cf')",
    "class": "Directus\\Database\\Exception\\InvalidQueryException",
    "file": "/Applications/MAMP/htdocs/app.directus/src/core/Directus/Database/TableGateway/BaseTableGateway.php",
    "line": 802
  }
}

Expected Behavior

Hook doing its job also no fatal or any errors.

Actual Behavior

Hook again does its job but also throwing a fatal error (because it can't write to directus_revisions)

Other Context & Screenshots

Technical Details

  • Latest bundled Directus version.

  • Device: [eg: Desktop, iPhone6, etc]

  • OS: [eg: MacOS 10.13.6, Windows 10.1803]
  • Web Server: [eg: Apache 2.4.37]
  • PHP Version: [eg: 7.2.0]
  • Database: [eg: MySQL 8.0.12]
  • Install Method: [eg: cloned master branch]
invalid

All 4 comments

Hey @grinninglich, this is an expected behavior as you are trying to set on all collections a value to a field named t1, which not exists on all collection.

There's two way you can create this properly:

Check the payload collection

<?php

return [
    'filters' => [
        'item.create:before' => function(\Directus\Hook\Payload $payload){
            $collectionName = $payload->attribute('collection_name');

            if ($collectionName === 'test') {
                $payload->set('t1',md5(time()));
            }

            return $payload;
        }
    ]
];

Using a hook specific to a collection

<?php

return [
    'filters' => [
        'item.create.test:before' => function(\Directus\Hook\Payload $payload){
            $payload->set('t1',md5(time()));
            return $payload;
        }
    ]
];

Hope it helps.

@WellingGuzman Thanks for the hand! The first one works great but second one is not working.

Hey @grinninglich, I updated the example:

It was suppose to be item.create.test:before instead of item.test.create:before.

Hey @grinninglich, I updated the example:

It was suppose to be item.create.test:before instead of item.test.create:before.

Thanks 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

24js picture 24js  路  3Comments

gitlabisbetterthangithub picture gitlabisbetterthangithub  路  3Comments

andgar2010 picture andgar2010  路  3Comments

rijkvanzanten picture rijkvanzanten  路  3Comments

cdwmhcc picture cdwmhcc  路  3Comments