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.
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;
<?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"
}'
{
"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
}
}
Hook doing its job also no fatal or any errors.
Hook again does its job but also throwing a fatal error (because it can't write to directus_revisions)
Latest bundled Directus version.
Device: [eg: Desktop, iPhone6, etc]
master branch]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:beforeinstead ofitem.test.create:before.
Thanks 馃憤