You are not logged in.
Pages: 1
Hello,
the following code worked fine before version 10:
global $DB;
$result = $DB->request([
'SELECT' => 'stanzafield',
'FROM' => 'glpi_plugin_fields_ticketstanzas',
'WHERE' => 'items_id = ' . $item->getField("id")
]);
if ($row = $result->next()) {
$room = $row['stanzafield'];
}
After the upgrade, it does not return any results. $result array is empty, but if I run the query directly on MySQL I get the result. I have tried them all and need help.
Thanks
Offline
Try with
global $DB;
$result = $DB->request([
'SELECT' => 'stanzafield',
'FROM' => 'glpi_plugin_fields_ticketstanzas',
'WHERE' => ['items_id' => $item->getField("id")]
]);
if ($row = $result->next()) {
$room = $row['stanzafield'];
}
Offline
The iterator result from $DB->request now properly matches the PHP iterator interface. This means that the next method returns nothing rather than the next element.
The equivalent in this case would be to use $result->current() instead.
Also for reference, you can loop through all results with something like:
foreach ($result as $data) {}
GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.
Offline
Thank you to both of you, but still not working.
I'm trying:
$result = $DB->request([
'SELECT' => 'stanzafield',
'FROM' => 'glpi_plugin_fields_ticketstanzas',
'WHERE' => ['items_id' => $item->getField("id")]
])->current();
$room = $result['stanzafield'];
but I get $result as an empty array. If I don't add ->current() the DB request doesn't work at all.
What am I doing worng?
Last edited by diego.giacani (2022-06-09 09:18:54)
Offline
It is difficult to say without knowing the complete context that this code is used in.
Did you remember to use the global statement before this snippet to bring the $DB global into scope?
"global $DB;"
Also, the SELECT value should be an array but I don't remember if that is required.
"'SELECT' => ['stanzafield'],"
GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.
Offline
SELECT value works even if not in array, thank you.
After many test I found out that this works:
global $DB;
$result = $DB->request([
'SELECT' => 'stanzafield',
'FROM' => 'glpi_plugin_fields_ticketstanzas',
'WHERE' => ['items_id' => 1]
]);
And I get an array $result->current() with only one item and the expected value.
Anyway, if I try:
global $DB;
$result = $DB->request([
'SELECT' => 'stanzafield',
'FROM' => 'glpi_plugin_fields_ticketstanzas',
'WHERE' => ['items_id' => $item->getField("id")]
]);
$result->current() is empty. I verified and $item->getField("id") has a value and it is the expected one.
Offline
I think I understand what the problem is. I am editing the code of src/NotificationTargetTicket.php. The array is empty not because of a query error, but because the record has not yet been registered on the Fields plugin table. While the rest of the ticket data is already available, the plugin data is not. How can I solve this? Which file needs to be modified?
Offline
Of course, the solution was quite simple: using the $_SESSION array and getting the value from there. I wasted so much time on this and it was sooo simple!
Thank you a lot for your help!
Last edited by diego.giacani (2022-06-15 10:42:06)
Offline
Pages: 1