You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2022-06-07 16:57:40

diego.giacani
Member
From: Rome, Italy
Registered: 2020-10-16
Posts: 31

Querying DB not working in GLPI 10

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

#2 2022-06-08 17:08:46

Kaya84
Member
Registered: 2019-06-13
Posts: 217

Re: Querying DB not working in GLPI 10

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

#3 2022-06-08 18:43:38

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,819
Website

Re: Querying DB not working in GLPI 10

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

#4 2022-06-09 09:17:49

diego.giacani
Member
From: Rome, Italy
Registered: 2020-10-16
Posts: 31

Re: Querying DB not working in GLPI 10

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

#5 2022-06-11 13:46:47

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,819
Website

Re: Querying DB not working in GLPI 10

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

#6 2022-06-13 11:43:37

diego.giacani
Member
From: Rome, Italy
Registered: 2020-10-16
Posts: 31

Re: Querying DB not working in GLPI 10

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

#7 2022-06-13 12:41:56

diego.giacani
Member
From: Rome, Italy
Registered: 2020-10-16
Posts: 31

Re: Querying DB not working in GLPI 10

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

#8 2022-06-15 10:41:40

diego.giacani
Member
From: Rome, Italy
Registered: 2020-10-16
Posts: 31

Re: Querying DB not working in GLPI 10

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

Board footer

Powered by FluxBB