PHP 8.5.2 Released!

Pdo\Pgsql::getNotify

(PHP 8 >= 8.4.0)

Pdo\Pgsql::getNotify非同期に通知を取得する

説明

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

保留中の非同期な通知を示す結果セットを返します。

パラメータ

fetchMode

結果セットを返すフォーマット。 次のいずれかの定数:

timeoutMilliseconds
レスポンスを待つ時間の長さ。ミリ秒で指定します。

戻り値

ひとつ以上の通知が保留中だった場合、 messagepid のフィールドを含む1行を返します。 そうでなければ false を返します。

エラー / 例外

fetchMode が有効な PDO::FETCH_* 定数のいずれでもない場合、ValueError がスローされます。

timeoutMilliseconds0 未満の場合、 ValueError がスローされます。

timeoutMilliseconds が符号付き32ビットの整数に 収まらない場合、E_WARNING が発生し、 その値は符号付き32ビットの整数の最大値に丸められます。

参考

add a note

User Contributed Notes 1 note

up
0
sage at sage dot sk
9 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