pdo_010.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. --TEST--
  2. PDO Common: PDO::FETCH_CLASSTYPE and GROUP/UNIQUE
  3. --SKIPIF--
  4. <?php # vim:ft=php
  5. if (!extension_loaded('pdo')) die('skip');
  6. $dir = getenv('REDIR_TEST_DIR');
  7. if (false == $dir) die('skip no driver');
  8. require_once $dir . 'pdo_test.inc';
  9. PDOTest::skip();
  10. ?>
  11. --FILE--
  12. <?php
  13. if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
  14. require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
  15. $db = PDOTest::factory();
  16. $db->exec('CREATE TABLE classtypes(id int NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL UNIQUE)');
  17. $db->exec('INSERT INTO classtypes VALUES(0, \'stdClass\')');
  18. $db->exec('INSERT INTO classtypes VALUES(1, \'Test1\')');
  19. $db->exec('INSERT INTO classtypes VALUES(2, \'Test2\')');
  20. $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, classtype int, val VARCHAR(10), grp VARCHAR(10))');
  21. $db->exec('INSERT INTO test VALUES(1, 0, \'A\', \'Group1\')');
  22. $db->exec('INSERT INTO test VALUES(2, 1, \'B\', \'Group1\')');
  23. $db->exec('INSERT INTO test VALUES(3, 2, \'C\', \'Group2\')');
  24. $db->exec('INSERT INTO test VALUES(4, 3, \'D\', \'Group2\')');
  25. $stmt = $db->prepare('SELECT classtypes.name, test.grp AS grp, test.id AS id, test.val AS val FROM test LEFT JOIN classtypes ON test.classtype=classtypes.id');
  26. class Test1
  27. {
  28. public function __construct()
  29. {
  30. echo __METHOD__ . "()\n";
  31. }
  32. }
  33. class Test2
  34. {
  35. public function __construct()
  36. {
  37. echo __METHOD__ . "()\n";
  38. }
  39. }
  40. class Test3
  41. {
  42. public function __construct()
  43. {
  44. echo __METHOD__ . "()\n";
  45. }
  46. }
  47. $stmt->execute();
  48. var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_GROUP, 'Test3'));
  49. $stmt->execute();
  50. var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_UNIQUE, 'Test3'));
  51. ?>
  52. --EXPECTF--
  53. Test1::__construct()
  54. Test2::__construct()
  55. Test3::__construct()
  56. array(2) {
  57. ["Group1"]=>
  58. array(2) {
  59. [0]=>
  60. object(stdClass)#%d (2) {
  61. ["id"]=>
  62. string(1) "1"
  63. ["val"]=>
  64. string(1) "A"
  65. }
  66. [1]=>
  67. object(Test1)#%d (2) {
  68. ["id"]=>
  69. string(1) "2"
  70. ["val"]=>
  71. string(1) "B"
  72. }
  73. }
  74. ["Group2"]=>
  75. array(2) {
  76. [0]=>
  77. object(Test2)#%d (2) {
  78. ["id"]=>
  79. string(1) "3"
  80. ["val"]=>
  81. string(1) "C"
  82. }
  83. [1]=>
  84. object(Test3)#%d (2) {
  85. ["id"]=>
  86. string(1) "4"
  87. ["val"]=>
  88. string(1) "D"
  89. }
  90. }
  91. }
  92. Test1::__construct()
  93. Test2::__construct()
  94. Test3::__construct()
  95. array(2) {
  96. ["Group1"]=>
  97. object(Test1)#%d (2) {
  98. ["id"]=>
  99. string(1) "2"
  100. ["val"]=>
  101. string(1) "B"
  102. }
  103. ["Group2"]=>
  104. object(Test3)#%d (2) {
  105. ["id"]=>
  106. string(1) "4"
  107. ["val"]=>
  108. string(1) "D"
  109. }
  110. }