PHP 8.5.2 Released!

Pdo\Pgsql::getNotify

(PHP 8 >= 8.4.0)

Pdo\Pgsql::getNotifyRecebe notificação assíncrona

Descrição

public Pdo\Pgsql::getNotify(int $fetchMode = PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0): array|false

Retorna um conjunto de resultados que representa uma notificação assíncrona pendente.

Parâmetros

fetchMode

O formato como o conjunto de resultados deve ser retornado, uma das seguintes constantes:

timeoutMilliseconds
O tempo de espera por uma resposta, em milissegundos.

Valor Retornado

Se uma ou mais notificações estiverem pendentes, retorna uma única linha, com os campos message e pid, caso contrário retorna false.

Erros/Exceções

Uma exceção ValueError será lançada se fetchMode não for uma das constantes PDO::FETCH_* válidas.

Uma exceção ValueError será lançada se timeoutMilliseconds for menor que 0.

Um E_WARNING é gerado quando timeoutMilliseconds é maior que o valor que pode estar contido em um inteiro com sinal de 32 bits; nesse caso, será o valor máximo de um inteiro com sinal de 32 bits.

Veja Também

adicionar nota

Notas de Usuários 1 note

up
0
sage at sage dot sk
10 days ago
This page needs an example to understand that you **need** to explicitly call LISTEN before using getNotify, like shown in https://www.php.net/manual/en/function.pg-get-notify.php

<?php

$db = new PDO($dsn, $user, $password, $options);
$db->query('LISTEN test');
$notification = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 10000);

// or

$db = new Pdo\Pgsql($dsn, $user, $password, $options);
$db->query('LISTEN test');
$notification = $db->getNotify(PDO::FETCH_ASSOC, 10000);

// now you can call NOTIFY elsewhere
// PG> NOTIFY test, 'payload string';
var_dump($notification);

?>

array(3) {
  ["message"]=>
  string(4) "test"
  ["pid"]=>
  int(123565)
  ["payload"]=>
  string(14) "payload string"
}

If you called NOTIFY before calling LISTEN, nothing will be returned!

You receive the first notification only, and you have to call getNotify again. And call LISTEN again if DB connection drops.
To Top