Pdo\Pgsql::getNotify

(PHP 8 >= 8.4.0)

Pdo\Pgsql::getNotify โ€” ้žๅŒๆœŸใซ้€š็Ÿฅใ‚’ๅ–ๅพ—ใ™ใ‚‹

่ชฌๆ˜Ž

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

ไฟ็•™ไธญใฎ้žๅŒๆœŸใช้€š็Ÿฅใ‚’็คบใ™็ตๆžœใ‚ปใƒƒใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚

ใƒ‘ใƒฉใƒกใƒผใ‚ฟ

fetchMode

็ตๆžœใ‚ปใƒƒใƒˆใ‚’่ฟ”ใ™ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ€‚ ๆฌกใฎใ„ใšใ‚Œใ‹ใฎๅฎšๆ•ฐ:

timeoutMilliseconds
ใƒฌใ‚นใƒใƒณใ‚นใ‚’ๅพ…ใคๆ™‚้–“ใฎ้•ทใ•ใ€‚ใƒŸใƒช็ง’ใงๆŒ‡ๅฎšใ—ใพใ™ใ€‚

ๆˆปใ‚Šๅ€ค

ใฒใจใคไปฅไธŠใฎ้€š็ŸฅใŒไฟ็•™ไธญใ ใฃใŸๅ ดๅˆใ€ message ใจ pid ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๅซใ‚€1่กŒใ‚’่ฟ”ใ—ใพใ™ใ€‚ ใใ†ใงใชใ‘ใ‚Œใฐ false ใ‚’่ฟ”ใ—ใพใ™ใ€‚

ใ‚จใƒฉใƒผ / ไพ‹ๅค–

fetchMode ใŒๆœ‰ๅŠนใช PDO::FETCH_* ๅฎšๆ•ฐใฎใ„ใšใ‚Œใงใ‚‚ใชใ„ๅ ดๅˆใ€ValueError ใŒใ‚นใƒญใƒผใ•ใ‚Œใพใ™ใ€‚

timeoutMilliseconds ใŒ0 ๆœชๆบ€ใฎๅ ดๅˆใ€ ValueError ใŒใ‚นใƒญใƒผใ•ใ‚Œใพใ™ใ€‚

timeoutMilliseconds ใŒ็ฌฆๅทไป˜ใ32ใƒ“ใƒƒใƒˆใฎๆ•ดๆ•ฐใซ ๅŽใพใ‚‰ใชใ„ๅ ดๅˆใ€E_WARNING ใŒ็™บ็”Ÿใ—ใ€ ใใฎๅ€คใฏ็ฌฆๅทไป˜ใ32ใƒ“ใƒƒใƒˆใฎๆ•ดๆ•ฐใฎๆœ€ๅคงๅ€คใซไธธใ‚ใ‚‰ใ‚Œใพใ™ใ€‚

ๅ‚่€ƒ

  • PDO::query() - ใƒ—ใƒฌใƒผใ‚นใƒ›ใƒซใƒ€ใ‚’ๆŒ‡ๅฎšใ›ใšใซใ€SQL ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’ๆบ–ๅ‚™ใ—ใฆๅฎŸ่กŒใ™ใ‚‹
  • PDOStatement::fetch() - ็ตๆžœใ‚ปใƒƒใƒˆใ‹ใ‚‰ๆฌกใฎ่กŒใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • PDOStatement::fetchAll() - ็ตๆžœใ‚ปใƒƒใƒˆใ‹ใ‚‰ใ€ๆฎ‹ใฃใฆใ„ใ‚‹่กŒใ‚’ๅ–ๅพ—ใ™ใ‚‹
๏ผ‹add a note

User Contributed Notes 1 note

up
0
sage at sage dot sk ยถ
5 months 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.